OpenCV's operation on images (4)

Table of contents

Official online help documentation: OpenCV: OpenCV modules

Do some simple operations on images with OpenCV 

1. Image histogram

2. Image two-dimensional histogram

3. Image histogram equalization

4. Image convolution operation

5. Gaussian Blur

Image averaging and Gaussian blur

6. Gaussian Bilateral Blur (Edge Filtering)


Official online help documentation: OpenCV: OpenCV modules

Do some simple operations on images with OpenCV 

1. Image histogram

Interpretation of image histograms

The image histogram is a statistical feature of the image pixel value, the calculation cost is small, and it has many advantages such as image translation, rotation, and scaling invariance.

It is widely used in various fields of image processing, especially threshold segmentation of grayscale images, color-based image retrieval, image classification, and backprojection tracking.

Commonly divided into

  • Gray histogram
  • color histogram

Bins refers to the size range of the histogram. For pixel values ​​between 0 and 255, there are at least 256 bins. In addition, there can be 16, 32, 48, 128, etc. The size of 256 divided by the bin should be an integer times.

Related APIs in OpenCV

calcHist(&bgr_plane[0],1,0,Mat(),b_hist,1,bins,ranges);

cv.calcHist([image],[i],None,[256],[0,256])

	#include<opencv2/opencv.hpp>
	#include<iostream>
	#include<vector>
	
	using namespace cv;
	using namespace std;
	class demo
	{
	public:
		// 图像直方图
		void histogram_demo(Mat& image) {
			// 三通道分离
			vector<Mat> bgr_plane;
			split(image, bgr_plane);
			// 定义参数变量
			const int channels[1] = { 0 };
			const int bins[1] = { 256 };
			float hranges[2] = { 0,255 };
			const float* ranges[1] = { hranges };
			Mat b_hist;
			Mat g_hist;
			Mat r_hist;
			//计算Blue, Green,Red通道的直方图
			calcHist(&bgr_plane[0], 1, 0, Mat(), b_hist, 1, bins, ranges);
			calcHist(&bgr_plane[1], 1, 0, Mat(), g_hist, 1, bins, ranges);
			calcHist(&bgr_plane[2], 1, 0, Mat(), r_hist, 1, bins, ranges);
	
			//显示直方图
			int hist_w = 512;
			int hist_h = 400;
			int bin_w = cvRound((double)hist_w / bins[0]);
			Mat histImage = Mat::zeros(hist_h, hist_w, CV_8UC3);
			//归一化直方图数据
			normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
			normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
			normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
			// 绘制直方图曲线
			for (int i = 1; i < bins[0]; i++) {
				line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(b_hist.at<float>(i - 1))),
					Point(bin_w * (i), hist_h - cvRound(b_hist.at<float>(i))), Scalar(255, 0, 0), 2, 8, 0);
				line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(g_hist.at<float>(i - 1))),
					Point(bin_w * (i), hist_h - cvRound(g_hist.at<float>(i))), Scalar(0, 255, 0), 2, 8, 0);
				line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(r_hist.at<float>(i - 1))),
					Point(bin_w * (i), hist_h - cvRound(r_hist.at<float>(i))), Scalar(0, 0, 255), 2, 8, 0);
			}
			// 显示直方图
			namedWindow("Histogram Demo", WINDOW_AUTOSIZE);
			imshow("Histogram Deno", histImage);
		}
	};
	int main(int argc, char** argv)
	{
		Mat src = imread("迪丽热巴.png",IMREAD_UNCHANGED);
		// Mat是一种特殊的数据类型格式,是一种二维数组,用来存储图片的数据
		// namedWindow("输出窗口", WINDOW_FREERATIO);
		// 只有imshow无法调整图片显示窗口的大小,通过namedWindow调整窗口的大小
		if (src.empty()) {
			cout << "没有找到你的图片" << endl;
			return -1;
		}
	
		imshow("输出窗口", src);
	
		demo d;
		d.histogram_demo(src); 
		waitKey(0);// 设置图片显示时间
		destroyAllWindows();// 释放所有窗口
		return 0;
	}

