-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetection.cpp
84 lines (67 loc) · 2.82 KB
/
detection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "detection.h"
Detection::Detection(const cv::Mat img)
{
image = img;
}
void Detection::findContours()
{
/* Find contours and extract objects by checking area of contours
objects are extracted from original image and stored in an array*/
cv::Mat out;
int x = 0;
cv::Rect r;
std::vector<cv::Mat> objimgs;
std::vector<cv::Vec4i> hiearchy;
std::vector<cv::Point> approxCurve;
std::vector<std::vector<cv::Point>> contours;
cv::Mat drawing = cv::Mat::zeros(image.rows, image.cols, image.type());
cv::Canny(image, out, 100, 200);
cv::findContours(out, contours, hiearchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);
for(size_t i=0; i<contours.size();i++)
{
cv::approxPolyDP(contours[i], approxCurve, 0.1*cv::arcLength(contours[i], true), true);
r = cv::boundingRect(contours[i]);
if(r.area() > 10450 &&(std::abs(r.x-x)>40))
{
objimgs.push_back(image(r));
x = r.x;
}
cv::drawContours(drawing, contours, int(i), cv::Scalar(0,0,255), 1, cv::LINE_8, cv::Mat());
}
detectedImages = objimgs;
}
void Detection::compareObjects()
{
/* Stored object images are compared based on their histogram calculations
All methods are applied on image pairs while comparing histograms*/
cv::Mat hsv1, hsv2;
cv::Mat hist_hsv1, hist_hsv2;
int histSize[] = {50, 50};
float h_ranges[] = {0, 180};
float s_ranges[] = {0, 256};
const float* ranges[] = {h_ranges, s_ranges};
int channels[] = {0, 1};
for(size_t i=0;i<detectedImages.size();i++)
{
cv::cvtColor(detectedImages[i], hsv1, cv::COLOR_BGR2HSV);
cv::calcHist(&hsv1, 1, channels, cv::Mat(), hist_hsv1, 2, histSize, ranges, true, false);
cv::normalize(hist_hsv1, hist_hsv1, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
for(size_t j=0;j<detectedImages.size();j++)
{
if(j==i)
continue;
cv::cvtColor(detectedImages[j], hsv2, cv::COLOR_BGR2HSV);
cv::calcHist(&hsv2, 1, channels, cv::Mat(), hist_hsv2, 2, histSize, ranges, true, false);
cv::normalize(hist_hsv2, hist_hsv2, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
double comparison = cv::compareHist(hist_hsv1, hist_hsv2, 0);
double comparison1 = cv::compareHist(hist_hsv1, hist_hsv2, 1);
double comparison2 = cv::compareHist(hist_hsv1, hist_hsv2, 2);
double comparison3 = cv::compareHist(hist_hsv1, hist_hsv2, 3);
std::cout <<"comp1: " << comparison <<" comp2: " <<comparison1 << " comp3: "<<comparison2<<" comp4: " <<comparison3<< std::endl;
cv::imshow("img1", detectedImages[i]);
cv::imshow("img2", detectedImages[j]);
cv::waitKey(0);
cv::destroyAllWindows();
}
}
}