#include #include "cv.h" #include "cxcore.h" #include "highgui.h" #pragma comment(lib,"cv.lib") #pragma comment(lib,"cxcore.lib") #pragma comment(lib,"highgui.lib") void binary(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel, int th); int main(){ IplImage *in, *out; int i,j; in = cvLoadImage("./image640.bmp", 0); out = cvCreateImage(cvSize(in->width, in->height), in->depth, in->nChannels); binary((unsigned char*)out->imageData, (unsigned char*)in->imageData, in->height, in->width, in->nChannels, 127); cvNamedWindow("in", 1); cvShowImage("in", in); cvNamedWindow("out", 1); cvShowImage("out", out); cvWaitKey(-1); cvDestroyWindow("in"); cvDestroyWindow("out"); cvReleaseImage( &in ); cvReleaseImage( &out ); return 0; } void binary(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel, int th){ int i, j; int index; int widthStep; widthStep = inWidth * inChannel; if(widthStep % 4 != 0){ widthStep = widthStep + 4- widthStep%4; } for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ index = i * widthStep + j; // 臒lˆČć‚Ķ255A臒l–¢–ž‚Ķ0‚É‚·‚é if(in[index] >= th){ out[index] = 255; } else { out[index] = 0; } } } }