OpenCV(C++)---图像操作

1、读写图像

  • imread (读):可以指定加载为灰度或者RGB图像
  • Imwrite (写):保存图像文件,类型由扩展名决定

2、读写像素

  • 读一个GRAY像素点的像素值(CV_8UC1)
Scalar intensity = img.at<uchar>(y, x); 
或者 Scalar intensity = img.at<uchar>(Point(x, y));

这里补充一个快捷键:
快捷键:选中代码,然后按 alt+“上下键”,可以上下移动代码
单通道的代码实现:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv) {
	Mat src,gray_src;
	src = imread("C:/Users/86180/Desktop/文档/学习/opencv/图片处理/zqy4.jpg");
	if (src.empty()) {
		cout << "could not load image..." << endl;
		return -1;
	}
	namedWindow("input", WINDOW_AUTOSIZE);
	imshow("input", src);
    
	cvtColor(src, gray_src, COLOR_BGR2GRAY);
	namedWindow("output", WINDOW_AUTOSIZE);
	imshow("output", gray_src);

	int height = gray_src.rows;
	int width = gray_src.cols;
	//快捷键:选中代码,然后按 alt+"上下键",可以上下移动代码

    //单通道
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			int gray = gray_src.at<uchar>(row, col);
			gray_src.at<uchar>(row, col) = 255 - gray;
		}
	}

	imshow("gray invert", gray_src);

	waitKey(0);
	return 0;
}

运行结果如下:在这里插入图片描述
在这里插入图片描述

  • 读一个RGB像素点的像素值
Vec3f intensity = img.at<Vec3f>(y, x); 
float blue = intensity.val[0]; 
float green = intensity.val[1]; 
float red = intensity.val[2];

多通道的代码实现:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv) {
	Mat src,gray_src;
	src = imread("C:/Users/86180/Desktop/文档/学习/opencv/图片处理/zqy4.jpg");
	if (src.empty()) {
		cout << "could not load image..." << endl;
		return -1;
	}
	namedWindow("input", WINDOW_AUTOSIZE);
	imshow("input", src);


	Mat dst;
	dst.create(src.size(), src.type());
	int height = src.rows;
	int width = src.cols;
	int nc = src.channels();

	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (nc == 1)
			{
			    int gray = gray_src.at<uchar>(row, col);
			    gray_src.at<uchar>(row, col) = 255 - gray;
			}
			else if (nc == 3)
			{
				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] = 255 - b;
				dst.at<Vec3b>(row, col)[1] = 255 - g;
				dst.at<Vec3b>(row, col)[2] = 255 - r;
			}
		}
	}

	imshow("gray invert", dst);

	waitKey(0);
	return 0;
}

运行结果如下:
在这里插入图片描述
若使用bitwise_not()函数效果相同
代码如下:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv) {
	Mat src,gray_src;
	src = imread("C:/Users/86180/Desktop/文档/学习/opencv/图片处理/zqy4.jpg");
	if (src.empty()) {
		cout << "could not load image..." << endl;
		return -1;
	}
	namedWindow("input", WINDOW_AUTOSIZE);
	imshow("input", src);


	Mat dst;
	dst.create(src.size(), src.type());
	int height = src.rows;
	int width = src.cols;
	int nc = src.channels();
/*
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (nc == 1)
			{
			    int gray = gray_src.at<uchar>(row, col);
			    gray_src.at<uchar>(row, col) = 255 - gray;
			}
			else if (nc == 3)
			{
				int b = src.at<Vec3b>(row, col)[0];
				int c = src.at<Vec3b>(row, col)[1];
				int d = src.at<Vec3b>(row, col)[2];
				dst.at<Vec3b>(row, col)[0] = 255 - b;
				dst.at<Vec3b>(row, col)[1] = 255 - c;
				dst.at<Vec3b>(row, col)[2] = 255 - d;
			}
		}
	}
    */
	bitwise_not(src, dst);//三个通道的取反操作
	imshow("gray invert", dst);

	waitKey(0);
	return 0;
}

上述代码运行结果与前面一致!
下面补充介绍一下几种函数:

  • bitwise_and是对二进制数据进行“与”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“与”操作,1&1=1,1&0=0,0&1=0,0&0=0
  • bitwise_or是对二进制数据进行“或”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“或”操作,1|1=1,1|0=0,0|1=0,0|0=0
  • bitwise_xor是对二进制数据进行“异或”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“异或”操作,11=0,10=1,01=1,00=0
  • bitwise_not是对二进制数据进行“非”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“非”操作,1=0,0=1

3、修改像素的值

  • 灰度图像
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);

4、Vec3b与Vec3F区别

  • Vec3b对应三通道的顺序是blue、green、red的uchar类型数据。
  • Vec3f对应三通道的float类型数据
  • 把CV_8UC1转换到CV32F1实现如下:
src.convertTo(dst, CV_32F);

补充:

  • 若不取反
for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (nc == 1)
			{
			    int gray = gray_src.at<uchar>(row, col);
			    gray_src.at<uchar>(row, col) = 255 - gray;
			}
			else if (nc == 3)
			{
				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] = b;
				dst.at<Vec3b>(row, col)[1] = g;
				dst.at<Vec3b>(row, col)[2] = 0;//改变这里
			}
		}
	}

其运行结果为:
在这里插入图片描述
若代码改变成:

for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (nc == 1)
			{
			    int gray = gray_src.at<uchar>(row, col);
			    gray_src.at<uchar>(row, col) = 255 - gray;
			}
			else if (nc == 3)
			{
				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] = b;
				dst.at<Vec3b>(row, col)[1] = 0;//这里改变
				dst.at<Vec3b>(row, col)[2] = r;
			}
		}
	}

其运行结果是:
在这里插入图片描述
若代码改变成;

for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (nc == 1)
			{
			    int gray = gray_src.at<uchar>(row, col);
			    gray_src.at<uchar>(row, col) = 255 - gray;
			}
			else if (nc == 3)
			{
				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] = 0;//这里改变
				dst.at<Vec3b>(row, col)[1] = g;
				dst.at<Vec3b>(row, col)[2] = r;
			}
		}
	}

其运行结果为:
在这里插入图片描述
也可以:

gray_src.at<uchar>(row, col) = max(r, max(b, g));//每个通道取最大值生成灰度图像等等

同理也可以最小值!

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页