#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 KidoRange(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel, unsigned char min, unsigned char max); int main(){ IplImage *in, *out; int i,j; in = cvLoadImage("./image640.bmp", 0); out = cvCreateImage(cvSize(in->width, in->height), in->depth, in->nChannels); KidoRange((unsigned char*)out->imageData, (unsigned char*)in->imageData, in->height, in->width, in->nChannels, 0, 200); 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 KidoRange(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel, unsigned char min, unsigned char max){ int i, j; int index; double tmp; double rate; int range; int widthStep; widthStep = inWidth * inChannel; if(widthStep % 4 != 0){ widthStep = widthStep + 4 - widthStep % 4; } range = abs(max - min); rate = (double)256 / (double)(range + 1); for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ index = i * widthStep + j; if(in[index] >= min && in[index] <= max){ tmp = (in[index] - min) * rate; if(tmp > 255){ tmp = 255; } out[index] = tmp; } else if(in[index] < min){ out[index] = 0; } else if(in[index] > max){ out[index] = 255; } } } }