65,183
社区成员




int main(int argc, char* const argv[]) {
// opt parse
struct ColorHistOptions colorHistOptions = get_option(argc , argv);
std::uint16_t* hist = new std::uint16_t[hash_length]();
switch(colorHistOptions.command){
case MAKELIB:{//lib
std::ofstream binfile_w("./hist.bin", std::ios::out|std::ios::binary);
std::ofstream idxfile_w("./hist.idx", std::ios::out|std::ios::binary);
glob_t buf;
const char * glob_pattern = strcat(colorHistOptions.lib, "/*");
glob(glob_pattern, GLOB_NOSORT, NULL, &buf);
uint i=0;
for(; i < buf.gl_pathc; i++){
try{
idxfile_w << buf.gl_pathv[i] << std::endl;
image_color_hist(buf.gl_pathv[i], size, hist);
binfile_w.write((char *) hist, hash_length*2);
}
catch(...){
printf("buf.gl_pathv[%d]= %s ERROR \n", i, (buf.gl_pathv[i]));
}
}
printf("end glob\n");
globfree(&buf);
printf("free glob\n");
binfile_w.close();
idxfile_w.close();
printf("file close\n");
break;
}
case COSDIST:{//cos
printf("cos\n");
double cos = 0.0;
double score = 0.0;
std::uint16_t* sample_hist = new std::uint16_t[hash_length]();
image_color_hist(colorHistOptions.sample, size, sample_hist);
struct stat statbuf;
if (stat("./hist.bin", &statbuf) == -1) {
/* check the value of errno */
}
int size = (int) statbuf.st_size;
std::uint16_t value = 0;
std::ifstream binfile_r("./hist.bin", std::ios::in|std::ios::binary);
char * buffer = new char[2];
for (int j=0; j<(size/hash_length/2); j++){
for (char i=0; i<hash_length; i++){
binfile_r.read(buffer, 2);
value = char2short(buffer);
hist[i] = value;
}
cos = cosDistance(sample_hist, hist);
score = std::max(score, cos);
}
binfile_r.close();
printf("%0.2f", score);
/*
if (score >= 0.9){//append lib
std::ofstream binfile_w("./hist.bin", std::ios::out|std::ios::binary|std::ios::app);
binfile_w.write((char *) sample_hist, hash_length*2);
binfile_w.close();
}
*/
break;
}
case HELP:
default:{
printf("help\n");
std::printf("cosWithLib -c buildLib -l <lib>\n");
std::printf("cosWithLib -c cosDist -s <sample>\n");
std::printf("cosWithLib -c help\n");
break;
}
}
printf("end switch\n");
return 0;
}
end glob
free glob
file close
end switch
Segmentation fault: 11
std::uint16_t* hist = new std::uint16_t[hash_length]();
这个没释放
gucci@MacBookAir:colorHist $ bash build.sh
main.cpp:38:19: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
char * sample = (char * ) "";
^
main.cpp:39:16: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
char * lib = (char * )"";
^
main.cpp:40:17: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
int command = HELP;
^
main.cpp:41:15: warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]
bool help = true;
^
4 warnings generated.
gucci@MacBookAir:colorHist $ ./colorHist
end glob
free glob
file close
end switch
gucci@MacBookAir:colorHist $ echo $?
0
gucci@MacBookAir:colorHist $ ./colorHist
end glob
free glob
file close
end switch
Segmentation fault: 11
gucci@MacBookAir:colorHist $ ./colorHist
colorHist(17823,0x7fff9eb853c0) malloc: *** error for object 0x7f912d6040e8: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
gucci@MacBookAir:colorHist $
image_color_hist(buf.gl_pathv[i], size, hist);
把这行注释了就不会出段错误,对应的函数为
void image_color_hist(char * file_path, uchar const size, std::uint16_t* hist){
cv::Mat image = cv::imread(file_path);
cv::resize(image, image, cv::Size(size, size), cv::INTER_CUBIC);
//cv::imwrite(strcat(file_path, ".cpp.png"), image);
int channels = image.channels();
if (channels == 1){
cv::cvtColor(image, image, cv::COLOR_GRAY2BGR);
}
//cv::divide(image, range, image);//not correct for unkown reason
//image = image/range; //not correct for unkown reason
uchar b,g,r;
for(uchar x=0; x<size; x++){
for(uchar y=0; y<size; y++){
b = image.at<cv::Vec3b>(x,y)[0]/range;
g = image.at<cv::Vec3b>(x,y)[1]/range;
r = image.at<cv::Vec3b>(x,y)[2]/range;
//printf("%d %d %d %d %d %d\n" , x, y, b, g, r, r*base*base+g*base+b);
hist[r*base*base+g*base+b] += 1;
}
}
}