Skip to content
Snippets Groups Projects
Commit 2dc92729 authored by rohmindo's avatar rohmindo
Browse files

label

parent a4ce7e93
Branches master
No related tags found
No related merge requests found
...@@ -37,13 +37,15 @@ ...@@ -37,13 +37,15 @@
using namespace cv; using namespace cv;
using namespace std; using namespace std;
using namespace cv;
using namespace std;
using namespace cv; using namespace cv;
using namespace std; using namespace std;
Mat3b img = imread("C:\\Users\\RMD\\desktop\\1233.png"); Mat3b img = imread("C:\\Users\\RMD\\desktop\\1233.png");
Mat3b templ = imread("C:\\Users\\RMD\\desktop\\t1.png"); Mat3b templ = imread("C:\\Users\\RMD\\desktop\\t1.png");
Mat3b templ2 = imread("C:\\Users\\RMD\\desktop\\t2.png"); Mat3b templ2 = imread("C:\\Users\\RMD\\desktop\\t2.png");
Mat3b templ3 = imread("C:\\Users\\RMD\\desktop\\123456.png"); Mat3b templ3 = imread("C:\\Users\\RMD\\desktop\\123456.png");
Mat3b templ4 = 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"); Mat final = imread("C:\\Users\\RMD\\desktop\\123456.png");
#include "opencv2/features2d.hpp" #include "opencv2/features2d.hpp"
#include "opencv2/features2d/features2d.hpp" #include "opencv2/features2d/features2d.hpp"
...@@ -53,9 +55,23 @@ struct rectanglesave { ...@@ -53,9 +55,23 @@ struct rectanglesave {
int ypos; int ypos;
int rowsize; int rowsize;
int colsize; int colsize;
int xposl;
int yposl;
int xposr;
int yposr;
int label;
double width;
double height;
double centerxpos;
double centerypos;
bool status; bool status;
}; };
static int labelcount = 0;
rectanglesave save[500] = { NULL }; rectanglesave save[500] = { NULL };
double lefttopxpos;
double lefttopypos;
double width;
double height;
using namespace cv; using namespace cv;
using namespace std; using namespace std;
bool check = true; bool check = true;
...@@ -87,6 +103,8 @@ void on_mouse(int event, int x, int y, int flags, void*) ...@@ -87,6 +103,8 @@ void on_mouse(int event, int x, int y, int flags, void*)
i++; i++;
} }
printf("xpos: %d, ypos: %d, col: %d, cols: %d\n", save[index].xpos, save[index].ypos, save[index].colsize, save[index].rowsize); 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(img, Rect(save[index].xpos, save[index].ypos, save[index].colsize, save[index].rowsize), Scalar(0, 0, 255), 1);
if (save[index].status == true) 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); rectangle(final, Rect(save[index].xpos, save[index].ypos, save[index].colsize - 4, save[index].rowsize - 4), Scalar(0, 0, 0), 1);
...@@ -148,6 +166,32 @@ void on_mouse(int event, int x, int y, int flags, void*) ...@@ -148,6 +166,32 @@ void on_mouse(int event, int x, int y, int flags, void*)
break; break;
} }
} }
void on_mouse2(int event, int x, int y, int flags, void*)
{
double min = 100;
int index;
int i = 0;
switch (event) {
case EVENT_LBUTTONUP:
//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("\nCentorxpos percent: %lf%%\nCentorypos percent: %lf%%\nWidth percent : %lf%%\nHeight percent : %lf%%\n\n", save[index].centerxpos, save[index].centerypos,save[index].width,save[index].height);
break;
default:
break;
}
}
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
...@@ -169,7 +213,7 @@ int main() ...@@ -169,7 +213,7 @@ int main()
cvtColor(templ, templ_gray, COLOR_BGR2GRAY); cvtColor(templ, templ_gray, COLOR_BGR2GRAY);
cvtColor(templ2, templ_gray2, COLOR_BGR2GRAY); cvtColor(templ2, templ_gray2, COLOR_BGR2GRAY);
cvtColor(templ3, templ_gray3, COLOR_BGR2GRAY); cvtColor(templ3, templ_gray3, COLOR_BGR2GRAY);
cvtColor(templ4, templ_gray4, COLOR_BGR2GRAY); //cvtColor(templ4, templ_gray4, COLOR_BGR2GRAY);
final = ~final; final = ~final;
Mat1f result; Mat1f result;
Mat1f result2; Mat1f result2;
...@@ -196,7 +240,7 @@ int main() ...@@ -196,7 +240,7 @@ int main()
matchTemplate(img, templ, result, TM_CCOEFF_NORMED); matchTemplate(img, templ, result, TM_CCOEFF_NORMED);
matchTemplate(img, templ2, result2, TM_CCOEFF_NORMED); matchTemplate(img, templ2, result2, TM_CCOEFF_NORMED);
matchTemplate(img, templ3, result3, TM_CCOEFF_NORMED); matchTemplate(img, templ3, result3, TM_CCOEFF_NORMED);
matchTemplate(img, templ4, result4, TM_CCOEFF_NORMED); //matchTemplate(img, templ4, result4, TM_CCOEFF_NORMED);
createTrackbar("Set !", "final", 0, 1000, on_trackbar); createTrackbar("Set !", "final", 0, 1000, on_trackbar);
waitKey(0); waitKey(0);
...@@ -214,7 +258,7 @@ int main() ...@@ -214,7 +258,7 @@ int main()
threshold(result, result, thresh, 1., THRESH_BINARY); threshold(result, result, thresh, 1., THRESH_BINARY);
threshold(result2, result2, thresh, 1., THRESH_BINARY); threshold(result2, result2, thresh, 1., THRESH_BINARY);
threshold(result3, result3, thresh, 1., THRESH_BINARY); threshold(result3, result3, thresh, 1., THRESH_BINARY);
threshold(result4, result4, thresh, 1., THRESH_BINARY); //threshold(result4, result4, thresh, 1., THRESH_BINARY);
printf("thresh: %f", thresh); printf("thresh: %f", thresh);
Mat1b resb, resb2,resb3,resb4; Mat1b resb, resb2,resb3,resb4;
result.convertTo(resb, CV_8U, 255); result.convertTo(resb, CV_8U, 255);
...@@ -231,7 +275,7 @@ int main() ...@@ -231,7 +275,7 @@ int main()
findContours(resb, contours, RETR_LIST, CHAIN_APPROX_SIMPLE); findContours(resb, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
findContours(resb2, contours2, RETR_LIST, CHAIN_APPROX_SIMPLE); findContours(resb2, contours2, RETR_LIST, CHAIN_APPROX_SIMPLE);
findContours(resb3, contours3, RETR_LIST, CHAIN_APPROX_SIMPLE); findContours(resb3, contours3, RETR_LIST, CHAIN_APPROX_SIMPLE);
findContours(resb4, contours4, RETR_LIST, CHAIN_APPROX_SIMPLE); //findContours(resb4, contours4, RETR_LIST, CHAIN_APPROX_SIMPLE);
//printf("contoursize: %d", contours.size()); //printf("contoursize: %d", contours.size());
int num111 = -100, num222 = -100; int num111 = -100, num222 = -100;
...@@ -273,8 +317,11 @@ int main() ...@@ -273,8 +317,11 @@ int main()
} }
else 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, save[count].status = true;; 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, save[count].xposl = max_point.x ,save[count].yposl=max_point.y,save[count].xposr= templ.cols - 4,save[count].yposr=templ.rows-4,save[count].width=templ.cols-4,save[count].height=templ.rows-4;
count++; count++;
save[count].label = ++labelcount;
string str = to_string(save[count].label);
putText(final, str, Point(max_point.x+(templ.cols-4)/2-2, max_point.y+(templ2.rows-4)/2), 2, 0.2, Scalar(0, 0, 255), 1);
rectangle(final, Rect(max_point.x, max_point.y, templ.cols - 4, templ.rows - 4), Scalar(100, 100, 100), 1); 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); rectangle(img, Rect(max_point.x, max_point.y, templ.cols, templ.rows), Scalar(0, 255, 0), 1);
...@@ -326,10 +373,15 @@ int main() ...@@ -326,10 +373,15 @@ int main()
} }
else 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, save[count].status = false; 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, save[count].xposl = max_point.x-10, save[count].yposl = max_point.y-6,save[count].xposr=templ2.cols-4,save[count].yposr=templ2.rows-4,save[count].width=templ2.cols-4,save[count].height=templ2.rows-4;
count++; count++;
save[count].label = ++labelcount;
string str = to_string(save[count].label);
putText(final, str, Point(max_point.x-10, (max_point.y -6)+(templ2.rows-4)/2), 2, 0.2, Scalar(0, 0, 255), 1);
rectangle(final, Rect(max_point.x - 10, max_point.y - 6, 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); rectangle(img, Rect(max_point.x, max_point.y, templ2.cols, templ2.rows), Scalar(0, 255, 0), 1);
//printf("xposl : %d xposr : %d yposl : %d yposr: %d\n", max_point.x-10, max_point.y-6, templ2.cols-4, templ2.rows-4);
} }
num111 = num11; num111 = num11;
...@@ -352,17 +404,22 @@ int main() ...@@ -352,17 +404,22 @@ int main()
minMaxLoc(result3, NULL, &max_val, NULL, &max_point, mask); 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; 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++; count++;
rectangle(final, Rect(max_point.x, max_point.y , templ3.cols - 4, templ3.rows - 4), Scalar(100, 100, 100), 1); rectangle(final, Rect(max_point.x+190, max_point.y+90 , templ3.cols - 270, templ3.rows - 170), Scalar(100, 100, 100), 1);
rectangle(img, Rect(max_point.x, max_point.y, templ3.cols, templ3.rows), Scalar(0, 255, 0), 1); rectangle(img, Rect(max_point.x+190, max_point.y+90, templ3.cols-270, templ3.rows-170), Scalar(0, 255, 0), 1);
width =(templ3.cols - 270);
height =(templ3.rows - 170);
lefttopxpos = max_point.x +190;
lefttopypos = max_point.y +90;
num111 = num11; num111 = num11;
num222 = num22; num222 = num22;
num11 = max_point.x; num11 = max_point.x;
num22 = max_point.y; num22 = max_point.y;
}num111 = -100, num222 = -100; }
/*
num111 = -100, num222 = -100;
num11 = -100, num22 = -100; num11 = -100, num22 = -100;
int t5[100] = { NULL }, t6[100] = { NULL }; int t5[100] = { NULL }, t6[100] = { NULL };
for (int i = 0; i < contours4.size(); ++i) for (int i = 0; i < contours4.size(); ++i)
...@@ -385,12 +442,30 @@ int main() ...@@ -385,12 +442,30 @@ int main()
num11 = max_point.x; num11 = max_point.x;
num22 = max_point.y; num22 = max_point.y;
}*/
//save[i].centerxpos = (save[i].xpos - lefttopxpos) / width;
//save[i].centerypos = (save[i].ypos - lefttopypos) / height;
//printf("x : %lf y : %lf \n", save[i].centerxpos, save[i].centerypos);
int e = 0;
while (save[e].xpos!=NULL) {
double cenx = (double)(save[e].xposl + save[e].xposr/2) ;
double ceny = (double)(save[e].yposl + save[e].yposr/2) ;
save[e].centerxpos = (((double)cenx - lefttopxpos) / (double)width)*100;
save[e].centerypos = (((double)ceny - lefttopypos) / (double)height)*100;
save[e].width = (save[e].width / width)*100;
save[e].height = (save[e].height / height)*100;
save[e].label += 1;
printf("\nlabel: %d\nCenterXpos percent: %lf%%\nCenterYpos percent: %lf%%\nWidth percent: %lf%%\nHeightpercent: %lf%%\n", save[e].label, save[e].centerxpos, save[e].centerypos, save[e].width, save[e].height);
e++;
} }
Mat mask = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2, 2), cv::Point(1, 1)); Mat mask = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2, 2), cv::Point(1, 1));
setMouseCallback("final", on_mouse); setMouseCallback("final", on_mouse);
namedWindow("final2"); namedWindow("final2");
setMouseCallback("final2", on_mouse); setMouseCallback("final2", on_mouse2);
imshow("final", img); imshow("final", img);
imshow("final2", final); imshow("final2", final);
printf("adsf"); printf("adsf");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment