cv::Mat FacePreprocessor::normalizeImage(cv::Mat face){ cv::Mat image32f; face.convertTo(image32f,CV_32F); cv::Mat mu; blur(image32f, mu,cv::Size(registeredFaceWidth,registeredFaceWidth)); cv::Mat mu2; blur(image32f.mul(image32f), mu2,cv::Size(registeredFaceWidth,registeredFaceWidth)); cv::Mat sigma; cv::sqrt(mu2 - mu.mul(mu), sigma); cv::Mat out; cv::Mat filtered; cv::subtract(image32f,mu,out); //cv::imshow("Subtract mean", mat2gray(out)); cv::Mat out2; cv::divide(out,sigma,out2); //imshow("divide", mat2gray(out2)); //cout<<out2<<endl; //cv::imshow("initial image", mat2gray(image32f)); //cv::imshow("mu", mat2gray(mu)); //cv::imshow("sigma",mat2gray(sigma)); //waitKey(); //cout<<mat2gray(out2)<<endl; return mat2gray(out2); }
// // Calculates the TanTriggs Preprocessing as described in: // // Tan, X., and Triggs, B. "Enhanced local texture feature sets for face // recognition under difficult lighting conditions.". IEEE Transactions // on Image Processing 19 (2010), 1635–650. // // Default parameters are taken from the paper. // cv::Mat FacePreprocessor::tan_triggs_preprocessing(InputArray src, float alpha, float tau , float gamma, int sigma0, int sigma1) { // Convert to floating point: Mat X = src.getMat(); X.convertTo(X, CV_32FC1); // Start preprocessing: Mat I; pow(X, gamma, I); // Calculate the DOG Image: { Mat gaussian0, gaussian1; // Kernel Size: int kernel_sz0 = (3*sigma0); int kernel_sz1 = (3*sigma1); // Make them odd for OpenCV: kernel_sz0 += ((kernel_sz0 % 2) == 0) ? 1 : 0; kernel_sz1 += ((kernel_sz1 % 2) == 0) ? 1 : 0; GaussianBlur(I, gaussian0, Size(kernel_sz0,kernel_sz0), sigma0, sigma0); GaussianBlur(I, gaussian1, Size(kernel_sz1,kernel_sz1), sigma1, sigma1); subtract(gaussian0, gaussian1, I); } { double meanI = 0.0; { Mat tmp; pow(abs(I), alpha, tmp); meanI = mean(tmp).val[0]; } I = I / pow(meanI, 1.0/alpha); } { double meanI = 0.0; { Mat tmp; pow(min(abs(I), tau), alpha, tmp); meanI = mean(tmp).val[0]; } I = I / pow(meanI, 1.0/alpha); } // Squash into the tanh: { for(int r = 0; r < I.rows; r++) { for(int c = 0; c < I.cols; c++) { I.at<float>(r,c) = tanh(I.at<float>(r,c) / tau); } } I = tau * I; } return mat2gray(I); }
cv::Mat FacePreprocessor::meanNormalizeImage(cv::Mat face){ cv::Mat image32f; face.convertTo(image32f,CV_32F); cv::Mat mu; blur(image32f, mu,cv::Size(registeredFaceWidth,registeredFaceWidth)); cv::Mat out; cv::subtract(image32f,mu,out); return mat2gray(out); }
void findEyeCenterByColorSegmentation(const Mat& image, Point2f & eyeCoord, float coordinateWeight, int kmeansIterations, int kmeansRepeats, int blurSize) { Mat img, gray_img; Mat colorpoints, kmeansPoints; img = equalizeImage(image); medianBlur(img, img, blurSize); cvtColor(image, gray_img, CV_BGR2GRAY); gray_img = imcomplement(gray_img); vector<Mat> layers(3); split(img, layers); for (int i = 0 ; i < layers.size(); i++) { layers[i] = layers[i].reshape(1,1).t(); } hconcat(layers, colorpoints); // add coordinates colorpoints.convertTo(colorpoints, CV_32FC1); Mat coordinates = matrixPointCoordinates(img.rows,img.cols,false) *coordinateWeight; hconcat(colorpoints, coordinates, kmeansPoints); Mat locIndex(img.size().area(),kmeansIterations,CV_32FC1,Scalar::all(-1)); linspace(0, img.size().area(), 1).copyTo(locIndex.col(0)); Mat index_img(img.rows,img.cols,CV_32FC1,Scalar::all(0)); Mat bestLabels, centers, clustered , colorsum , minColorPtIndex; for(int it = 1 ; it < kmeansIterations ; it++) { if (kmeansPoints.rows < 2) { break; } kmeans(kmeansPoints,2,bestLabels,TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, kmeansRepeats, 0.001),kmeansRepeats,KMEANS_PP_CENTERS,centers); reduce(centers.colRange(0, 3), colorsum, 1, CV_REDUCE_SUM); if (colorsum.at<float>(0) < colorsum.at<float>(1)) { findNonZero(bestLabels==0, minColorPtIndex); } else { findNonZero(bestLabels==1, minColorPtIndex); } minColorPtIndex = minColorPtIndex.reshape(1).col(1); for (int i = 0; i <minColorPtIndex.rows ; i ++) { locIndex.at<float>(i,it) = locIndex.at<float>(minColorPtIndex.at<int>(i),it-1); } Mat temp; for (int i = 0; i <minColorPtIndex.rows ; i ++) { temp.push_back(kmeansPoints.row(minColorPtIndex.at<int>(i))); } temp.copyTo(kmeansPoints); temp.release(); for (int i = 0 ; i < minColorPtIndex.rows ; i ++) { int r, c; ind2sub(locIndex.at<float>(i,it), index_img.cols, index_img.rows, r, c); index_img.at<float>(r,c) +=1; } } // imagesc("layered",mat2gray(index_img)); Mat layerweighted_img = index_img.mul(index_img); layerweighted_img = mat2gray(layerweighted_img); gray_img.convertTo(gray_img, CV_32FC1,1/255.0); Mat composed = gray_img.mul(layerweighted_img); float zoomRatio = 5.0f; Mat zoomed; imresize(composed, zoomRatio, zoomed); Mat score = calculateImageSymmetryScore(zoomed); // imagesc("score", score); Mat scoresum; reduce(score.rowRange(0, zoomed.cols/6), scoresum, 0, CV_REDUCE_SUM,CV_32FC1); // plotVectors("scoresum", scoresum.t()); double minVal , maxVal; Point minLoc, maxLoc; minMaxLoc(scoresum,&minVal,&maxVal,&minLoc,&maxLoc); float initialHC = (float)maxLoc.x/zoomRatio; line(zoomed, Point(maxLoc.x,0), Point(maxLoc.x,zoomed.rows-1), Scalar::all(255)); // imshow("zoomed", zoomed); int bestx = 0,bestlayer = 0; Mat bestIndex_img = index_img >=1; minMaxLoc(index_img,&minVal,&maxVal,&minLoc,&maxLoc); for (int i = 1 ; i<=maxVal; i++) { Mat indexlayer_img = index_img >=i; medianBlur(indexlayer_img, indexlayer_img, 5); erode(indexlayer_img, indexlayer_img, blurSize); erode(indexlayer_img, indexlayer_img, blurSize); indexlayer_img = removeSmallBlobs(indexlayer_img); indexlayer_img = fillHoleInBinary(indexlayer_img); indexlayer_img = fillConvexHulls(indexlayer_img); Mat score = calculateImageSymmetryScore(indexlayer_img); Mat scoresum; reduce(score.rowRange(0, indexlayer_img.cols/6), scoresum, 0, CV_REDUCE_SUM,CV_32FC1); minMaxLoc(scoresum,&minVal,&maxVal,&minLoc,&maxLoc); if (abs(maxLoc.x - initialHC) < abs(bestx - initialHC)) { if (sum(indexlayer_img)[0]/255 < indexlayer_img.size().area()/5*2 && sum(indexlayer_img)[0]/255 > indexlayer_img.size().area()/6) { bestx = maxLoc.x; bestlayer = i; bestIndex_img = indexlayer_img.clone(); } } } Point2f massCenter = findMassCenter_BinaryBiggestBlob(bestIndex_img); eyeCoord = Point2f(initialHC,massCenter.y); }