Created
April 23, 2013 14:48
-
-
Save uugan/5444175 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Smoothing image by K nearest neighbors */ | |
| float SmoothbyKNN(unsigned char *inbuf, unsigned char* outbuf, int height, int width){ | |
| int start = GetTickCount(); | |
| int _k = 6; // _k<n*n | |
| int _n = 3; // 3x3 | |
| int p = 1; // 3/2 | |
| int raz[9]; | |
| int tmp=0, cnt=0, sum=0; | |
| double* out = new double[height*width]; | |
| for(int i=p;i<height-p;i++) | |
| for(int j=p;j<width-p;j++){ | |
| cnt = 0; | |
| tmp = 0; | |
| for(int k=-p;k<=p;k++) | |
| for(int l=-p;l<=p;l++) | |
| raz[cnt++]=inbuf[(i+k)*width+j+l]; | |
| --cnt; | |
| /* start sorting */ | |
| for(int k=0; k<cnt-1 ;k++) | |
| for(int u=k+1; u<cnt ;u++) | |
| if(abs(raz[k] - inbuf[i*width+j])>abs(raz[u] - inbuf[i*width+j])){ | |
| tmp = raz[k]; | |
| raz[k] = raz[u]; | |
| raz[u] = tmp; | |
| } | |
| /* end of sorting */ | |
| sum=0; | |
| for(int k=0;k<_k;k++){ | |
| sum = sum + raz[k]; | |
| } | |
| out[i*width+j] = (sum/_k); | |
| } | |
| /* finding max, min */ | |
| double max=-999,min=999; | |
| for(int i=p;i<height-p;i++) | |
| for(int j=p;j<width-p;j++){ | |
| if(out[i*width+j]>max)max=out[i*width+j]; | |
| if(out[i*width+j]<min)min=out[i*width+j]; | |
| } | |
| /* double array to unsigned char */ | |
| for(int i=p;i<height-p;i++) | |
| for(int j=p;j<width-p;j++) | |
| outbuf[i*width+j]=(unsigned char)(out[i*width+j]-min)/(max-min)*255 ; | |
| return (float)(GetTickCount()-start); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment