diff --git a/source/main.cpp b/source/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f02be7f07c2140cb47f70cd5adfdc43780c23f9e --- /dev/null +++ b/source/main.cpp @@ -0,0 +1,277 @@ +#include "opencv2/core/core.hpp" +#include "opencv2/highgui/highgui.hpp" +#include "opencv2/imgproc/imgproc.hpp" +#include "opencv2/opencv.hpp" +#include "opencv2/opencv_modules.hpp" +#include "iostream" +#include <stdio.h> +#include <stdlib.h> +#include "opencv2/highgui/highgui.hpp" +#include "opencv2/opencv.hpp" +#include "opencv2/imgproc/imgproc.hpp" + +#include "opencv2/core/core.hpp" + +#include "opencv2/core/core_c.h" + +#include "opencv2/highgui/highgui_c.h" + +#include "opencv2/imgproc/imgproc_c.h" + +#include "opencv2/video/video.hpp" + +#include "opencv2/features2d/features2d.hpp" + +#include "opencv2/objdetect/objdetect.hpp" + +#include "opencv2/flann/miniflann.hpp" + +#include "opencv2/photo/photo.hpp" + +#include "opencv2/calib3d/calib3d.hpp" + +#include "opencv2/ml/ml.hpp" +#include "opencv2/highgui/highgui_c.h" +#include "opencv2/core/utility.hpp" + +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"); +Mat final = imread("C:\\Users\\RMD\\desktop\\123456.png"); +bool check = true; +int startx = 0, starty = 0; +bool check2 = true; +bool check3 = true; +Mat tempo; +using namespace cv; +using namespace std; +void on_mouse(int event, int x, int y, int flags, void*) +{ + + + switch (event) { + case EVENT_LBUTTONDOWN: + if (check == true) + check = false; + else + check = true; + if (check == true) { + cout << "EVENT_LBUTTONDOWN: " << startx << ", " << starty << "currenx : " << x << "currenty:" << y << endl; + line(img, Point(startx, starty), Point(x, y), Scalar(0, 255, 255), 2); + line(final, Point(startx, starty), Point(x, y), Scalar(100, 100, 100), 2); + imshow("final", img); + imshow("final2", final); + check3 = false; + } + else { + startx = x; + starty = y; + } + //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; + else + check2 = true; + if (check2 == true) { + cout << "EVENT_LBUTTONDOWN: " << startx << ", " << starty << "currenx : " << x << "currenty:" << y << endl; + circle(img, Point(x, y), 30, Scalar(0, 255, 255), 2); + putText(img, "Exit", Point(x - 13, y + 6), 2, 0.5, Scalar(0, 0, 255), 1); + circle(final, Point(x, y), 30, Scalar(100, 100, 100), 2); + putText(final, "Exit", Point(x - 13, y + 6), 2, 0.5, Scalar(100, 100, 100), 1); + + //circle(final, Point(startx, starty), Point(x, y), Scalar(100, 100, 100), 2); + imshow("final", img); + imshow("final2", final); + + } + else { + circle(img, Point(x, y), 30, Scalar(0, 255, 255), 2); + putText(img, "Entrance", Point(x - 17, y + 6), 2, 0.3, Scalar(0, 0, 255), 1); + circle(final, Point(x, y), 30, Scalar(100, 100, 100), 2); + putText(final, "Entrance", Point(x - 17, y + 6), 2, 0.3, Scalar(100, 100, 100), 1); + + //circle(final, Point(startx, starty), Point(x, y), Scalar(100, 100, 100), 2); + imshow("final", img); + imshow("final2", final); + } + break; + default: + break; + } +} +#include <iostream> +#include <cstdlib> +#include <cmath> +static void on_trackbar(int, void*) { + +} +int num1, num2; +void on_mouse(int event, int x, int y, int flags, void*); +int main() +{ + Mat1b img_gray; + Mat1b templ_gray; + Mat1b templ_gray2; + cvtColor(img, img_gray, COLOR_BGR2GRAY); + cvtColor(templ, templ_gray, COLOR_BGR2GRAY); + cvtColor(templ2, templ_gray2, COLOR_BGR2GRAY); + final = ~final; + Mat1f result; + Mat1f result2; + + namedWindow("final"); + setTrackbarPos("low threshold", "final", 50); + + imshow("final", img); + imshow("final2", final); + + int test1[500] = { NULL }; + int test2[500] = { NULL }; + double thresh = 0.545; + while (1) { + Mat1f result; + Mat1f result2; + + matchTemplate(img, templ, result, TM_CCOEFF_NORMED); + matchTemplate(img, templ2, result2, TM_CCOEFF_NORMED); + createTrackbar("Set !", "final", 0, 1000, on_trackbar); + waitKey(0); + + putText(img, "Please wait..", Point(50, 50), 2, 0.4, Scalar(0, 0, 255), 1); + imshow("final", img); + + img = imread("C:\\Users\\RMD\\desktop\\1233.png"); + + thresh = (double)(getTrackbarPos("Set !", "final")) / 100; + check3 = true; + 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); + printf("thresh: %f", thresh); + Mat1b resb, resb2; + result.convertTo(resb, CV_8U, 255); + result2.convertTo(resb2, CV_8U, 255); + + + vector<vector<Point>> contours; + vector<vector<Point>> contours2; + + 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; + for (int i = 0; i < contours.size(); ++i) + { + Mat1b mask(result.rows, result.cols, uchar(0)); + drawContours(mask, contours, i, Scalar(255), CV_FILLED); + + Point max_point; + double max_val; + minMaxLoc(result, NULL, &max_val, NULL, &max_point, mask); + + + int check; + if (templ2.cols > templ2.rows) + check = templ.rows; + else + check = templ.cols; + + bool ch = true; + test1[i - 1] = max_point.x; + test2[i - 1] = max_point.y; + int j = 0; + 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) { + ch = false; + break; + } + j++; + } + + if (ch == true) { + 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 t1[100] = { NULL }, t2[100] = { NULL }; + for (int i = 0; i < contours2.size(); ++i) + { + Mat1b mask(result2.rows, result2.cols, uchar(0)); + drawContours(mask, contours2, i, Scalar(255), CV_FILLED); + + Point max_point; + double max_val; + minMaxLoc(result2, NULL, &max_val, NULL, &max_point, mask); + + int check; + if (templ2.cols > templ2.rows) + check = templ2.rows; + else + check = templ2.cols; + + bool ch=true; + t1[i - 1] = max_point.x; + t2[i - 1] = max_point.y; + int j = 0; + 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) { + ch = false; + break; + } + j++; + } + + if (ch==true) { + 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); + imshow("final", img); + imshow("final2", final); + printf("adsf"); + printf("!!"); + putText(img, "Finish", Point(30, 70), 2, 0.4, Scalar(0, 0, 255), 1); + + imshow("final", img);// + } + waitKey(0); + + + return 0; +} \ No newline at end of file