大宇私人领地伊甸园丨宁愿做了后悔,也不要错过后悔[www.mrdayu.com]
注册

24小时联系邮箱:[email protected]

所有文章
大宇私人伊甸园: 首页 > 所有文章 > 编程人生 > 开源库 > opencv > [001.05]图像操作

[001.05]图像操作

发布时间:2018/07/11 opencv 标签opencv阅读:39

图像操作:

读写图像:

读写像素:

读一个GRAY像素点的像素值(CV_8UC1)

Scalar intensity = img.at<uchar>(y, x);

或者 Scalar intensity = img.at<uchar>(Point(x, y));

读一个RGB像素点的像素值

Vec3f intensity = img.at<Vec3f>(y, x);

float blue = intensity.val[0];

float green = intensity.val[1];

float red = intensity.val[2];

修改像素值:

灰度图像

img.at<uchar>(y, x) = 128;

RGB三通道图像

img.at<Vec3b>(y,x)[0]=128; // blue

img.at<Vec3b>(y,x)[1]=128; // green

img.at<Vec3b>(y,x)[2]=128; // red

空白图像赋值

img = Scalar(0);

ROI选择

Rect r(10, 10, 100, 100);

Mat smallImg = img(r);
for (int row = 0; row < height; row++) {
	for (int col = 0; col < width; col++) {
		if (channels == 3) {
			image.at<Vec3b>(row, col)[0] = 0; // blue
			image.at<Vec3b>(row, col)[1] = 0; // green
		}
	}
}

Vec3bVec3F:

Vec3b对应三通道的顺序是blue、green、red的uchar类型数据。

Vec3f对应三通道的float类型数据

把CV_8UC1转换到CV32F1实现如下:

src.convertTo(dst, CV_32F);

#include <opencv2/core/core.hpp> 
#include <opencv2/imgcodecs.hpp> 
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;
int main(int argc, char** args) {
Mat image = imread("D:/test.jpg", IMREAD_COLOR);
if (image.empty()) {
cout << "could not find the image resource..." << std::endl;
return -1;
}

int height = image.rows;
int width = image.cols;
int channels = image.channels();
printf("height=%d width=%d channels=%d", height, width, channels);
for (int row = 0; row < height; row++) {
       for (int col = 0; col < width; col++) {
                if (channels == 3) {
                         image.at<Vec3b>(row, col)[0] = 0; // blue
                         image.at<Vec3b>(row, col)[1] = 0; // green
                }
         }
}

namedWindow("My Image", CV_WINDOW_AUTOSIZE);
imshow("My Image", image);
waitKey(0);
return 0;
}

效果图:

一看就是在红灯区自拍留给警察叔叔来过的证据。

The bottom line, for me, is simple.

Let’s hope it doesn’t take 500 years for a discipline of testing to becomes the standard for software developers.

From:dayu
相关推荐

您好!请登录

合作网站快捷登录:
点击取消回复

已有1评论

    大宇博客

    点击这里给我发消息点击这里给我发消息