码迷,mamicode.com
首页 > 其他好文 > 详细

opencv 实现图像像素点反转

时间:2018-08-01 20:46:24      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:tco   14.   shadow   [1]   space   分享   tab   ref   text   

最近在学习opencv图像处理,自学到将一副原图像上的像素点像素值反转,再输出新的图像

,代码如下:

#include<opencv2/opencv.hpp>

#include<iostream>

#include<math.h>

using namespace cv;

using namespace std;

int main(int argc, char **argv)

{

Mat gray_image;

Mat src = imread("C:/Users/Administrator/Desktop/1.jpg");

if (!src.data)

{

cout << "could not load image..." << endl;

return -1;

}

cvNamedWindow("原图",WINDOW_AUTOSIZE);

imshow("原图", src);

cvtColor(src, gray_image, CV_BGR2GRAY);

//namedWindow("output", CV_WINDOW_AUTOSIZE);

//imshow("output", gray_image);

//int height = gray_image.rows;

//int width = gray_image.cols;

Mat dst;

dst.create(src.size(), src.type());

int height = src.rows;

int width = src.cols;

int nc = src.channels();

for (int row = 0; row < height; row++) {

for (int col = 0; col < width; col++) {

//单通道

if (nc == 1) {

int gray = gray_image.at<uchar>(row, col);

gray_image.at<uchar>(row, col) = 255 - gray;

}

//三通道

else if (nc == 3) {

int b = src.at<Vec3b>(row,col)[0];

int g = src.at<Vec3b>(row, col)[1];

int r = src.at<Vec3b>(row, col)[2];

dst.at<Vec3b>(row, col)[0] = 255-b;

dst.at<Vec3b>(row, col)[1] = 255 - g;

dst.at<Vec3b>(row, col)[2] = 255 - r;

gray_image.at<uchar>(row, col) = max(r, max(b, g));//取最大值作为灰度值

}

}

}

imshow("output", gray_image);

//bitwise_not(src, dst);

imshow("gray invert", dst);


waitKey(0);

return 0;

}


技术分享图片

技术分享图片

技术分享图片 


opencv 实现图像像素点反转

标签:tco   14.   shadow   [1]   space   分享   tab   ref   text   

原文地址:http://blog.51cto.com/13485871/2153336

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!