首页 > 编程知识 正文

OpenCV学习C 像素点的读取

时间:2023-05-06 01:17:34 阅读:228855 作者:1290

OpenCV学习(C++)——像素点的读取 C++中的像素遍历与访问

对彩色图片处理:(原像素-255)反色 基于数组下表改变每一个像素点

数组遍历 void QuickDemo::pixel_visit_demo(Mat &image){int w = image.cols;//获取图像宽度int h = image.rows;//获取图像高度int dims = image.channels();//获取通道数for (int row = 0; row < h; row++){for (int col = 0; col < w; col++){if (dims == 1)//灰度图像{int pv = image.at<uchar>(row, col);//单通道 像素值是uchar型image.at<uchar>(row, col) = 255 - pv;}if (dims == 3)//彩色图像{Vec3b bgr=image.at<Vec3b>(row, col);//返回三个值哦可以把bgr这个结构看成数组image.at<Vec3b>(row, col)[0] = 255 - bgr[0];image.at<Vec3b>(row, col)[1] = 255 - bgr[1];image.at<Vec3b>(row, col)[2] = 255 - bgr[2];}}}imshow("像素读写演示", image);} 指针方式遍历

把上面的代码中的双重for循环改成下main这个就行了

for (int row = 0; row < h; row++){uchar *current_row = image.ptr<uchar>(row);for (int col = 0; col < w; col++){if (dims == 1)//灰度图像{int pv = *current_row;//单通道 像素值是uchar型*current_row++ = 255 - pv;}if (dims == 3)//彩色图像{*current_row++ = 255 - *current_row;//往后走三个位置(三通道)*current_row++ = 255 - *current_row;*current_row++ = 255 - *current_row;}}}

记得自己声明void QuickDemo::pixel_visit_demo(Mat &image);这个函数

主函数 #include<opencv2/opencv.hpp>#include<iostream>#include<quickjopencv.h>using namespace std;using namespace cv;int main(){Mat src = imread("C:/Users/LENOVO/Desktop/work/picture/2.png");if (src.empty())//没找到图像{printf("could not find picture!n");return -1;}imshow("输入窗口", src);//输入窗口名字:输入窗口QuickDemo qd;qd.pixel_visit_demo(src);waitKey(0);destroyAllWindows();return 0;}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。