-
Notifications
You must be signed in to change notification settings - Fork 3
/
getLabelledImages.cpp
135 lines (116 loc) · 5 KB
/
getLabelledImages.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*****************************************************************************
* @file trainModel.c
* @version 1.0
* @since 2013-04-24
* @author Jimmy Lin (u5223173) - [email protected]
* @author Christopher Claou'e-Long (u5183532) - [email protected]
******************************************************************************/
// c++ standard headers
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <map>
// eigen matrix library headers
#include "Eigen/Core"
// opencv library headers
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
// darwin library headers
#include "drwnBase.h"
#include "drwnIO.h"
#include "drwnML.h"
#include "drwnVision.h"
#include "features.h"
#include "parseLabel.h"
using namespace std;
using namespace Eigen;
// usage ---------------------------------------------------------------------
// copied from Stephen Gould's trainCOMP3130Model.cpp 2013 version
void usage() {
cerr << DRWN_USAGE_HEADER << endl;
cerr << "USAGE: ./trainModel [OPTIONS] <imgDir> <lblFile>\n";
cerr << "OPTIONS:\n"
<< " -o <model> :: output model\n"
<< " -x :: visualize\n"
<< DRWN_STANDARD_OPTIONS_USAGE
<< endl;
}
// main ----------------------------------------------------------------------
int main (int argc, char * argv[]) {
// what way are we building up a default classifier?
// Set default value for optional command line arguments.
const char *modelFile = NULL;
bool bVisualize = false;
DRWN_BEGIN_CMDLINE_PROCESSING(argc, argv)
DRWN_CMDLINE_STR_OPTION("-o", modelFile)
DRWN_CMDLINE_BOOL_OPTION("-x", bVisualize)
DRWN_END_CMDLINE_PROCESSING(usage());
// Check for the correct number of required arguments
if (DRWN_CMDLINE_ARGC != 3) {
usage();
return -1;
}
/* Check that the image directory and labels directory exist. All
* images with a ".jpg" extension will be used for training the
* model. It is assumed that the labels directory contains files
* with the same base as the image directory, but with extension
* ".txt".
*/
const char *imgDir = DRWN_CMDLINE_ARGV[0];
const char *lblFile = DRWN_CMDLINE_ARGV[1];
const char *outputDir = DRWN_CMDLINE_ARGV[2];
//DRWN_ASSERT_MSG(drwnDirExists(imgDir), "image directory " << imgDir << " does not exist");
// second argument is not directory any more,
// it's a single text file with multiple rectangle
//DRWN_ASSERT_MSG(drwnDirExists(lblFile), "labels directory " << lblFile << " does not exist");
// Get a list of images from the image directory.
vector<string> baseNames = drwnDirectoryListing(imgDir, ".jpg", false, false);
DRWN_LOG_MESSAGE("Loading " << baseNames.size() << " images and labels...");
/* Build a dataset by loading images and labels. For each image,
find the salient area using the labels and then compute the set of features
that determine this saliency. Compute the values for the rest of the image
as well (maybe as superpixels??? unsure.
*/
drwnClassifierDataset dataset;
// MAP FROM FILENAME TO RECTANGLE
map< string, vector<int> > fileLabelPairs = parseLabel(lblFile);
int left, top, right, bottom;
vector<int> tempRectangle;
for (unsigned i = 0; i < baseNames.size(); i++) {
String processedImage = baseNames[i] + ".jpg";
DRWN_LOG_STATUS("...processing image " << baseNames[i]);
// read the image and draw the rectangle of labels of training data
cv::Mat img = cv::imread(string(imgDir) + DRWN_DIRSEP + processedImage);
tempRectangle = fileLabelPairs.find(processedImage)->second ;
left = tempRectangle [0];
top = tempRectangle [1];
right = tempRectangle [2];
bottom = tempRectangle[3];
// show the image and feature maps
if (bVisualize) { // draw the current image comparison
//drwnDrawRegionBoundaries and drwnShowDebuggingImage use OpenCV 1.0 C API
IplImage cvimg = (IplImage)img;
IplImage *canvas = cvCloneImage(&cvimg);
drwnShowDebuggingImage(canvas, "image", false);
cvReleaseImage(&canvas);
cv::Mat labeledimg = cv::Mat(img);
// draw rectangle .. label
for (int l = 0; l < 8 ; l += 2) {
cv::rectangle(labeledimg, cv::Point(left-l-1, top-l-1), cv::Point(right+l+1, bottom+l+1), Scalar(255,255,255));
cv::rectangle(labeledimg, cv::Point(left-l, top-l), cv::Point(right+l, bottom+l), Scalar(0,0,0));
}
IplImage pcvimg = (IplImage) labeledimg;
IplImage *present = cvCloneImage(&pcvimg);
drwnShowDebuggingImage(present, "Labels", false);
cvReleaseImage(&present);
cv::imwrite(string(outputDir) + baseNames[i] + ".jpg", labeledimg);
}
}
// Clean up by freeing memory and printing profile information.
cvDestroyAllWindows();
drwnCodeProfiler::print();
return 0;
}