2. Image two-dimensional histogram

	#include<opencv2/opencv.hpp>
	#include<iostream>
	#include<vector>
	
	using namespace cv;
	using namespace std;
	class demo
	{
	public:
		// 二维直方图
		void histogram_2d_demo(Mat& image)
		{
			// 2D直方图
			Mat hsv, hs_hist;
			cvtColor(image, hsv, COLOR_BGR2HSV); 
			int hbins = 30, sbins = 32;
			int hist_bins[] = { hbins, sbins }; 
			float h_range[] = { 0,180 };
			float s_range[] = { 0,256 };
			const float* hs_ranges[] = { h_range, s_range }; 
			int hs_channels[] = { 0,1 };
			calcHist(&hsv,1, hs_channels,Mat(), hs_hist,2, hist_bins, hs_ranges, true, false); 
			double maxval = 0;
			minMaxLoc(hs_hist, 0, &maxval, 0, 0); 
			int scale = 10;
			Mat hist2d_image = Mat::zeros(sbins * scale, hbins * scale, CV_8UC3); 
			for (int h = 0; h < hbins; h++) {
				for (int s = 0; s < sbins; s++) {
					float binVal = hs_hist.at<float>(h, s);
					int intensity = cvRound(binVal * 255 / maxval);
					rectangle(hist2d_image, Point(h * scale, s * scale),
						Point((h + 1) * scale - 1, (s + 1) * scale - 1),
						Scalar::all(intensity),
						-1);
				}
				// 输出图像色彩转换
				applyColorMap(hist2d_image, hist2d_image, COLORMAP_JET);
				imshow("H-S Histogram", hist2d_image);
				// imwrite("D:/ hist__2d.png", hist2d_image);
			}
		}
	};
	int main(int argc, char** argv)
	{
		Mat src = imread("迪丽热巴.png",IMREAD_UNCHANGED);
		// Mat是一种特殊的数据类型格式,是一种二维数组,用来存储图片的数据
		// namedWindow("输出窗口", WINDOW_FREERATIO);
		// 只有imshow无法调整图片显示窗口的大小,通过namedWindow调整窗口的大小
		if (src.empty()) {
			cout << "没有找到你的图片" << endl;
			return -1;
		}
	
		imshow("输出窗口", src);
	
		demo d;
		d.histogram_2d_demo(src); 
		waitKey(0);// 设置图片显示时间
		destroyAllWindows();// 释放所有窗口
		return 0;
	}

3. Image histogram equalization

Image histogram equalization

Image histogram equalization can be used for image enhancement, histogram equalization processing for input images, and improving the accuracy of subsequent object detection. It is very common in the code demonstration of OpenCV face detection. In addition, for medical imaging images and satellite remote sensing images, histogram equalization is often used to improve image quality.

API

  • equalizeHist(src,dst)
	#include<opencv2/opencv.hpp>
	#include<iostream>
	#include<vector>
	
	using namespace cv;
	using namespace std;
	class demo
	{
	public:
		// 直方图均衡化
		void histogram_eq_demo(Mat& image)
		{
			Mat gray;
			cvtColor(image, gray, COLOR_BGR2GRAY);
			imshow("灰度图像", gray);
			Mat dst;
			equalizeHist(gray, dst);
			imshow("图像直方图均衡化演示", dst);
		}
	};
	int main(int argc, char** argv)
	{
		Mat src = imread("迪丽热巴.png",IMREAD_UNCHANGED);
		// Mat是一种特殊的数据类型格式,是一种二维数组,用来存储图片的数据
		// namedWindow("输出窗口", WINDOW_FREERATIO);
		// 只有imshow无法调整图片显示窗口的大小,通过namedWindow调整窗口的大小
		if (src.empty()) {
			cout << "没有找到你的图片" << endl;
			return -1;
		}
	
		imshow("输出窗口", src);
	
		demo d;
		d.histogram_eq_demo(src); 
		waitKey(0);// 设置图片显示时间
		destroyAllWindows();// 释放所有窗口
		return 0;
	}

4. Image convolution operation

Related APIs (C++)

