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); }
//readimage Function void readImage(string fname, ImageType& image){ int i, j; int N, M, Q; unsigned char *charImage; char header [100], *ptr; ifstream ifp; ifp.open(fname.c_str(), ios::in | ios::binary); if (!ifp) { cout << "Can't read image: " << fname << endl; exit(1); } // read header ifp.getline(header,100,'\n'); if((header[0]!=80) || (header[1]!=53) ) { cout << "Image " << fname << " is not PGM" << endl; exit(1); } ifp.getline(header,100,'\n'); while(header[0]=='#') ifp.getline(header,100,'\n'); M=strtol(header,&ptr,0); N=atoi(ptr); ifp.getline(header,100,'\n'); Q=strtol(header,&ptr,0); cout<<Q<<endl; charImage = (unsigned char *) new unsigned char [M*N]; ifp.read( reinterpret_cast<char *>(charImage), (M*N)*sizeof(unsigned char)); if (ifp.fail()) { exit(1); } ifp.close(); int val; for(i=0; i<N; i++) { for(j=0; j<M; j++) { val = (int)charImage[i*M+j]; image.setPixelVal(i, j, val); } } }
/* 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); }
/* 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); }
void readImage(char fname[], ImageType& image) { int i, j; int N, M, Q; unsigned char *charImage; char header [100], *ptr; ifstream ifp; ifp.open(fname, ios::in | ios::binary); if (!ifp) { cout << "Can't read image: " << fname << endl; exit(1); } // read header ifp.getline(header,100,'\n'); if ( (header[0]!=80) || /* 'P' */ (header[1]!=54) ) { /* '6' */ cout << "Image " << fname << " is not PPM" << endl; exit(1); } ifp.getline(header,100,'\n'); while(header[0]=='#') ifp.getline(header,100,'\n'); M=strtol(header,&ptr,0); N=atoi(ptr); ifp.getline(header,100,'\n'); Q=strtol(header,&ptr,0); charImage = (unsigned char *) new unsigned char [3*M*N]; ifp.read( reinterpret_cast<char *>(charImage), (3*M*N)*sizeof(unsigned char)); if (ifp.fail()) { cout << "Image " << fname << " has wrong size" << endl; exit(1); } ifp.close(); /* Convert the unsigned characters to integers */ RGB val; for(i=0; i < N; i++) for(j=0; j < 3*M; j+=3) { val.r = (int)charImage[i*3*M+j]; val.g = (int)charImage[i*3*M+j+1]; val.b = (int)charImage[i*3*M+j+2]; image.setPixelVal(i, j/3, val); } delete [] charImage; }