1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 图像元素遍历及像素值取反

图像元素遍历及像素值取反

时间:2019-04-04 02:26:04

相关推荐

图像元素遍历及像素值取反

下面直接来看代码

#include<iostream>#include<opencv2\opencv.hpp>using namespace std;using namespace cv;int main(){Mat img;img = imread("D:/photogallery/image_4.png");if (img.empty()){cout << "读入失败" << endl;return -1;}imshow("图片4", img);for (int row = 0; row < img.rows; row++){for (int col = 0; col<img.cols; col++){if (img.channels() == 1)//灰度图片{int pv = img.at<uchar>(row, col);img.at<uchar>(row, col) = 255 - pv;}if (img.channels() == 3)//BGR图片{Vec3b bgr = img.at<Vec3b>(row, col);img.at<Vec3b>(row, col)[0] = 255 - bgr[0];img.at<Vec3b>(row, col)[1] = 255 - bgr[1];img.at<Vec3b>(row, col)[2] = 255 - bgr[2];}}}/*int dims = img.channels();for (int row = 0; row < img.rows; row++){uchar*current_row = img.ptr<uchar>(row);for (int col = 0; col <img.cols; col++){if (dims == 1){int pv = *current_row;*current_row ++= 255 - pv;}if (dims == 3){Vec3b bgr = img.at<Vec3b>(row, col);*current_row++ = 255 - *current_row;*current_row++ = 255 - *current_row;*current_row++ = 255 - *current_row;}}}*/imshow("像素读写", img);waitKey(0);system("pause");return 0;}

代码运行结果如下:

左图为原图,右图是像素值取反后的结果,可以看出黑色头发变为白色。

遍历像素是利用了两个for循环,将行,列的每个像素点遍历,进入循环后 通过if语句来判断图片是彩色图像,还是灰度图像,这里是利用channels()通道数来判断的,当然如果你知道自己是什么图像也可不用判断。

单通道代码解读(if (img.channels() == 1)):

int pv = img.at(row, col);

这句是获得当前的像素点 ,调用对象的at模板,<>里是像素的数据类型,为了像素值的取反,将其赋给int类型的pv。

img.at(row, col) = 255 - pv;

因为像素值在0~255,所以用255减去目前的像素值,实现取反,再利用赋值操作赋给图像。

彩图图代码解读(if (img.channels() == 3)

Vec3b bgr = img.at(row, col);

在opencv中彩色图的像素类型专用Vec3b,你可以把这个类型看成一个数组。

img.at(row, col)[0] = 255 - bgr[0];

img.at(row, col)[1] = 255 - bgr[1];

img.at(row, col)[2] = 255 - bgr[2];

因为是三通道,所以数组元素下标为 0,1,2。将其取反后,分别赋值回去。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。