// function loads input image, calls processing function, and saves result void Dip2::run(void){ // load images as grayscale cout << "load images" << endl; Mat noise1 = imread("noiseType_1.jpg", 0); if (!noise1.data){ cerr << "noiseType_1.jpg not found" << endl; cout << "Press enter to exit" << endl; cin.get(); exit(-3); } noise1.convertTo(noise1, CV_32FC1); Mat noise2 = imread("noiseType_2.jpg",0); if (!noise2.data){ cerr << "noiseType_2.jpg not found" << endl; cout << "Press enter to exit" << endl; cin.get(); exit(-3); } noise2.convertTo(noise2, CV_32FC1); cout << "done" << endl; // apply noise reduction // TO DO !!! // ==> Choose appropriate noise reduction technique with appropriate parameters // ==> "average" or "median"? Why? // ==> try also "adaptive" (and if implemented "bilateral") cout << "reduce noise" << endl; Mat restorated1 = noiseReduction(noise1, "median", 5); Mat restorated2 = noiseReduction(noise2, "adaptive", 9, 40); cout << "done" << endl; // save images cout << "save results" << endl; imwrite("restorated1.jpg", restorated1); imwrite("restorated2.jpg", restorated2); cout << "done" << endl; }
//-------------------------------------------------------------- void testApp::draw_squares(){ depthImage.draw(0,0,depthValues.getWidth(),depthValues.getHeight()); noiseReduction(); for (int i =0; i < depthValues.getWidth(); i++) { for(int j=0; j<depthValues.getHeight(); j++) { //cout<<" the value of time outside loop is"<<time(NULL); if (time(NULL) - previousDepth[i][j].initialTime>0.5 && previousDepth[i][j].changed == true ) { previousDepth[i][j].initialTime = 0; //cout<<" the value of current time is "<<time(NULL); //cout<<" the value of previousDepthTime is "<<previousDepth[i][j].initialTime; previousDepth[i][j].changed = false; depthImage.setColor(i, j, depthValues.getPixelColor(i, j)); } if ((depthValues.getPixelDepth(i, j) - previousDepth[i][j].depth > 1000) || (depthValues.getPixelDepth(i, j) - previousDepth[i][j].depth < -1000)) { depthImage.setColor(i, j, ofColor::red); previousDepth[i][j].changed = true; previousDepth[i][j].initialTime = time(NULL); } else { if (previousDepth[i][j].changed == false) { depthImage.setColor(i,j,depthValues.getPixelColor(i, j)); //noiseReduction(); } } previousDepth[i][j].depth = depthValues.getPixelDepth(i, j); } } }
int scanAndReadUPC(uint8_t *imgAddress, size_t imgHeight, size_t imgWidth, char *resultCString, int resultMaxLength) { int resCStrLength = 0; unsigned char *imageBytes = imgAddress; int *scanDataArr = malloc(imgHeight*sizeof(int)); for (int jj = 0; jj < imgHeight; jj++) { if (scanDataArr[jj]!=0) { scanDataArr[jj]=0; } } int scanThick = 2; scanHorizLine(scanDataArr, imageBytes, imgWidth, imgHeight, scanThick); drawHorizScanLine(imageBytes, imgWidth, imgHeight, scanThick); drawHorizHistogram(imageBytes, imgWidth, scanDataArr, imgHeight); int *scanDataDerivArr = malloc((imgHeight-1)*sizeof(int)); int noiseThreshold = 150; derivative(scanDataArr, scanDataDerivArr, imgHeight); int *scanDataFilteredArr = malloc((imgHeight-1)*sizeof(int)); noiseReduction(scanDataDerivArr, scanDataFilteredArr, imgHeight, noiseThreshold); free(scanDataDerivArr); // free(scanDataArr); int maxMinCount = concentrateData(scanDataFilteredArr, imgHeight); underivative(scanDataFilteredArr, scanDataArr, imgHeight); drawHorizHistogram(imageBytes, imgWidth, scanDataArr, imgHeight); free(scanDataArr); drawHorizLineHistogram(imageBytes, imgWidth, imgHeight, scanDataFilteredArr); int *extrDataArr = malloc(maxMinCount*sizeof(int)); int *intrDataArr = malloc(maxMinCount*sizeof(int)); extractChangepoints(extrDataArr, scanDataFilteredArr, imgHeight); free(scanDataFilteredArr); changepointsToIntervals(extrDataArr, intrDataArr, maxMinCount); maxMinCount--; //float maxMinThreshold = 4.625; float maxMinThreshold = 4.2; if (maxMinCount>2) { int js; int je; int absMin; int absMax; calcAbsMaxMin(intrDataArr, maxMinCount, &js, &je, &absMin, &absMax, maxMinThreshold); float avgMin = calcAvgMin(absMin, intrDataArr, je, js); int normDataLength = je - js + 1; int *normDataArr = malloc((normDataLength)*sizeof(int)); normalize(avgMin, je, intrDataArr, js, normDataArr); int startLeft = -1; int endLeft = -1; int startRight = -1; int endRight = -1; startLeft = findLeftDataStart(normDataArr, normDataLength); endLeft = findLeftDataEnd(normDataArr, normDataLength, startLeft); if (endLeft+5 < normDataLength) { startRight = endLeft+5; if ((endLeft - startLeft) + startRight < normDataLength) endRight = (endLeft - startLeft) + startRight; } if (startLeft>=0) drawLine(imageBytes, imgWidth, imgHeight, extrDataArr[startLeft], 0, extrDataArr[startLeft], imgWidth); if (endLeft>=0) drawLine(imageBytes, imgWidth, imgHeight, extrDataArr[endLeft], 0, extrDataArr[endLeft], imgWidth); if (startRight>=0) drawLine(imageBytes, imgWidth, imgHeight, extrDataArr[startRight], 0, extrDataArr[startRight], imgWidth); if (endRight>=0) drawLine(imageBytes, imgWidth, imgHeight, extrDataArr[endRight], 0, extrDataArr[endRight], imgWidth); decodeData(&resCStrLength, resultCString, resultMaxLength, normDataArr, endLeft, startLeft); decodeData(&resCStrLength, resultCString, resultMaxLength, normDataArr, endRight, startRight); free(normDataArr); char correctResult; correctResult = checkControlDigit(resultCString, resCStrLength); if (correctResult == 0) { resCStrLength = 0; } } free(extrDataArr); free(intrDataArr); return resCStrLength; }
void testApp::draw() { depthImage.draw(0,0,640,400); frameImage.draw(depthValues.getWidth(),0,640,400); color_image.draw(0,400,640,400); noiseReduction(); //ofDrawBitmapString(msg.str(),900,600); if (imageTaken ==0) { cout<<" how many times is this executed"; color_image.setFromPixels(frameImage.getPixels(), 640, 480, OF_IMAGE_COLOR); snapshot.setFromPixels(frameImage.getPixels(), 640, 480, OF_IMAGE_COLOR); imageTaken = 1; /* for (int a =0; a<640; a=a+1) { for(int d=0; d<480; d=d+1) { color_image.setColor(a, d, color_image.getColor(a, d)/8); colorDarkenedValues[a][d].darkened = true; colorDarkenedValues[a][d].darkened_time = time(NULL); colorDarkenedValues[a][d].firstChange = false; colorDarkenedValues[a][d].secondChange = true; colorDarkenedValues[a][d].thirdChange = true; } }*/ } //int rows = 20 * 4; //int column = 16*4; int width = depthImage.getWidth()/column; int height = depthImage.getHeight()/rows; int sum; int color_sum; //int colorThreshold = 75000/4; //int depthThresholdValue = 6000000/60; // calculate the change in depth values for every square for (int i=0; i <column; i++) { for (int j=0; j<rows; j++) { sum = 0; color_sum = 0; for (int k=i*width; k<(i+1)*width; k++ ) { for (int l = (j*height); l<(j+1)*height; l++) { //cout<<" the value of depth is "<<depthValues.getPixelDepth(k, l); //cout<<"the k l pair is "<<k<<" "<<l; if (rectifiedDepthValues[k][l] - previousDepth[k][l].depth > 0) sum = sum + rectifiedDepthValues[k][l] - previousDepth[k][l].depth; else sum = sum + previousDepth[k][l].depth - rectifiedDepthValues[k][l]; if ((depthValues.getPixelColor(k, l).r + depthValues.getPixelColor(k, l).g + depthValues.getPixelColor(k, l).b) - previousColorSum[k][l] > 0) color_sum = color_sum + depthValues.getPixelColor(k, l).r + depthValues.getPixelColor(k, l).g + depthValues.getPixelColor(k, l).b - previousColorSum[k][l]; else color_sum = color_sum - depthValues.getPixelColor(k, l).r - depthValues.getPixelColor(k, l).g - depthValues.getPixelColor(k, l).b + previousColorSum[k][l]; previousDepth[k][l].depth = rectifiedDepthValues[k][l]; previousColorSum[k][l] = depthValues.getPixelColor(k, l).r + depthValues.getPixelColor(k,l).g + depthValues.getPixelColor(k,l).b; } } //cout<<" the value of sum is "<<sum; //cout<<" the value of colorsum is "<<color_sum; if ((color_sum> colorThreshold && considerColor == true) || (sum>depthThresholdValue && considerDepth == true)) //if (color_sum > 75000/4) //if (sum>6000000/60) //if (sum>6000000/144) { //color that square red for 1 sec for (int m = i*width; m<(i+1)*width; m++) { for(int n=j*height; n<(j+1)*height; n++) { if (previousDepth[m][n].changed == false) { depthImage.setColor(m, n, ofColor::red); previousDepth[m][n].changed = true; previousDepth[m][n].initialTime = time(NULL); //colorDarkenedValues[m][n].darkened = true; if (colorDarkenedValues[m][n].darkened == false) { //color_image.setColor(m, n, color_image.getColor(m, n)/8); color_image.setColor(m, n, snapshot.getColor(m, n)/8); colorDarkenedValues[m][n].darkened = true; /* colorDarkenedValues[m][n].darkened_time = time(NULL); colorDarkenedValues[m][n].firstChange = false; colorDarkenedValues[m][n].secondChange = true; colorDarkenedValues[m][n].thirdChange = true; */ } } else if (previousDepth[m][n].changed == true && time(NULL) - previousDepth[m][n].initialTime>0.1) { depthImage.setColor(m, n, depthValues.getPixelColor(m, n)); previousDepth[m][n].changed = false; } else { depthImage.setColor(m, n, ofColor::red); } } } } else { for(int r = i*width; r<(i+1)*width; r++) { for (int t = j*height; t<(j+1)*height; t++) { colorDarkenedValues[r][t].darkened = false; if (previousDepth[r][t].changed == true && (time(NULL) - previousDepth[r][t].initialTime < 0.1)) { depthImage.setColor(r, t, ofColor::red); } else depthImage.setColor(r, t, depthValues.getPixelColor(r, t)); } } } } } //gradually lighten the values of color for (int a =0; a<640; a=a+1) { for (int d = 0; d<480; d=d+1) { //cout<<"coming in the for loop"; if (colorDarkenedValues[a][d].darkened == true) { color_image.setColor(a,d,snapshot.getColor(a,d)/8); //cout<<" coming here"; /* if ((time(NULL) - colorDarkenedValues[a][d].darkened_time)>1 && colorDarkenedValues[a][d].firstChange == false) { //cout<<" in first change"; color_image.setColor(a, d, color_image.getColor(a, d) + color_image.getColor(a, d)); colorDarkenedValues[a][d].firstChange = true; colorDarkenedValues[a][d].secondChange = false; colorDarkenedValues[a][d].thirdChange = false; } else if ((time(NULL) - colorDarkenedValues[a][d].darkened_time)>2 && colorDarkenedValues[a][d].secondChange == false) { //cout<<" in second change"; color_image.setColor(a, d, color_image.getColor(a,d) + color_image.getColor(a, d)); colorDarkenedValues[a][d].secondChange = true; } else if ((time(NULL) - colorDarkenedValues[a][d].darkened_time)>3 && colorDarkenedValues[a][d].thirdChange == false) { //cout<<" in third change"; color_image.setColor(a,d, color_image.getColor(a,d) + color_image.getColor(a, d)); //colorDarkenedValues[a][d].thirdChange = true; colorDarkenedValues[a][d].firstChange = false; colorDarkenedValues[a][d].secondChange = false; colorDarkenedValues[a][d].thirdChange = true; colorDarkenedValues[a][d].darkened = false; }*/ } else { //color_image.setColor(a, d, snapshot.getColor(a, d)*factor1 + color_image.getColor(a, d )*factor2); color_image.setColor(a, d, color_image.getColor(a, d) + ( (snapshot.getColor(a, d) - color_image.getColor(a, d))) *factor2); } } } stringstream msg; msg<<"Number of columns : "<<column << "\t Press c to increase, v to decrease"; msg<<"\nNumber of rows : "<<rows<<"\t Press r to increase and t to decrease "; msg<<"\nDepth Threshold is : "<<depthThresholdValue<<"\t Press d to increase and f to decrease" ; msg<<"\nColor Threshold is : "<<colorThreshold<<"\t Press n to increase and m to decrease"; msg<<"\nDepth Threshold considered : "<<considerDepth<<"\t Press k to toggle"; msg<<"\nColor Threshold considered : "<<considerColor<<"\t Press j to toggle"; msg<<"\ni to multiply and o to reduce "<<factor1; msg<<"\n q to increase factor 2 and w to reduce factor 2 "<<factor2; ofDrawBitmapString(msg.str(), 700, 500); }