diff --git a/source/main.cpp b/source/main.cpp index d252d81a0347cf2c477e4940ce97c7b078c8cf27..ee8e4f2951eae6d45e045f1a58a09216d0f1420c 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -40,6 +40,18 @@ 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"); Mat final = imread("C:\\Users\\RMD\\desktop\\123456.png"); +#include "opencv2/features2d.hpp" +#include "opencv2/features2d/features2d.hpp" +#include "opencv2/features2d/hal/interface.h" +struct rectanglesave { + int xpos; + int ypos; + int rowsize; + int colsize; +}; +rectanglesave save[500] = { NULL }; +using namespace cv; +using namespace std; bool check = true; int startx = 0, starty = 0; bool check2 = true; @@ -47,12 +59,36 @@ bool check3 = true; Mat tempo; using namespace cv; using namespace std; +#include <cstdlib> +#include <cmath> void on_mouse(int event, int x, int y, int flags, void*) { - + double min = 100; + int index; + int i = 0; switch (event) { - case EVENT_LBUTTONDOWN: + + case EVENT_MBUTTONUP: + cout << "EVENT_DBDBBUTTONUP: " << x << ", " << y << endl; + + 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; + index = i; + } + 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); + imshow("final", img); + imshow("final2", final); + + i = 0; + break; + case EVENT_LBUTTONUP: if (check == true) check = false; else @@ -71,9 +107,6 @@ void on_mouse(int event, int x, int y, int flags, void*) } //cout << "EVENT_LBUTTONDOWN: " << x << ", " << y << endl; break; - case EVENT_LBUTTONUP: - //cout << "EVENT_LBUTTONUP: " << x << ", " << y << endl; - break; case EVENT_RBUTTONDOWN: if (check2 == true) check2 = false; @@ -106,6 +139,7 @@ void on_mouse(int event, int x, int y, int flags, void*) break; } } + #include <iostream> #include <cstdlib> #include <cmath> @@ -128,10 +162,10 @@ int main() namedWindow("final"); setTrackbarPos("low threshold", "final", 50); - + int count = 0; imshow("final", img); imshow("final2", final); - + resizeWindow("final", 1024, 1024); int test1[500] = { NULL }; int test2[500] = { NULL }; double thresh = 0.545; @@ -169,7 +203,7 @@ int main() findContours(resb, contours, RETR_LIST, CHAIN_APPROX_SIMPLE); findContours(resb2, contours2, RETR_LIST, CHAIN_APPROX_SIMPLE); printf("contoursize: %d", contours.size()); - int num111=-100, num222=-100; + int num111 = -100, num222 = -100; for (int i = 0; i < contours.size(); ++i) { Mat1b mask(result.rows, result.cols, uchar(0)); @@ -178,7 +212,7 @@ int main() Point max_point; double max_val; minMaxLoc(result, NULL, &max_val, NULL, &max_point, mask); - + int check; if (templ2.cols > templ2.rows) @@ -193,7 +227,7 @@ int main() while (1) { if (test1[j] == NULL) break; - if (abs(max_point.x - test1[j - 1]) < check/3 && abs(max_point.y - test2[j - 1]) < check/3) { + if (abs(max_point.x - test1[j - 1]) < check / 3 && abs(max_point.y - test2[j - 1]) < check / 3) { ch = false; break; } @@ -201,27 +235,29 @@ int main() } if (ch == true) { - printf("x: %d, y: %d\n", max_point.x, max_point.y); + //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))); + 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; + 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); + } - - - + + num111 = num1; num222 = num2; - + num1 = max_point.x; num2 = max_point.y; } num111 = -100, num222 = -100; - int num11=-100, num22=-100; + int num11 = -100, num22 = -100; int t1[100] = { NULL }, t2[100] = { NULL }; for (int i = 0; i < contours2.size(); ++i) { @@ -238,42 +274,43 @@ int main() else check = templ2.cols; - bool ch=true; + bool ch = true; t1[i - 1] = max_point.x; t2[i - 1] = max_point.y; int j = 0; - while(1) { + while (1) { if (t1[j] == NULL) break; - if (abs(max_point.x - t1[j-1]) < check/3 && abs(max_point.y - t2[j-1]) < check/3) { + if (abs(max_point.x - t1[j - 1]) < check / 3 && abs(max_point.y - t2[j - 1]) < check / 3) { ch = false; break; } j++; } - - if (ch==true) { - + + 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))); - + 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; + count++; rectangle(final, Rect(max_point.x, max_point.y, 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; - + } Mat mask = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2, 2), cv::Point(1, 1)); - + setMouseCallback("final", on_mouse); namedWindow("final2"); setMouseCallback("final2", on_mouse);