当前位置:   article > 正文

04 Opencv图像操作

04 Opencv图像操作

读写像素

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

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

修改像素值

  • 灰度图像 img.at(y, x) = 128;

  • RGB三通道图像 img.at(y,x)[0]=128; // blue img.at(y,x)[1]=128; // green img.at(y,x)[2]=128; // red

  • 空白图像赋值 img = Scalar(0);

  • ROI选择 Rect r(10, 10, 100, 100); Mat smallImg = img®;

Vec3b与Vec3F

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

灰度图像增强

可以用增强图像加钱图像的辨析度

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
	Mat src = imread("test.jpg");//读取图片
	if (src.empty())
	{
		cout << "could not load img...";
		return -1;
	}
	namedWindow("test");//设置窗口名称
	imshow("test", src);

	//灰度图像反转
	Mat gray_image;
	cvtColor(src, gray_image, COLOR_BGR2GRAY);// 转换为灰度图

	namedWindow("invert gray");
	imshow("invert gray", gray_image);
	int width = gray_image.cols;//获取图像的宽高
	int high = gray_image.rows;
	for (int i = 0; i < high; i++)
	{
		for (int j = 0; j < width; j++)
		{
			int gray = gray_image.at<uchar>(i, j);//获取行列坐标点
			gray_image.at<uchar>(i, j) = 255 - gray;//图像反转
		}
	}
	
	namedWindow("invert gray");
	imshow("invert gray", gray_image);
	
	
	waitKey(0);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

在这里插入图片描述

获取图像通道

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
	Mat src = imread("test.jpg");//读取图片
	if (src.empty())
	{
		cout << "could not load img...";
		return -1;
	}
	namedWindow("test");//设置窗口名称
	imshow("test", src);

	//灰度图像反转
	Mat gray_image;
	cvtColor(src, gray_image, COLOR_BGR2GRAY);// 转换为灰度图
	//namedWindow("invert gray");
	//imshow("invert gray", gray_image);
	int width = gray_image.cols;//获取图像的宽高
	int high = gray_image.rows;
	int nc = src.channels();//获取通道值
	Mat dst;
	dst.create(src.size(), src.type());
	for (int row = 0; row < high; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (nc == 1)//单通道
			{
				int gray = gray_image.at<uchar>(row, col);//获取行列坐标点
				gray_image.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] = r;//调整bgr参数获取想要的效果
				gray_image.at<uchar>(row, col) = min(r, min(b, g));//获取rgb的最小值
			}
			
		}
	}
	//namedWindow("invert gray");
	//imshow("invert gray", gray_image);
	imshow("三色通道", dst);
	waitKey(0);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

在这里插入图片描述

bitwise_not 算子对图像非操作

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
	Mat src = imread("test.jpg");//读取图片
	if (src.empty())
	{
		cout << "could not load img...";
		return -1;
	}
	namedWindow("test");//设置窗口名称
	imshow("test", src);

	//灰度图像反转
	Mat gray_image;
	cvtColor(src, gray_image, COLOR_BGR2GRAY);// 转换为灰度图
	//namedWindow("invert gray");
	//imshow("invert gray", gray_image);
	int width = gray_image.cols;//获取图像的宽高
	int high = gray_image.rows;
	int nc = src.channels();//获取通道值
	Mat dst;
	dst.create(src.size(), src.type());
	//for (int row = 0; row < high; row++)
	//{
	//	for (int col = 0; col < width; col++)
	//	{
	//		if (nc == 1)//单通道
	//		{
	//			int gray = gray_image.at<uchar>(row, col);//获取行列坐标点
	//			gray_image.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;//调整bgr参数获取想要的效果
	//			gray_image.at<uchar>(row, col) = min(r, min(b, g));//获取rgb的最小值
	//		}
	//		
	//	}
	//}
	bitwise_not(src, dst);
	//namedWindow("invert gray");
	//imshow("invert gray", gray_image);
	imshow("三色通道", dst);
	waitKey(0);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

在这里插入图片描述

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
	Mat src = imread("test.jpg");//读取图片
	if (src.empty())
	{
		cout << "could not load img...";
		return -1;
	}
	namedWindow("test");//设置窗口名称
	imshow("test", src);

	//灰度图像反转
	Mat gray_image;
	cvtColor(src, gray_image, COLOR_BGR2GRAY);// 转换为灰度图
	//namedWindow("invert gray");
	//imshow("invert gray", gray_image);
	int width = gray_image.cols;//获取图像的宽高
	int high = gray_image.rows;
	int nc = src.channels();//获取通道值
	Mat dst;
	dst.create(src.size(), src.type());
	for (int row = 0; row < high; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (nc == 1)//单通道
			{
				int gray = gray_image.at<uchar>(row, col);//获取行列坐标点
				gray_image.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;//调整bgr参数获取想要的效果
				//gray_image.at<uchar>(row, col) = min(r, min(b, g));//获取rgb的最小值
			}
			
		}
	}
	//bitwise_not(src, dst);
	//namedWindow("invert gray");
	//imshow("invert gray", gray_image);
	imshow("三色通道", dst);
	waitKey(0);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/173483?site
推荐阅读
相关标签
  

闽ICP备14008679号