std::vector<double> FaceMatchingSingle::getNormalizedSimilarity(cv::Mat& target1, cv::Mat& target2) {
    cv::Mat sample1 = cropImg(target1);
    cv::Mat sample2 = cropImg(target2);
    if (sample1.type() == CV_8UC3) {
        cv::cvtColor(sample1, sample1, CV_BGR2GRAY);
    }
    if (sample2.type() == CV_8UC3) {
        cv::cvtColor(sample2, sample2, CV_BGR2GRAY);
    }
    cv::Mat proj1 = cv::subspaceProject(W, mean, sample1.reshape(1, 1));
    cv::Mat proj2 = cv::subspaceProject(W, mean, sample2.reshape(1, 1));
    double distance = cv::norm(proj1, proj2);
    double similarity = ((maxDistance - distance) / maxDistance) * 100;

    if (similarity < 0){
        similarity = 0.0;
    }
    if (similarity > 100.0){
        similarity = 100.0;
    }
    std::vector<double> similarities;
    similarities.push_back(similarity);
    this->similarities.push_back(similarity);
    //Audrey
    //All other calling functions call this similarities values
    return similarities;
}
double FaceMatchingSingle::getDistance(cv::Mat& target1, cv::Mat& target2) {
    cv::Mat sample1 = cropImg(target1);
    cv::Mat sample2 = cropImg(target2);
    if (sample1.type() == CV_8UC3) {
        commonTool.log("error, why color?");
        cv::cvtColor(sample1, sample1, CV_BGR2GRAY);
    }
    if (sample2.type() == CV_8UC3) {
        commonTool.log("error, why color ?");
        cv::cvtColor(sample2, sample2, CV_BGR2GRAY);
    }
    if (sample1.type() != sample2.type()) {
        commonTool.log("error, diffrent image type of sample");
    }
    cv::Mat proj1 = cv::subspaceProject(W, mean, sample1.reshape(1, 1));
    cv::Mat proj2 = cv::subspaceProject(W, mean, sample2.reshape(1, 1));
    return cv::norm(proj1, proj2);
}
Ejemplo n.º 3
0
void FaceFeatureRecognitionApp::GenerateNonFaceFromFace(void)
{
    std::string fileName;
    char filterName[] = "Land Files(*.txt)\0*.txt\0";
    MagicCore::ToolKit::FileOpenDlg(fileName, filterName);
    std::string imgPath = fileName;
    std::string::size_type pos = imgPath.rfind("/");
    if (pos == std::string::npos)
    {
        pos = imgPath.rfind("\\");
    }
    imgPath.erase(pos);
    std::ifstream fin(fileName);
    int dataSize;
    fin >> dataSize;
    int cropSize = 40;
    int outputSize = 64;
    cv::Size cvOutputSize(outputSize, outputSize);
    int imgNumPerData = 1;
    srand(time(NULL));
    for (int dataId = 0; dataId < dataSize; dataId++)
    {
        std::string imgName;
        fin >> imgName;
        imgName = imgPath + imgName;
        pos = imgName.rfind(".");
        imgName.replace(pos, 5, "_gray.jpg");
        /*cv::Mat imgOrigin = cv::imread(imgName);
        cv::Mat grayImg;
        cv::cvtColor(imgOrigin, grayImg, CV_BGR2GRAY);
        imgOrigin.release();*/
        cv::Mat grayImg = cv::imread(imgName);
        int imgH = grayImg.rows;
        int hMax = imgH - cropSize;
        int imgW = grayImg.cols;
        int wMax = imgW - cropSize;
        int synImgNum = 0;
        while (synImgNum < imgNumPerData)
        {
            int sH = rand() % hMax;
            int sW = rand() % wMax;
            cv::Mat cropImg(cropSize, cropSize, CV_8UC1);
            for (int hid = 0; hid < cropSize; hid++)
            {
                for (int wid = 0; wid < cropSize; wid++)
                {
                    cropImg.ptr(hid, wid)[0] = grayImg.ptr(sH + hid, sW + wid)[0];
                }
            }
            cv::Mat outputImg(cvOutputSize, CV_8UC1);
            cv::resize(cropImg, outputImg, cvOutputSize);
            cropImg.release();
            std::stringstream ss;
            ss << "./nonFaceFromFace/nonFace" << dataId << "_" << synImgNum << "_40.jpg";
            std::string outputName;
            ss >> outputName;
            cv::imwrite(outputName, outputImg);
            outputImg.release();
            synImgNum++;
        }
        grayImg.release();
    }
    fin.close();
    DebugLog << "GenerateNonFaceFromFace Done" << std::endl;
}