/
queryimage.cpp
103 lines (93 loc) · 3.61 KB
/
queryimage.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "queryimage.h"
#include "debugHeader.h"
#include <iostream>
/**
* @brief QueryImage::QueryImage Costruttore della classe che si occupa di generare le feature per la query image
* @param filename Nome della query image
*/
QueryImage::QueryImage(string filename, bool computeSIFT): image(filename), imgName(filename) {
IplImage iplimg = *(image.getImage());
vector<vector<Pixel> > superPixels;
segmentImage(&iplimg, superPixels);
cv::Mat segmented(image.getImage()->size(), CV_8UC1);
int nSp = superPixels.size();
for(int i=0; i<nSp; ++i){
superPixelList.push_back(new SuperPixel(superPixels[i], *image.getImage(), computeSIFT));
for(uint j=0; j<superPixels[i].size(); ++j){
assert(superPixels[i][j].y<segmented.rows);
assert(superPixels[i][j].x<segmented.cols);
segmented.at<uchar>(superPixels[i][j].y, superPixels[i][j].x) = (uchar)255*i/nSp;
}
}
SuperPixel::computeAdiacents(superPixelList, image.getImage()->rows, image.getImage()->cols);
SuperPixel::computeWeight(superPixelList);
cvNamedWindow("superPixels",2);
imshow("superPixels",segmented);
}
QueryImage::~QueryImage(){
for(int i=0; i<superPixelList.size(); ++i){
delete superPixelList[i];
}
}
/**
* @brief QueryImage::getSuperPixels Metodo per ottenere il vettore di superpixel della queryimage
*/
vector<SuperPixel *> *QueryImage::getSuperPixels(){
return &superPixelList;
}
/**
* @brief QueryImage::buildObtainedMat Build the mat representing the obtained labeling
* @param result Mat in which the function will print the result
*/
void QueryImage::buildObtainedMat(cv::Mat &result, set<int> *usedLabels){
result.create(image.getImage()->size(), CV_8UC1);
result.setTo(cv::Scalar::all(0));
if(usedLabels!=NULL) usedLabels->clear();
for(uint i=0; i<superPixelList.size(); ++i){
if(usedLabels!=NULL) usedLabels->insert(superPixelList[i]->getLabel());
superPixelList[i]->printToMat(result);
}
}
/**
* @brief QueryImage::showLabeling Show the labeling made to the current image
*/
void QueryImage::showLabeling(string title){
cv::Mat labeledImage, toVisualize;
set<int> usedLabels;
buildObtainedMat(labeledImage, &usedLabels);
VisualUtils::colorLabels(labeledImage, toVisualize, usedLabels);
cvNamedWindow(title.c_str(),CV_WINDOW_AUTOSIZE);
imshow(title.c_str(),toVisualize);
showOriginalLabeling();
}
void QueryImage::showOriginalLabeling(){
cv::Mat toVisualize;
set<int> usedLabels;
for(int i=-1; i<34; ++i)
usedLabels.insert(i);
GeoLabel correctLabeling(imgName);
cv::Mat lbl;
correctLabeling.getLabeledImg()->convertTo(lbl,CV_8UC1);
VisualUtils::colorLabels(lbl, toVisualize, usedLabels);
cvNamedWindow("ORIGINAL LABELS",CV_WINDOW_AUTOSIZE);
imshow("ORIGINAL LABELS",toVisualize);
}
void QueryImage::showSrc(){
image.show();
}
double QueryImage::checkResults(){
cv::Mat resultingImg;
cv::Mat difference(image.getImage()->size(), CV_8UC1, cv::Scalar::all(0));
buildObtainedMat(resultingImg);
GeoLabel correctLabeling(imgName);
//Check that the two Mat have the same size
assert(correctLabeling.getLabeledImg()->size() == resultingImg.size());
int diff = 0;
for(int x=0; x<resultingImg.cols; ++x){ for(int y=0; y<resultingImg.rows; ++y){
if(resultingImg.at<uchar>(y,x)!=(uchar)correctLabeling.getLabeledImg()->at<short>(y,x)) ++diff;
else difference.at<uchar>(y,x) = 255;
}}
cvNamedWindow("DIFFERENCE", 2);
imshow("DIFFERENCE", difference);
return diff/(double)(resultingImg.rows*resultingImg.cols);
}