OpenCV 图像取反:将图像变为原来图像的反色。例如,如果一副灰度图像的每个像素值由2^8=256个,假设点(i,j)出像素值为 a,则取反后的像素值为 255-a。
取反公式:
inv(i,j)=L-1-a(i,j) //L表示图像的灰度级
以下是利用OpenCV将彩色图像取反并保存的源代码:
#include <highgui.h>
#include <core/core.hpp>
#include <cv.h>
#include <iostream>
using namespace std;
int main()
{
IplImage *img=0,*outImage=0;
int height,width,step,channels;
uchar *data;
img=cvLoadImage("D:\\robot\\Picture_set1\\机器人图片.png"); //由图像路径获得图像,可以修改
if (!img)
{
cout<<"Could not load image file: "<<endl;
exit(0);
}
/*获取图像相关信息*/
height=img->height;
width=img->width;
step=img->widthStep;
channels=img->nChannels;
data=(uchar*)img->imageData;
outImage =cvCreateImage(cvSize(width,height),img->dept