先贴代码:
// testcv.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
int main(int argc, char* argv[])
{
IplImage* src=cvLoadImage("D:\\360安全浏览器下载\\BB\\2000.jpg",-1);
int height=src->height;
int width=src->width;
int channels=src->nChannels;
int depth=src->depth;
int step=src->widthStep;
IplImage* img=cvCreateImage(cvSize(width,height),depth,channels);
uchar* data1=(uchar *)img->imageData;
uchar* data2=(uchar *)src->imageData;
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
{
if(i==0||j==0)
{
data1[i*step+j*channels+0]=128;
data1[i*step+j*channels+1]=128;
data1[i*step+j*channels+2]=128;
}
else
{
data1[i*step+j*channels+0]=(uchar)(data2[i*step+j*channels+0]-data2[(i-1)*step+(j-1)*channels+0]+255)/2;
data1[i*step+j*channels+1]=(uchar)(data2[i*step+j*channels+1]-data2[(i-1)*step+(j-1)*channels+1]+255)/2;
data1[i*step+j*channels+2]=(uchar)(data2[i*step+j*channels+2]-data2[(i-1)*step+(j-1)*channels+2]+255)/2;
}
}
cvNamedWindow("win1");
cvShowImage("win1",src);
cvNamedWindow("win2");
cvShowImage("win2",img);
cvWaitKey(0);
cvReleaseImage(&src);
cvDestroyWindow("win1");
cvSaveImage("D:\\360安全浏览器下载\\BB\\2001.jpg",img);
cvReleaseImage(&img);
cvDestroyWindow("win2");
return 0;
}
效果图:
实现原理:每个位置像素个分量减去其左上角位置像素相应个分量,然后加上255再除以2,得此效果。