/** * Method for converting from string to image format. The data in the columns is * separated by "\t" rows are separated by "\n". * * @param data - String that contains the delimited data. * @param depth - depth for the image to be. * @param channels - Numbers of channels in the image. * * @return Returns the pointer to IplImage with the values contained in the string. */ IplImage* LibFaceUtils::stringToImage(const string& data, int depth, int channels) { std::string::size_type start, stop; start = 0; stop = data.find('\n', start); int cols = 0, rows = 0; vector<double> values; //First figure out how many rows and cols there are and store data in vector while (stop != std::string::npos) { string copy(data, start, stop); int extract = 1; cols = 0; while(extract) { std::string::size_type startIndex = copy.find("\t"); string value = copy.substr(0, startIndex); copy = copy.substr(startIndex+1); values.insert(values.end(),atof(value.c_str())); if(copy == "") extract = 0; cols++; } start = stop + 1; stop = data.find('\n', start); rows++; } IplImage* img = cvCreateImage(cvSize(rows, cols), depth, channels); int i, j; //Create matrix and transfer data to the matrix. for(i=0 ; i < rows ; ++i) { for(j=0;j<cols;++j) { cvSetAt(img,cvScalarAll(values.at(cols*i+j)),i,j); } } return img; }
void otsuN(IplImage* img, IplImage* img_seg, int modes, double **thr, double* sep) { int _verbose=1; int xsize = img->width; int ysize = img->height; double* data=new double[xsize * ysize]; for (int i = 0; i < ysize; i++) for (int j = 0; j < xsize; j++) { CvScalar c = cvGet2D(img, i, j); data[i * xsize + j] = c.val[0] / 255.0; } if(_verbose) printf("img: %d %d %d %d\n",xsize,ysize,img->nChannels,img->depth); double* Iseg=new double[xsize * ysize]; otsu(Iseg, thr, sep, data, xsize, ysize, modes); for (int i = 0; i < ysize; i++) for (int j = 0; j < xsize; j++) { CvScalar c; double v = Iseg[i * xsize + j]; c.val[0] = v * 255; c.val[1] = v * 255; c.val[2] = v * 255; cvSetAt(img_seg, c, i, j); } if(_verbose) { double_vector_save_to_file("data.txt", xsize*ysize, data); double_vector_save_to_file("data_out.txt", xsize*ysize, Iseg); double_vector_save_to_file("thr.txt", modes-1, *thr); std::cout << "thr = "; double_vector_print(modes-1, *thr); std::cout<< "Criterio de separabilidad: " << *sep << std::endl; } delete[] Iseg; delete[] data; }