diff --git a/source/main.cpp b/source/main.cpp index ee8e4f2951eae6d45e045f1a58a09216d0f1420c..8e5a464c82f22a572a83261fdcafc895c9cc55e8 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -34,11 +34,16 @@ #include "opencv2/highgui/highgui_c.h" #include "opencv2/core/utility.hpp" +using namespace cv; +using namespace std; + using namespace cv; using namespace std; Mat3b img = imread("C:\\Users\\RMD\\desktop\\1233.png"); Mat3b templ = imread("C:\\Users\\RMD\\desktop\\t1.png"); Mat3b templ2 = imread("C:\\Users\\RMD\\desktop\\t2.png"); +Mat3b templ3 = imread("C:\\Users\\RMD\\desktop\\123456.png"); +Mat3b templ4 = imread("C:\\Users\\RMD\\desktop\\123456.png"); Mat final = imread("C:\\Users\\RMD\\desktop\\123456.png"); #include "opencv2/features2d.hpp" #include "opencv2/features2d/features2d.hpp" @@ -48,6 +53,7 @@ struct rectanglesave { int ypos; int rowsize; int colsize; + bool status; }; rectanglesave save[500] = { NULL }; using namespace cv; @@ -71,8 +77,8 @@ void on_mouse(int event, int x, int y, int flags, void*) case EVENT_MBUTTONUP: cout << "EVENT_DBDBBUTTONUP: " << x << ", " << y << endl; - - while (save[i].xpos!=0) { + + while (save[i].xpos != 0) { double distance = sqrt(pow(x - save[i].xpos, 2) + pow(y - save[i].ypos, 2)); if (distance < min) { min = distance; @@ -81,8 +87,11 @@ void on_mouse(int event, int x, int y, int flags, void*) i++; } printf("xpos: %d, ypos: %d, col: %d, cols: %d\n", save[index].xpos, save[index].ypos, save[index].colsize, save[index].rowsize); - rectangle(img, Rect(save[index].xpos, save[index].ypos, save[index].colsize, save[index].rowsize), Scalar(0, 0,255), 1); - rectangle(final, Rect(save[index].xpos, save[index].ypos, save[index].colsize-4, save[index].rowsize-4), Scalar(0, 0, 0), 1); + rectangle(img, Rect(save[index].xpos, save[index].ypos, save[index].colsize, save[index].rowsize), Scalar(0, 0, 255), 1); + if (save[index].status == true) + rectangle(final, Rect(save[index].xpos, save[index].ypos, save[index].colsize - 4, save[index].rowsize - 4), Scalar(0, 0, 0), 1); + else + rectangle(final, Rect(save[index].xpos - 10, save[index].ypos - 6, save[index].colsize - 4, save[index].rowsize - 4), Scalar(0, 0, 0), 1); imshow("final", img); imshow("final2", final); @@ -153,12 +162,19 @@ int main() Mat1b img_gray; Mat1b templ_gray; Mat1b templ_gray2; + Mat1b templ_gray3; + Mat1b templ_gray4; + cvtColor(img, img_gray, COLOR_BGR2GRAY); cvtColor(templ, templ_gray, COLOR_BGR2GRAY); cvtColor(templ2, templ_gray2, COLOR_BGR2GRAY); + cvtColor(templ3, templ_gray3, COLOR_BGR2GRAY); + cvtColor(templ4, templ_gray4, COLOR_BGR2GRAY); final = ~final; Mat1f result; Mat1f result2; + Mat1f result3; + Mat1f result4; namedWindow("final"); setTrackbarPos("low threshold", "final", 50); @@ -168,13 +184,19 @@ int main() resizeWindow("final", 1024, 1024); int test1[500] = { NULL }; int test2[500] = { NULL }; + int test3[500] = { NULL }; + int test4[500] = { NULL }; double thresh = 0.545; while (1) { Mat1f result; Mat1f result2; + Mat1f result3; + Mat1f result4; matchTemplate(img, templ, result, TM_CCOEFF_NORMED); matchTemplate(img, templ2, result2, TM_CCOEFF_NORMED); + matchTemplate(img, templ3, result3, TM_CCOEFF_NORMED); + matchTemplate(img, templ4, result4, TM_CCOEFF_NORMED); createTrackbar("Set !", "final", 0, 1000, on_trackbar); waitKey(0); @@ -185,24 +207,33 @@ int main() thresh = (double)(getTrackbarPos("Set !", "final")) / 100; check3 = true; - matchTemplate(img, templ, result, TM_CCOEFF_NORMED); - matchTemplate(img, templ2, result2, TM_CCOEFF_NORMED); + //matchTemplate(img, templ, result, TM_CCOEFF_NORMED); + //matchTemplate(img, templ2, result2, TM_CCOEFF_NORMED); thresh = (double)(getTrackbarPos("Set !", "final")) / 100; threshold(result, result, thresh, 1., THRESH_BINARY); threshold(result2, result2, thresh, 1., THRESH_BINARY); + threshold(result3, result3, thresh, 1., THRESH_BINARY); + threshold(result4, result4, thresh, 1., THRESH_BINARY); printf("thresh: %f", thresh); - Mat1b resb, resb2; + Mat1b resb, resb2,resb3,resb4; result.convertTo(resb, CV_8U, 255); result2.convertTo(resb2, CV_8U, 255); + result3.convertTo(resb3, CV_8U, 255); + result4.convertTo(resb4, CV_8U, 255); vector<vector<Point>> contours; vector<vector<Point>> contours2; + vector<vector<Point>> contours3; + vector<vector<Point>> contours4; findContours(resb, contours, RETR_LIST, CHAIN_APPROX_SIMPLE); findContours(resb2, contours2, RETR_LIST, CHAIN_APPROX_SIMPLE); - printf("contoursize: %d", contours.size()); + findContours(resb3, contours3, RETR_LIST, CHAIN_APPROX_SIMPLE); + findContours(resb4, contours4, RETR_LIST, CHAIN_APPROX_SIMPLE); + + //printf("contoursize: %d", contours.size()); int num111 = -100, num222 = -100; for (int i = 0; i < contours.size(); ++i) { @@ -238,11 +269,11 @@ int main() //printf("x: %d, y: %d\n", max_point.x, max_point.y); if (templ.cols < templ.rows) { //max_point.x = (int)(check-4 *(floor((double)max_point.x / (double)(check-4)+ 0.5))); - max_point.y = (int)((templ.rows + 2) *(floor((double)max_point.y / (double)(templ.rows + 2) + 0.5))); + max_point.y = (int)((templ.rows + 2) * (floor((double)max_point.y / (double)(templ.rows + 2) + 0.5))); } else - max_point.x = (int)((templ.cols + 2) *(floor((double)max_point.x / (double)(templ.cols + 2) + 0.5))); - save[count].xpos = max_point.x, save[count].ypos=max_point.y, save[count].colsize = templ.cols , save[count].rowsize=templ.rows; + max_point.x = (int)((templ.cols + 2) * (floor((double)max_point.x / (double)(templ.cols + 2) + 0.5))); + save[count].xpos = max_point.x, save[count].ypos = max_point.y, save[count].colsize = templ.cols, save[count].rowsize = templ.rows, save[count].status = true;; count++; rectangle(final, Rect(max_point.x, max_point.y, templ.cols - 4, templ.rows - 4), Scalar(100, 100, 100), 1); rectangle(img, Rect(max_point.x, max_point.y, templ.cols, templ.rows), Scalar(0, 255, 0), 1); @@ -291,17 +322,63 @@ int main() if (ch == true) { if (templ2.cols < templ2.rows) { - //max_point.x = (int)(check-4 *(floor((double)max_point.x / (double)(check-4)+ 0.5))); - max_point.y = (int)(templ2.rows *(floor((double)max_point.y / (double)(templ2.rows) + 0.5))); + max_point.y = (int)(templ2.rows * (floor((double)max_point.y / (double)(templ2.rows) + 0.5))); } else - max_point.x = (int)(templ2.cols *(floor((double)max_point.x / (double)(templ2.cols) + 0.5))); - save[count].xpos = max_point.x, save[count].ypos = max_point.y, save[count].colsize = templ2.cols, save[count].rowsize = templ2.rows; + max_point.x = (int)(templ2.cols * (floor((double)max_point.x / (double)(templ2.cols) + 0.5))); + save[count].xpos = max_point.x, save[count].ypos = max_point.y, save[count].colsize = templ2.cols, save[count].rowsize = templ2.rows, save[count].status = false; count++; - rectangle(final, Rect(max_point.x, max_point.y, templ2.cols - 4, templ2.rows - 4), Scalar(100, 100, 100), 1); + rectangle(final, Rect(max_point.x - 10, max_point.y - 6, templ2.cols - 4, templ2.rows - 4), Scalar(100, 100, 100), 1); rectangle(img, Rect(max_point.x, max_point.y, templ2.cols, templ2.rows), Scalar(0, 255, 0), 1); } + num111 = num11; + num222 = num22; + + num11 = max_point.x; + num22 = max_point.y; + + } + num111 = -100, num222 = -100; + num11 = -100, num22 = -100; + int t3[100] = { NULL }, t4[100] = { NULL }; + for (int i = 0; i < contours3.size(); ++i) + { + Mat1b mask(result3.rows, result3.cols, uchar(0)); + drawContours(mask, contours3, i, Scalar(255), CV_FILLED); + + Point max_point; + double max_val; + minMaxLoc(result3, NULL, &max_val, NULL, &max_point, mask); + save[count].xpos = max_point.x, save[count].ypos = max_point.y, save[count].colsize = templ3.cols, save[count].rowsize = templ3.rows, save[count].status = true; + count++; + rectangle(final, Rect(max_point.x, max_point.y , templ3.cols - 4, templ3.rows - 4), Scalar(100, 100, 100), 1); + rectangle(img, Rect(max_point.x, max_point.y, templ3.cols, templ3.rows), Scalar(0, 255, 0), 1); + + + num111 = num11; + num222 = num22; + + num11 = max_point.x; + num22 = max_point.y; + + }num111 = -100, num222 = -100; + num11 = -100, num22 = -100; + int t5[100] = { NULL }, t6[100] = { NULL }; + for (int i = 0; i < contours4.size(); ++i) + { + Mat1b mask(result4.rows, result4.cols, uchar(0)); + drawContours(mask, contours4, i, Scalar(255), CV_FILLED); + + Point max_point; + double max_val; + minMaxLoc(result4, NULL, &max_val, NULL, &max_point, mask); + save[count].xpos = max_point.x, save[count].ypos = max_point.y, save[count].colsize = templ3.cols, save[count].rowsize = templ3.rows, save[count].status = true; + count++; + rectangle(final, Rect(max_point.x, max_point.y, templ4.cols - 4, templ4.rows - 4), Scalar(100, 100, 100), 1); + rectangle(img, Rect(max_point.x, max_point.y, templ4.cols, templ4.rows), Scalar(0, 255, 0), 1); + + num111 = num11; num222 = num22; @@ -319,10 +396,7 @@ int main() printf("adsf"); printf("!!"); putText(img, "Finish", Point(30, 70), 2, 0.4, Scalar(0, 0, 255), 1); - //int aaa = 133,bbb = 9; - //printf("\n********값 : %d더블값:%lf 반올림값 : %lf",aaa,(double)aaa,floor(aaa/(double)bbb+0.5)); - //printf("\n%d", (int)floor(aaa / (double)bbb + 0.5)); - imshow("final", img);// + imshow("final", img); } waitKey(0);