Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
um make template matching program using opencv and c++. this is my code

C++
void track()
{
    if (select_flag)
    {
        //roiImg.copyTo(mytemplate);
//         select_flag = false;
        go_fast = true;
    }

//     imshow( "mytemplate", mytemplate ); waitKey(0);

    Mat result  =  TplMatch( img, mytemplate );
    Point match =  minmax( result ); 

    rectangle( img, match, Point( match.x + mytemplate.cols , match.y + mytemplate.rows ), CV_RGB(255, 0, 255), 1 );

	 char test[] =match;
	 WriteFile(hSerial,test,strlen(test),&btsIO,NULL);

     



    std::cout << "match: " << match << endl;

    /// latest match is the new template
  /*  Rect ROI = cv::Rect( match.x, match.y, mytemplate.cols, mytemplate.rows );
    roiImg = img( ROI );
    roiImg.copyTo(mytemplate);
    imshow( "roiImg", roiImg ); //waitKey(0);*/
}


how i send image cordinate into arduino as serial communication. " char test[] =match;" this line not work. pls help me
Posted
Updated 3-Jun-15 22:32pm
v2

You must create a string containing the point members x and y. How to create (format) the string depends on how the data are interpreted by the receiving program on your Arduino. Example:
C++
// Buffer must be large enough for all possible values to be send
char test[32];
// If Arduino expects data as 'x y' integers followed by line feed.
sprintf(test, "%d %d\n", (int)match.x, (int)match.y);
 
Share this answer
 
Comments
Jochen Arndt 4-Jun-15 5:12am    
Just change the format string to "x%d y%d\n".
Amal anjula 5-Jun-15 2:27am    
its ok sir. but i need another thing. when x=15 serial data is 15. i need it as "050". and when x=204 i need it as "204"is this possible?
Jochen Arndt 5-Jun-15 2:42am    
What do you mean by that (x=15 should be printed as '050').

If that is a typo and you need leading zeroes use "x%03d y%03d" where the '0' defines that numbers should be left padded with zeroes and the '3' defines the minimum number of characters to be printed. If the coordinates may be also negative you might use "x%+04d y%+04d". Then all numbers will have a sign.

Have a look at the printf format specifications. You should know them because you will need them as C/C++ programmer.
Amal anjula 11-Jun-15 6:06am    
sir , another problem. my code is change to like this below. but now serial data not work properly. have any idea?

cv::RotatedRect boundingBox = cv::minAreaRect(contours[biggestContourIdx]);
cv::Point2f corners[4];
boundingBox.points(corners);
cv::line(drawing, corners[0], corners[1], cv::Scalar(255,255,255));

cv::line(drawing, corners[1], corners[2], cv::Scalar(255,255,255));
cv::line(drawing, corners[2], corners[3], cv::Scalar(255,255,255));
cv::line(drawing, corners[3], corners[0], cv::Scalar(255,255,255));

int w=0;
int h=0;
int area;

//area=abs(corners[0].x-corners[1].x);
w=(corners[0].x-corners[1].x);
h=(corners[0].y-corners[1].y);
area=h*w;

cout<<w;
cout<<"\t";
cout<<h;
cout<<"\t";
cout<<area;
cout<<"\t";


cout<<boundingBox.size;
cout<<"\n";

char test[32];
sprintf(test,"%dx\n%dy\n", (int)w, (int)h);

imshow("MyVideo1", drawing);




cv::namedWindow("Canny");
cv::imshow("Canny",canny_output);


cv::namedWindow("Fill");
cv::imshow("Fill",drawing);
Jochen Arndt 11-Jun-15 6:11am    
This

sprintf(test,"%dx\n%dy\n", (int)w, (int)h);

does not look like the previously used format where the x and y characters has been printed in front of the corresponding values.
Admittedly you have a mismatch on match.
Even the technical workaround
char * p = (char * ) match;

would be a mistake, because, on the Arduino side, there is no notion about the Point struct).
Probably you have to send the value of the point coordinates (namely match.x, match.y) to the Arduino device. However the format you should use (e.g. binary or textual representation) depends on what is expected by the other side.
 
Share this 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