#include "stdafx.h" #include <iostream> #include <stdlib.h> #include <cv.h> #include <cxcore.h> #include <highgui.h> #include <math.h> int main( int argc, char** argv ) { int height ,width ,step ,channels ; int i,j,k,same ,max,min,thresh,sum; uchar*data0,*data1 ; //char *filename="result.bmp"; // IplImage* Img,*nimg; //声明IplImage指针 //载入图像 IplImage* nimg = cvLoadImage("1.jpg"); IplImage* Img = cvCreateImage(cvGetSize(nimg),8,1); cvCvtColor( nimg, Img, CV_BGR2GRAY ); height = Img->height; width = Img->width; step = Img->widthStep/sizeof(uchar); channels = Img->nChannels; data0 = (uchar*)Img->imageData; data1 = (uchar*)nimg->imageData; printf("Processing a %d X %d image with %d channels\n",width,height,channels); int OffSetX[37] = { -1, 0, 1, -2,-1, 0, 1, 2, -3,-2,-1, 0, 1, 2, 3, -3,-2,-1, 0, 1, 2, 3, -3,-2,-1, 0, 1, 2, 3, -2,-1, 0, 1, 2, -1, 0, 1 }; int OffSetY[37] = { -3,-3,-3, -2,-2,-2,-2,-2, -1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3 }; max = min = data0[0]; //for(i=0;i<height;i++) // for(j=0;j<width;j++) //{ // if(data0[i*step+j]>max) max = data0[i*step+j]; //if(data0[i*step+j]<min) min = data0[i*step+j]; // } for(i=3;i<height-3;i++) for(j=3;j<width-3;j++) { same =0;sum = 0; for(k=0;k<37;k++) { sum+=data0[(i+OffSetY[k])*step+(j+OffSetX[k])]; thresh = sum/37; if(fabs( (float)(data0[(i+OffSetY[k])*step+(j+OffSetX[k])]-data0[i*step+j]))<=thresh) same++; if(same<18) data1[i*step+j] = 255; else data1[i*step+j] = 0; } } cvNamedWindow( "Image", 1 ); //创建窗口 cvShowImage( "Image", nimg ); //显示图像 cvWaitKey(0); //等待按键 cvDestroyWindow( "Image" );//销毁窗口 cvReleaseImage( &Img ); //释放图像 cvReleaseImage( &nimg ); return 0; }