int _tmain(int argc, _TCHAR* argv[]) {
IplImage *img = NULL;
int height, width, step, channels;
uchar *data;
int i, j, k;
//load an image
img = cvLoadImage("Lena.jpg");
if(!img) {
perror("Cannot open the file\n");
return -1;
}
//get the image data
height = img ->height;
width = img ->width;
step = img ->widthStep;
channels = img ->nChannels;
data = (uchar *)img ->imageData;
printf("Processing a %d*%d image with %d channels\n", height, width, channels);
//create a window
cvNamedWindow("test1", CV_WINDOW_AUTOSIZE);
cvMoveWindow("test1", 100, 100);
//invert the image
//which is equal to cvNot(img);
/*IplImage *dst = cvCreateImage(cvGetSize(img), img ->depth, channels);
cvNot(img, dst);*/
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
for(k = 0; k < channels; k++) {
data[i * step + j * channels + k] =
255 - data[i * step + j * channels + k];
}
}
}
//show the image
cvShowImage("test1", img);
cvWaitKey(0);
cvReleaseImage(&img);
//cvReleaseImage(&dst);
cvDestroyAllWindows();
return 0;
}invert color of image
最新推荐文章于 2024-11-22 12:30:36 发布
本文介绍如何使用OpenCV库实现图像反转功能,通过读取图像、获取图像数据并进行数据反转,最终展示处理后的图像。涵盖了图像加载、属性获取、数据操作及显示等关键步骤。
1215

被折叠的 条评论
为什么被折叠?



