Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of Pixel Operation in opencv3/C++ Image

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

这篇文章主要介绍了opencv3/C++图像像素操作的方法的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇opencv3/C++图像像素操作的方法文章都会有所收获,下面我们一起来看看吧。

RGB图像转灰度图

RGB图像转换为灰度图时通常使用:

进行转换,以下尝试通过其他对图像像素操作的方式将RGB图像转换为灰度图像。

#include#includeusing namespace cv;int main(){ //像素操作 Mat src,dst; src = imread("E:/image/image/daibola.jpg"); if(src.empty()) { printf("can not load image \n"); return -1; } namedWindow("input"); imshow("input",src); dst.create(src.size(), src.type()); for(int row = 0; row < src.rows; row++) { for(int col = 0; col < src.cols; col++) { int b = src.at(row, col)[0]; int g = src.at(row, col)[1]; int r = src.at(row, col)[2]; dst.at(row, col)[0] = max(r,max(g,b)); dst.at(row, col)[1] = max(r,max(g,b)); dst.at(row, col)[2] = max(r,max(g,b)); } } namedWindow("output"); imshow("output",dst); waitKey();}

同理使用min(r,min(g,b))可以看到由于选择了较小的灰度值图像会明显变暗:

图像线性增强

通过对图像像素操作(线性变换),实现图像的线性增强。

#include#includeusing namespace cv;int main(){ Mat src1, dst; src1 = imread("E:/image/image/im1.jpg"); if(src1.empty()) { printf("can not load im1 \n"); return -1; } double alpha = 1.2, beta = 50; dst = Mat::zeros(src1.size(), src1.type()); for(int row = 0; row < src1.rows; row++) { for(int col = 0; col < src1.cols; col++) { if(src1.channels() == 3) { int b = src1.at(row, col)[0]; int g = src1.at(row, col)[1]; int r = src1.at(row, col)[2]; dst.at(row, col)[0] = saturate_cast(b*alpha + beta); dst.at(row, col)[1] = saturate_cast(g*alpha + beta); dst.at(row, col)[2] = saturate_cast(r*alpha + beta); } else if (src1.channels() == 1) { float v = src1.at(row, col); dst.at(row, col) = saturate_cast(v*alpha + beta); } } } namedWindow("output",CV_WINDOW_AUTOSIZE); imshow("output", dst); waitKey(); return 0;}

掩膜操作调整图像对比度

使用一个3×3掩模增强图像对比度:

#include#includeusing namespace cv;int main(){ Mat src, dst; src = imread("E:/image/image/daibola.jpg"); CV_Assert(src.depth() == CV_8U); if(!src.data) { printf("can not load image \n"); return -1; } src.copyTo(dst); for(int row = 1; row

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report