opencv4opencv视频教程 C++ 5读写图像imreadimwrite读写像素at<>()修改像素值ROI区域选择(图像裁剪)RectVec3b与Vec3FCV_32F)
Posted Dontla
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv4opencv视频教程 C++ 5读写图像imreadimwrite读写像素at<>()修改像素值ROI区域选择(图像裁剪)RectVec3b与Vec3FCV_32F)相关的知识,希望对你有一定的参考价值。
文章目录
读写图像
读写像素at<>()
修改像素值、区域选择(图像裁剪)Rect
貌似opencv4没有img这个函数。。。(不是img函数,而是图像名称!)
示例:区域裁剪
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, const char* argv[])
Mat src, dst, dst1, dst2, dst3, difference;
src = imread("./test.jpg");
//if (src.empty())
if (!src.data)
printf("could not load image...\\n");
return -1;
namedWindow("input img"); //默认自动窗口大小
imshow("input img", src);
Rect r(100,50,300,200); //(起始x,起始y,截取x,截取y)
dst = src(r);
imshow("output img", dst);
waitKey(0);
return 0;
VS编译运行:
Vec3b与Vec3F
示例:我这转换后咋变这样了?
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, const char* argv[])
Mat src, dst, dst1, dst2, dst3, difference;
src = imread("./test.jpg");
//if (src.empty())
if (!src.data)
printf("could not load image...\\n");
return -1;
namedWindow("input img"); //默认自动窗口大小
imshow("input img", src);
src.convertTo(dst,CV_32F);
imshow("output img", dst);
waitKey(0);
return 0;
转换成CV_32F浮点数,加上一个参数就没事了,但是打印像素值却有点让人看不懂呢/??显示跟像素值是怎么映射的?不明觉厉,已上知乎问了
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, const char* argv[])
Mat src, dst, dst1, dst2, dst3, difference;
src = imread("./test.jpg");
//if (src.empty())
if (!src.data)
printf("could not load image...\\n");
return -1;
namedWindow("input img"); //默认自动窗口大小
imshow("input img", src);
//src.convertTo(dst, CV_32F);
src.convertTo(dst, CV_32F, 1 / 255.0);
printf("dst type:%d\\n", dst.type()); //dst type:21
printf("%f\\n", dst.at<float>(0, 0)); //0.133333
printf("%f\\n", dst.at<float>(0, 1)); //0.054902
imshow("output img", dst);
waitKey(0);
return 0;
参考文章:Opencv中Mat数据类型CV_8UC3转换为 CV_32FC3注意事项
代码示例
手动求一张彩色图片的灰度图,并且与opencv内置求灰度函数cvtColor函数作比较
原图:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, const char* argv[])
Mat src, dst, dst1, dst2, dst3, difference;
src = imread("./test.jpg");
//if (src.empty())
if (!src.data)
printf("could not load image...\\n");
return -1;
namedWindow("input img"); //默认自动窗口大小
imshow("input img", src);
cvtColor(src,dst,COLOR_BGR2GRAY);
imshow("output img", dst);
//dst1 = Mat(src.size(),src.type());
dst1.create(src.size(), CV_8UC1);
dst2.create(src.size(), CV_8UC1);
dst3.create(src.size(), CV_8UC1);
difference.create(src.size(), CV_8UC1);
int height = src.rows;
int width = src.cols;
int sc = src.channels();
for (int row = 0; row < height; row++)
for (int col = 0; col < width; 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];
dst1.at<char>(row, col) = min(b, min(g, r));
dst2.at<char>(row, col) = b + g + r - max(b, max(g, r)) - min(b, min(g, r)); //灰度取中间值
dst3.at<char>(row, col) = max(b, max(g, r));
//namedWindow("output img1"); //默认自动窗口大小
imshow("output img1", dst1);
imshow("output img2", dst2);
imshow("output img3", dst3);
//求两灰度图差
for (int row = 0; row < height; row++)
for (int col = 0; col < width; col++)
//difference.at<char>(row, col) = abs(dst.at<char>(row, col) - dst2.at<char>(row, col));
//difference.at<char>(row, col) = dst.at<char>(row, col) - dst2.at<char>(row, col);
difference.at<char>(row, col) = (dst.at<char>(row, col) - dst2.at<char>(row, col)) > 0 ? 0 : 255; //从结果看来,opencv转换灰度方法确实跟我们自己用的不一样
//imshow("output img3", dst - dst2);
imshow("output img4", difference);
//imshow("output img4", abs(dst-dst2));
waitKey(0);
return 0;
VS编译运行结果:
以上是关于opencv4opencv视频教程 C++ 5读写图像imreadimwrite读写像素at<>()修改像素值ROI区域选择(图像裁剪)RectVec3b与Vec3FCV_32F)的主要内容,如果未能解决你的问题,请参考以下文章
opencv4opencv教程 C++ 4Mat对象(深拷贝:clone()copyTo(),create()创建图片,zeros()eye()初始化空白图像,Scalar()创建向量)
opencv4opencv视频教程 C++(opencv教程)2加载,修改,保存图像
opencv4opencv视频教程 C++(opencv教程)1opencv介绍和环境搭建
opencv4opencv视频教程 C++(opencv教程)3矩阵的掩膜操作(filter2D)
opencv4opencv视频教程 C++ 7手动调整图像亮度与对比度 g(i, j) = αf(i, j) + β(点操作与邻域操作概念)