opencv3/C++图像像素操作详解
作者:阿卡蒂奥 时间:2021-04-23 08:29:26
RGB图像转灰度图
RGB图像转换为灰度图时通常使用:
进行转换,以下尝试通过其他对图像像素操作的方式将RGB图像转换为灰度图像。
#include<opencv2/opencv.hpp>
#include<math.h>
using 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<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] = max(r,max(g,b));
dst.at<Vec3b>(row, col)[1] = max(r,max(g,b));
dst.at<Vec3b>(row, col)[2] = max(r,max(g,b));
}
}
namedWindow("output");
imshow("output",dst);
waitKey();
}
同理使用min(r,min(g,b))可以看到由于选择了较小的灰度值图像会明显变暗:
图像线性增强
通过对图像像素操作(线性变换),实现图像的线性增强。
#include<opencv2/opencv.hpp>
#include<math.h>
using 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<Vec3b>(row, col)[0];
int g = src1.at<Vec3b>(row, col)[1];
int r = src1.at<Vec3b>(row, col)[2];
dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b*alpha + beta);
dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g*alpha + beta);
dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r*alpha + beta);
}
else if (src1.channels() == 1)
{
float v = src1.at<uchar>(row, col);
dst.at<uchar>(row, col) = saturate_cast<uchar>(v*alpha + beta);
}
}
}
namedWindow("output",CV_WINDOW_AUTOSIZE);
imshow("output", dst);
waitKey();
return 0;
}
掩膜操作调整图像对比度
使用一个3×3掩模增强图像对比度:
#include<opencv2/opencv.hpp>
#include<math.h>
using 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<(src.rows - 1); row++)
{
const uchar* previous = src.ptr<uchar>(row - 1);
const uchar* current = src.ptr<uchar>(row);
const uchar* next = src.ptr<uchar>(row + 1);
uchar* output = dst.ptr<uchar>(row);
for(int col = src.channels(); col < (src.cols - 1)*src.channels(); col++)
{
*output = saturate_cast<uchar>(9 * current[col] - 2*previous[col] - 2*next[col] - 2*current[col - src.channels()] - 2*current[col + src.channels()]);
output++;
}
}
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image",dst);
waitKey();
return 0;
}
像素重映射
利用cv::remap实现像素重映射;
cv::remap参数说明:
Remap(
InputArray src,// 输入图像
OutputArray dst,// 输出图像
InputArray map1,// 映射表1(CV_32FC1/CV_32FC2)
InputArray map2,// 映射表2(CV_32FC1/CV_32FC2)
int interpolation,// 选择的插值
int borderMode,// 边界类型(BORDER_CONSTANT)
const Scalar borderValue// 颜色
)
插值方法:
CV_INTER_NN =0,
CV_INTER_LINEAR =1,
CV_INTER_CUBIC =2,
CV_INTER_AREA =3,
CV_INTER_LANCZOS4 =4
通过像素重映射实现图像垂直翻转:
#include<opencv2/opencv.hpp>
using 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", CV_WINDOW_AUTOSIZE);
imshow("input", src);
Mat mapx,mapy;
mapx.create(src.size(), CV_32FC1);
mapy.create(src.size(), CV_32FC1);
for(int row = 0; row < src.rows; row++)
{
for(int col = 0; col < src.cols; col++)
{
mapx.at<float>(row, col) = col;
mapy.at<float>(row, col) = src.rows - row - 1;
}
}
remap(src, dst, mapx, mapy, CV_INTER_NN, BORDER_CONSTANT, Scalar(0,255,255));
namedWindow("output", CV_WINDOW_AUTOSIZE);
imshow("output",dst);
waitKey();
return 0;
}
来源:https://blog.csdn.net/akadiao/article/details/78836384
标签:opencv3,C++,图像,像素
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python统计一个文本中重复行数的方法
2021-08-20 12:50:46
修改、删除数据记录(DELETE\\UPDATE)
2009-02-27 15:50:00
mysql使用LOAD语句批量录入数据
2010-03-18 16:19:00
python实现图像高斯金字塔的示例代码
2023-05-06 02:02:32
![](https://img.aspxhome.com/file/2023/1/84981_0s.png)
CSS在页面布局中实现div水平居中的方法总结
2008-06-03 12:09:00
Logo 设计准则[译]
2009-07-22 21:05:00
![](https://img.aspxhome.com/file/UploadPic/20097/22/01-16s.jpg)
Python异常处理知识点总结
2023-01-04 16:03:36
Mysql数据库常用命令
2009-03-06 14:29:00
PHP详解ASCII码对照表与字符转换
2023-11-07 01:16:55
![](https://img.aspxhome.com/file/2023/9/55549_0s.jpg)
Python关于抽奖系统的思考与设计思路
2022-06-09 12:53:36
Python实现双轴组合图表柱状图和折线图的具体流程
2021-08-24 03:23:33
![](https://img.aspxhome.com/file/2023/9/95529_0s.jpg)
XML DOM介绍和例子
2007-10-15 20:23:00
Python简单几步画个钻石戒指
2023-04-26 13:59:27
![](https://img.aspxhome.com/file/2023/8/94088_0s.gif)
Python如何配置环境变量详解
2021-11-20 08:18:21
![](https://img.aspxhome.com/file/2023/6/97056_0s.png)
pytorch 一行代码查看网络参数总量的实现
2023-04-23 17:42:36
![](https://img.aspxhome.com/file/2023/8/89138_0s.jpg)
pytorch model.cuda()花费时间很长的解决
2021-12-08 08:02:06
![](https://img.aspxhome.com/file/2023/3/86143_0s.jpg)
分析描述CentOS mysql安装系统
2010-10-25 20:34:00
Python实现求一个集合所有子集的示例
2022-09-01 20:56:51
python重试装饰器的简单实现方法
2022-07-15 12:54:10
PHP图片库imagemagick安装方法
2023-11-16 18:54:38