Click here to Skip to main content
14,735,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have to build up face detection system for human identification. if correct human present the door will open.like that. um using opencv and c++ for that.um using template matching alogo for that. no i want know what is the images matching percentage. how do it? any way is there have enchanted system for human face identification? thank you
below is my current code:

What I have tried:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/// Global Variables
Mat img; Mat templ; Mat result;
char* image_window = "Source Image";
char* result_window = "Result window";

int match_method;
int max_Trackbar = 5;

/// Function Headers
void MatchingMethod(int, void*);

/** @function main */
int main(int argc, char** argv)
{
	/// Load image and template
	img = imread("IMG_20160214_092738.jpg", 1);
	cv::resize(img, img, cv::Size(), 0.35, 0.3);
	templ = imread("IMG_20160131_120252.jpg", 1);
	cv::resize(templ, templ, cv::Size(), 0.25, 0.25);
	/// Create windows
	namedWindow(image_window, CV_WINDOW_AUTOSIZE);
	namedWindow(result_window, CV_WINDOW_AUTOSIZE);

	/// Create Trackbar
	char* trackbar_label = "Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED";
	createTrackbar(trackbar_label, image_window, &match_method, max_Trackbar, MatchingMethod);
	//match_method
	MatchingMethod(0, 0);

	waitKey(0);
	return 0;
}

/**
* @function MatchingMethod
* @brief Trackbar callback
*/
void MatchingMethod(int, void*)
{
	/// Source image to display
	Mat img_display;
	img.copyTo(img_display);

	/// Create the result matrix
	int result_cols = img.cols - templ.cols + 1;
	int result_rows = img.rows - templ.rows + 1;

	result.create(result_rows, result_cols, CV_32FC1);

	/// Do the Matching and Normalize
	matchTemplate(img, templ, result, match_method);
	normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());

	/// Localizing the best match with minMaxLoc
	double minVal; double maxVal; Point minLoc; Point maxLoc;
	Point matchLoc;
	
	minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
	cout << minVal << "***" << maxVal << endl;
	/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
	if (match_method == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED)
	{
		matchLoc = minLoc;
	}
	else
	{
		matchLoc = maxLoc;
	}

	/// Show me what you got
	rectangle(img_display, matchLoc, Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), Scalar::all(0), 2, 8, 0);
	rectangle(result, matchLoc, Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), Scalar::all(0), 2, 8, 0);

	imshow(image_window, img_display);
	imshow(result_window, result);

	return;
}<pre>
Posted
Updated 2-Jan-17 15:29pm
v2

It looks like you scale the pics some different. Wouldnt it be better to scale them to the same (and fixed) size?

Do you have Googled about this issue? (I dont believe it) because the results are fine and I would recommand toe visit the fifth link for some possible solution approaches.
   
Comments
Afzaal Ahmad Zeeshan 2-Jan-17 16:11pm
   
5ed.

Of course, he just needs to scale the images to same size and then compare each element of the array to other; similar ones increase the percentage etc.
Solution 1 gives you a good way to calculate the solutions, you can use the Mat object to perform scaling operations and that will be easier — then you can later compare every element to the relative element in other image Mat (see my comment to Solution 1).

Just a sample of what we are talking about, there is a .NET framework alternate to this sort of issue, Simple image comparison in .NET[^] that article uses the same mechanism in a much better way to compare each pixel with the relative pixel in other image.

You can still implement the similar idea in C++ using OpenCV — or use C# with EmguCV>
   
Face recognition is one of the most complex subjects in computer vision. To ask for a "quick answer" on how to write a good face recognition system is like to ask "how to write an opera in three easy steps". Every simple pixel-to-pixel comparison will fail for several reasons:

- the person might face the camera in slightly differing positions and angles
- the lighting might be different on another day and time
- the person might be in another mood, wear glasses or not, have new haircut, ...
- and a thousand other difficulties.

Professional programs apply a whole bunch of techniques to accomplish there results:

- all kinds of normalization (brightness, contrast, position, angle)
- extraction of abstract features (face dimensions, dimension ratios, ...)
- reduction of the feature vector
- and many more

You may find tons of literature in the internet about the subject. Be prepared to spend a couple of years before you reach a professional level.

Sorry, but this question can't be answered seriously in a "quick answer" post.
   
Read this:
Facial recognition system - Wikipedia[^]
You will see that the subject is so complex that it is out off topic for a Quick Answer.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900