void testSkinRecogWithThreshold(const std::vector<double> &mean, const Matrix &cov, ImageType &image, std::string out){ RGB white(255,255,255); RGB black(0,0,0); int height, width, levels; image.getImageInfo(height,width,levels); RGB val; std::vector<double> pc(2); // pure color double thR, thG; for(int row = 0; row < height; row++){ for(int col = 0; col < width; col++){ image.getPixelVal(row, col, val); pc[0] = val.r/float(val.r+val.g+val.b); pc[1] = val.g/float(val.r+val.g+val.b); thR = exp(-(cov[0][0] * pow((pc[0] - mean[0]),2) + cov[0][1] * (pc[0]- mean[0]))); thG = exp(-(cov[1][0] * (pc[1] - mean[1]) + cov[1][1] * pow((pc[1] - mean[1]),2))); if((thR >= .9 && thG >= 1.0 && thG < 1.2) || (thR <= .8 && thR >= .7 && thG > 1.1)){ image.setPixelVal(row, col, white); } else{ image.setPixelVal(row, col, black); } } } // end outer for loop writeImage(out.c_str(), image); }
/* Expand image function Writen by: Jeremiah Berns Dependincies, image.cpp, image.h Discription: Will accept the shrunken image, the grow size of the image, and then expand the image back to 256x256 */ void expandImage(ImageType oldImage, ImageType& newImage, int growVal, string newImageName) { //Variable decliration int rows, cols, Q, tempValue; //Variable setting oldImage.getImageInfo(rows, cols, Q); for(int i=0;i<rows;i++) { for(int j=0;j<cols;j++) { oldImage.getPixelVal(i,j, tempValue); for(int k=0;k<growVal;k++) { for(int l=0;l<growVal;l++) { newImage.setPixelVal(i*growVal+k,j*growVal+l,tempValue); } } } } writeImage(newImageName, newImage); }
void crop(ImageType& image) { if(imageLoaded(image)) { // initialize to -1 for input validation int ULr = -1, ULc = -1, LRr = -1, LRc = -1; int N, M, Q; int errorCode = 0; // get values image.getImageInfo(N,M,Q); // get inputs cout << "Enter the upper left corner's row: "; cin >> ULr; cout << endl << "Enter the upper left corner's column: "; cin >> ULc; cout << endl << "Enter the lower right corner's row: "; cin >> LRr; cout << endl << "Enter the lower right corner's column: "; cin >> LRc; // check for errors if(ULr < 0 || ULc < 0 || LRr < 0 || LRc < 0) errorCode = 1; else if(ULr > N || LRr > N || ULc > M || LRc > M) errorCode = 2; else if(ULr >= LRr || ULc >= LRc) errorCode = 3; switch(errorCode) { case 1: cout << "ERROR: All inputs must be non-negative."; break; case 2: cout << "ERROR: All crop boundaries must be within image boundaries."; break; case 3: cout << "ERROR: All crop boundaries must be in the correct order."; break; } // crop image if no error was found if(errorCode == 0) { image.getSubImage(ULr, ULc, LRr, LRc, image); cout << endl << endl << "Image has been cropped successfully."; } pressEnterToContinue(); } }
void testSkinRecognition(const BayesianClassifier &classifier, ImageType &image, ImageType &ref, std::string out, bool YCbCr){ int height, width, levels; image.getImageInfo(height,width,levels); ImageType outImg(height,width,levels); RGB val1, val2; int label; std::vector<double> color(2); int TP = 0, TN = 0, FN = 0, FP = 0; RGB white(255,255,255); RGB black(0,0,0); for(int row = 0; row < height; row++){ for(int col = 0; col < width; col++){ image.getPixelVal(row, col, val1); ref.getPixelVal(row, col, val2); if(YCbCr == true){ color[0] = -0.169*val1.r - 0.332*val1.g+ 0.500*val1.b; color[1] = 0.500*val1.r - 0.419*val1.g - 0.081*val1.b; } else{ color[0] = val1.r/float(val1.r+val1.g+val1.b); color[1] = val1.g/float(val1.r+val1.g+val1.b); } label = classifier.predict(color); if(label == 0){ outImg.setPixelVal(row, col, white); if(val2 != black){ TP++; } else{ FP++; } } else{ outImg.setPixelVal(row, col, black); if(val2 == black){ TN++; } else{ FN++; } } } } // end outer for loop std::cout << std::endl << "TP: " << TP << std::endl << "TN: " << TN << std::endl << "FP: " << FP << std::endl << "FN: " << FN << std::endl; /*std::stringstream ss; ss << FP << " " << FN; Debugger debugger("Data_Prog2/errors3a.txt",true); debugger.debug(ss.str()); */ writeImage(out.c_str(), outImg); }
void makeColorMatrices(ImageType& img, ImageType& ref, Matrix &sk_cols, Matrix &nsk_cols, bool YCbCr) { int height1, width1, levels1; int height2, width2, levels2; img.getImageInfo(height1, width1, levels1); ref.getImageInfo(height2, width2, levels2); assert(height1 == height2); assert(width1 == width2); assert(levels1 == levels2); RGB val1, val2; std::vector<double> color(2); RGB black(0,0,0); for(int row = 0; row < height1; row++){ for(int col = 0; col < width1; col++){ img.getPixelVal(row, col, val1); ref.getPixelVal(row, col, val2); if(YCbCr == true){ color[0] = -0.169*val1.r - 0.332*val1.g+ 0.500*val1.b; color[1] = 0.500*val1.r - 0.419*val1.g - 0.081*val1.b; } else{ color[0] = val1.r/float(val1.r+val1.g+val1.b); color[1] = val1.g/float(val1.r+val1.g+val1.b); } if(val2 != black){ sk_cols.push_back(color); } else{ nsk_cols.push_back(color); } } } }
void writeImage(const char fname[], ImageType& image) /* write PPM image */ { int i, j; int N, M, Q; unsigned char *charImage; ofstream ofp; image.getImageInfo(N, M, Q); // make space for PPM charImage = (unsigned char *) new unsigned char [3*M*N]; // convert the RGB to unsigned char RGB val; for(i=0; i<N; i++) { for(j=0; j<3*M; j+=3) { image.getPixelVal(i, j/3, val); charImage[i*3*M+j]=(unsigned char)val.r; charImage[i*3*M+j+1]=(unsigned char)val.g; charImage[i*3*M+j+2]=(unsigned char)val.b; } } ofp.open(fname, ios::out | ios::binary); if (!ofp) { cout << "Can't open file: " << fname << endl; exit(1); } ofp << "P6" << endl; ofp << M << " " << N << endl; ofp << Q << endl; ofp.write( reinterpret_cast<char *>(charImage), (3*M*N)*sizeof(unsigned char)); if (ofp.fail()) { cout << "Can't write image " << fname << endl; exit(0); } ofp.close(); delete [] charImage; }
/* Histogram Equalization function Written by: Jeremiah Berns Dependincies:image.h, image.cpp Discription: This function will perform the histogram equalization algorithem to the oldImage and will output the newImage with the given newImageName. */ void histogramEq(ImageType oldImage, ImageType& newImage, string newImageName) { int rows, cols, Q, pixelValue, pixelCount; oldImage.getImageInfo(rows,cols,Q); pixelCount = rows*cols; int adjustedHistogram[Q]; double histogramArray[Q], equalizedHistogram[Q]; double probabilityArray[Q], cumulativeProbability[Q], probTotal=0; for (int i = 0; i<Q;i++) { histogramArray[i] = 0; equalizedHistogram[i] = 0; } for(int i=0; i<rows;i++) { for(int j=0; j<cols;j++) { oldImage.getPixelVal(i,j,pixelValue); histogramArray[pixelValue]+=1; } } for(int i=0;i<Q;i++) { probTotal+= histogramArray[i]/pixelCount; cumulativeProbability[i] = probTotal; cumulativeProbability[i] = cumulativeProbability[i]*255; adjustedHistogram[i] = cumulativeProbability[i]; cout<<adjustedHistogram[i]<<endl; } for(int i=0; i<rows;i++) { for(int j=0; j<cols;j++) { oldImage.getPixelVal(i,j,pixelValue); newImage.setPixelVal(i,j,adjustedHistogram[pixelValue-1]); } } writeImage(newImageName, newImage); }
void displayInfo(ImageType& image) { if(imageLoaded(image)) { int N, M, Q; // get values image.getImageInfo(N,M,Q); cout << "Height : " << N << endl << "Width : " << M << endl << "Max Pixel Value : " << Q << endl << "Mean Gray Value : " << image.meanGray(); pressEnterToContinue(); } }
void writeImage(string fname, ImageType& image){ int i, j; int N, M, Q; unsigned char *charImage; ofstream ofp; image.getImageInfo(N, M, Q); charImage = (unsigned char *) new unsigned char [M*N]; // convert the integer values to unsigned char int val; for(i=0; i<N; i++){ for(j=0; j<M; j++){ image.getPixelVal(i, j, val); charImage[i*M+j]=(unsigned char)val; } } ofp.open(fname.c_str(), ios::out | ios::binary); if (!ofp) { cout << "Can't open file: " << fname << endl; exit(1); } ofp << "P5" << endl; ofp << M << " " << N << endl; ofp << Q << endl; ofp.write( reinterpret_cast<char *>(charImage), (M*N)*sizeof(unsigned char)); if (ofp.fail()) { cout << "Can't write image " << fname << endl; exit(0); } ofp.close(); }
/* shrink Image funtion. Writen By Jeremiah Berns. Dependincies: image.h, image.cpp Discription: Will take in the old image, and the new image, and the pixel value based apon the shrink value passed to it. It will place that value from the old image into the new image, then save the new image with the passed in file name. */ void shrinkImage(ImageType oldImage, ImageType& newImage, int shrinkVal, string newImageFname) { //Variable decliration int rows, col, Q, tempValue; //Variable setting oldImage.getImageInfo(rows, col, Q); for(int i=0; i<rows;i++) { for(int j=0;j<col;j++) { if(i%shrinkVal == 0 && j%shrinkVal ==0) { oldImage.getPixelVal(i,j, tempValue); newImage.setPixelVal(i/shrinkVal,j/shrinkVal,tempValue); } } } writeImage(newImageFname, newImage); }