- blur(

InputArray src, //input

OutputArray dst, //output

Size ksize, //window size

Point anchor = Point(-1,-1), //default value

int borderType = BORDER_ _DEFAULT //default value)

Python version

dst= cv.blur( src, ksize[, dst[, anchor[, borderTyel]]    )

	#include<opencv2/opencv.hpp>
	#include<iostream>
	#include<vector>
	
	using namespace cv;
	using namespace std;
	class demo
	{
	public:
		// 图像卷积操作
		void blur_demo(Mat& image)
		{
			Mat dst;
			blur(image, dst, Size(23,23), Point(-1, -1));
			// 支持一维卷积---->Size(15,1)
			imshow("图像模糊", dst);
		}
	};
	int main(int argc, char** argv)
	{
		Mat src = imread("迪丽热巴.png",IMREAD_UNCHANGED);
		// Mat是一种特殊的数据类型格式,是一种二维数组,用来存储图片的数据
		// namedWindow("输出窗口", WINDOW_FREERATIO);
		// 只有imshow无法调整图片显示窗口的大小,通过namedWindow调整窗口的大小
		if (src.empty()) {
			cout << "没有找到你的图片" << endl;
			return -1;
		}
	
		imshow("输出窗口", src);
	
		demo d;
		d.blur_demo(src); 
		waitKey(0);// 设置图片显示时间
		destroyAllWindows();// 释放所有窗口
		return 0;
	}

5. Gaussian Blur

Image averaging and Gaussian blur

Mean blur means that the coefficients of the convolution kernel are exactly the same. Gaussian blur takes into account the influence of the center pixel distance, and uses the Gaussian distribution formula to generate different weight coefficients for the convolution kernel, and then uses this convolution kernel to complete the image convolution. The output result is the output after Gaussian blurring of the image.

OpenCV Gaussian Blur API function

void GaussianBlur(

InputArray src,

OutputArray dst,

Size ksize, // Ksize is the Gaussian filter window size

double sigmax, //X direction filter coefficient

double sigmaY=O, // filter coefficient in Y direction

int borderType=BORDER_DEFAULT //Default edge interpolation method )

When Size(0,0) will start to calculate the Gaussian convolution kernel coefficient from sigmax, when the size is not zero, it is preferred to calculate the Gaussian convolution kernel coefficient from size

	#include<opencv2/opencv.hpp>
	#include<iostream>
	#include<vector>
	
	using namespace cv;
	using namespace std;
	class demo
	{
	public:
		// 高斯模糊
		void gaussian_blur_demo(Mat& image)
		{
			Mat dst;
			GaussianBlur(image, dst, Size(0, 0), 15);
			// Size(0, 0)是最厉害的模糊
			imshow("高斯模糊图像", dst);
		}
	};
	int main(int argc, char** argv)
	{
		Mat src = imread("迪丽热巴.png",IMREAD_UNCHANGED);
		// Mat是一种特殊的数据类型格式,是一种二维数组,用来存储图片的数据
		// namedWindow("输出窗口", WINDOW_FREERATIO);
		// 只有imshow无法调整图片显示窗口的大小,通过namedWindow调整窗口的大小
		if (src.empty()) {
			cout << "没有找到你的图片" << endl;
			return -1;
		}
	
		imshow("输出窗口", src);
	
		demo d;
		d.gaussian_blur_demo(src); 
		waitKey(0);// 设置图片显示时间
		destroyAllWindows();// 释放所有窗口
		return 0;
	}

6. Gaussian Bilateral Blur (Edge Filtering)

Edge Preserving Filtering Algorithm – Gaussian Bilateral Blur

The image convolution processing we introduced earlier, whether it is mean or Gaussian, is a fuzzy convolution. They all have a common feature that the edge information of the image no longer exists after blurring.

damaged. The filtering method we introduced today has the ability to blur the image through convolution processing without causing damage to the edge of the image. The output after filtering completely preserves the overall edge (contour) information of the image. We call this type of filtering algorithm edge preservation. Filter algorithm (EPF). The most common edge-preserving filtering algorithms are as follows

Gaussian bilateral blur

Meanshift fuzzy local mean square error fuzzy

There is also a dedicated API for edge-preserving filtering in OpenCV

Gaussian blur considers the influence of the image spatial position on the weight, but it does not consider the influence of the image pixel distribution on the image convolution output. The bilateral blur considers the influence of the pixel value distribution, and reserves the large difference in the pixel value spatial distribution to complete The edge information of the image is preserved.

C++:

bilateralFilter(

InputArray     src,

OutputArray     dst,

int d,

double sigmaColor,

double sigmaSpace,

int borderType = BORDER_DEFAULT

)

Python:

dst =cv.bilateralFilter(src,d, sigmaColor, sigmaSpace[, dst[,borderType]]  )

	#include<opencv2/opencv.hpp>
	#include<iostream>
	#include<vector>
	
	using namespace cv;
	using namespace std;
	class demo
	{
	public:
		// 高斯双边模糊
		void bifilter_demo(Mat& image)
		{
			// 磨皮美颜
			Mat dst;
			bilateralFilter(image, dst, 0, 100, 10);
			imshow("双边模糊", dst);
		}
	};
	int main(int argc, char** argv)
	{
		Mat src = imread("迪丽热巴.png",IMREAD_UNCHANGED);
		// Mat是一种特殊的数据类型格式,是一种二维数组,用来存储图片的数据
		// namedWindow("输出窗口", WINDOW_FREERATIO);
		// 只有imshow无法调整图片显示窗口的大小,通过namedWindow调整窗口的大小
		if (src.empty()) {
			cout << "没有找到你的图片" << endl;
			return -1;
		}
	
		imshow("输出窗口", src);
	
		demo d;
		d.bifilter_demo(src); 
		waitKey(0);// 设置图片显示时间
		destroyAllWindows();// 释放所有窗口
		return 0;
	}

Guess you like

Origin blog.csdn.net/hjl011006/article/details/127120523