You are on page 1of 3

#include <iostream> #include <fstream> using namespace std; #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgproc.

hpp> using namespace cv; int ham(int** &tImg, char* fname, int &tWidth, int &tHeight, float& tAverage){ Mat tInput = imread("tMatching_template.jpg"); Mat input; cvtColor(tInput,input,CV_RGB2GRAY); tHeight = input.rows; tWidth = input.cols; tImg = new int* [tHeight]; tAverage = 0; for(int i = 0 ; i < tHeight ; i++) tImg[i] = new int[tWidth]; for(int i = 0 ; i < tHeight ; i++) for(int j = 0 ; j < tWidth ; j++){ tImg[i][j] = (int)input.at<uchar>(i,j); tAverage += tImg[i][j]; } tAverage = tAverage / (tHeight* tWidth); //nMask = 0; //for(int i = 0 ; i <m ; i++) // for(int j = 0 ; j <n ; j++) // nMask += mask[i][j]; //nMask = (nMask==0)?1:nMask; // namedWindow("TEMP"); imshow("TEMP",input); return 0; } void main(){ Mat input = imread("pic1.jpg"); Mat img;// START IMAGE cvtColor(input,img,CV_RGB2GRAY); Mat img_new = img.clone(); // FINAL IMAGE int tWidth = 45; // TEMPLATE WIDTH int tHeight = 29; // TEMPLATE HEIGHT float tAverage = 0; // Average Gray Level of Template

int** tImg = NULL; // Template Image ham(tImg, "tMatching_template.jpg", tWidth, tHeight, tAverage); //---------------------------------// cout << "iWIDTH : " << img.cols<<endl; cout << "iHEIGHT : " << img.rows<<endl; cout << "tWIDTH : " << tWidth<<endl; cout << "tHEIGHT : " << tHeight<<endl; cout << "tAverage : " << tAverage<<endl; namedWindow("BEFORE"); imshow("BEFORE",img); //-----------------------------------------// TEMPLATE MATCHING float iAverage = 0; // Average Gray Level of Image float threshold = 0.800; //Threshold float cor = 0; int foundX= 0, foundY = 0; /// COMPUTE for (int i = 0 ; i < img.rows ; i++){ if( (img.rows - i) < tHeight ) break; for(int j = 0 ; j < img.cols ; j ++){ if( (img.cols - j) < tWidth ) break; /// FIND AVERAGE GRAY LEVEL OF SECTION IMAGE for(int k = 0; k < tHeight; k++) for(int l = 0; l < tWidth ; l++){ iAverage += (int)img.at<uchar>(i + k, j + l); } iAverage = iAverage / (tHeight * tWidth); /// COMPUTE COR float numerator = 0; //tu so float denominator = 0; // mau so float tCor = 0; // numerator / denominator float sumxix = 0; // sum xi - x float sumyiy = 0; // sum yi - y //denominator = sqrt(sumxix * sumyiy) for(int k = 0; k < tHeight; k++) for(int l = 0; l < tWidth ; l++){ int xi = tImg[k][l]; int yi = img_new.at<uchar>(i + k, j + l); numerator += (xi - tAverage)*(yi - iAverage);

sumxix += (xi - tAverage)*(xi - tAverage); sumyiy += (yi - iAverage)*(yi - iAverage); } denominator = sqrt(sumxix*sumyiy); tCor = numerator / denominator; if(tCor >= threshold && tCor >= cor){ cor = tCor; foundX = i; foundY = j; cout << "FOUND : "<< cor <<endl; } } } ///FOUND? for (int i = 0 ; i < img.rows && cor != 0; i++) for(int j = 0 ; j < img.cols ; j ++){ if( i == foundX || j == foundY || i== foundX + tHeight || j == foundY + tWidth) img_new.at<uchar>(i,j) =255; } //------------------------------------------namedWindow("AFTER"); imshow("AFTER",img_new); waitKey(0); }

You might also like