OpenCV C++开发 第二节:图像处理(七、放大与缩小、阀值操作、自定义线性滤波)

一、放大与缩小

代码:

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
int main(int argc, char** argv) {
	Mat src, dst;
	src = imread("C:\\Users\\Administrator\\Desktop\\test.png");
	if (!src.data) {
		printf("could not load image...\n");
		return -1;
	}
	char INPUT_WIN[] = "input image";
	char OUTPUT_WIN[] = "sample up";
	namedWindow(INPUT_WIN, CV_WINDOW_AUTOSIZE);
	namedWindow(OUTPUT_WIN, CV_WINDOW_AUTOSIZE);
	imshow(INPUT_WIN, src);

	// 上采样,放大
	pyrUp(src, dst, Size(src.cols * 2, src.rows * 2));
	imshow(OUTPUT_WIN, dst);

	// 降采样,缩小
	Mat s_down;
	pyrDown(src, s_down, Size(src.cols / 2, src.rows / 2));
	imshow("sample down", s_down);
	waitKey(0);
	return 0;
}

以上代码中主要的几个知识点解释下:

1.pyrUp(src, dst, Size(src.cols * 2, src.rows * 2));

放大操作,即上采样,Size是宽高放大倍数。

2.pyrDown(src, s_down, Size(src.cols / 2, src.rows / 2));

缩小操作,即降采样,Size是宽高缩小倍数。

效果如下图:

二、阀值操作

代码:

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

using namespace cv;
Mat src, gray_src, dst;
int threshold_value = 127;
int threshold_max = 255;
int type_value = 2;
int type_max = 4;
const char* output_title = "binary image";
void Threshold_Demo(int, void*);
int main(int argc, char** argv) {
	src = imread("C:\\Users\\Administrator\\Desktop\\test.png");
	if (!src.data) {
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	namedWindow(output_title, CV_WINDOW_AUTOSIZE);
	imshow("input image", src);

	createTrackbar("Threshold Value:", output_title, &threshold_value, threshold_max, Threshold_Demo);
	createTrackbar("Type Value:", output_title, &type_value, type_max, Threshold_Demo);
	Threshold_Demo(0, 0);

	waitKey(0);
	return 0;
}

void Threshold_Demo(int, void*) {
	cvtColor(src, gray_src, CV_BGR2GRAY);
	threshold(src, dst, threshold_value, 255, THRESH_BINARY|type_value);//阀值操作
	imshow(output_title, dst);
}

以上代码中主要的几个知识点解释下:

1.threshold(src, dst, threshold_value, 255, THRESH_BINARY|type_value);

这里是阀值操作的主要代码。threshold_value这里是可调的阀值大小,255是最大的阀值。最后一个参数是阀值操作类型

(1)THRESH_BINARY

如下图,参考上面的表,蓝色为阀值。若大于蓝色线则取最大值。若小于蓝色线取0。

      ------参考上表,---->    

 

(2)THRESH_BINARY_INV

如下图,参考上面的表,蓝色为阀值。若大于蓝色线则取0。若小于蓝色线取最大值。

------参考上表,---->

(3)THRESH_TRUNC

如下图,参考上面的表,蓝色为阀值。若大于蓝色线则取阀值。若小于蓝色线取原来的值。

------参考上表,---->

(4)THRESH_TOZERO

如下图,参考上面的表,蓝色为阀值。若大于蓝色线则取原来的值。若小于蓝色线取0。

------参考上表,---->

(5)THRESH_TOZERO_INV

如下图,参考上面的表,蓝色为阀值。若大于蓝色线则取0。若小于蓝色线取原来的值。

------参考上表,---->

三、自定义线性滤波

代码:

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

using namespace cv;
int main(int argc, char** argv) {
	Mat src, dst;
	int ksize = 0;
	src = imread("C:\\Users\\Administrator\\Desktop\\test.png");
	if (!src.data) {
		printf("could not load image...\n");
		return -1;
	}

	char INPUT_WIN[] = "input image";
	char OUTPUT_WIN[] = "Custom Blur Filter Result";
	namedWindow(INPUT_WIN, CV_WINDOW_AUTOSIZE);
	namedWindow(OUTPUT_WIN, CV_WINDOW_AUTOSIZE);

	imshow(INPUT_WIN, src);

	// Sobel X 方向
	// Mat kernel_x = (Mat_<int>(3, 3) << -1, 0, 1, -2,0,2,-1,0,1);
	// filter2D(src, dst, -1, kernel_x, Point(-1, -1), 0.0);

	// Sobel Y 方向
	// Mat yimg;
	// Mat kernel_y = (Mat_<int>(3, 3) << -1, -2, -1, 0,0,0, 1,2,1);
	// filter2D(src, yimg, -1, kernel_y, Point(-1, -1), 0.0);

	// 拉普拉斯算子
	//Mat kernel_y = (Mat_<int>(3, 3) << 0, -1, 0, -1, 4, -1, 0, -1, 0);
	//filter2D(src, dst, -1, kernel_y, Point(-1, -1), 0.0);
	int c = 0;
	int index = 0;
	while (true) {
		c = waitKey(500);
		if ((char)c == 27) {// ESC 
			break;
		}
		ksize = 5 + (index % 8) * 2;
		Mat kernel = Mat::ones(Size(ksize, ksize), CV_32F) / (float)(ksize * ksize);
		filter2D(src, dst, -1, kernel, Point(-1, -1));
		index++;
		imshow(OUTPUT_WIN, dst);
	}

	// imshow("Sobel Y", yimg);
	return 0;
}

其实之前的文章里有用到自定义线性滤波的功能,就是掩膜操作。

以上代码中主要的几个知识点解释下:

1.卷积是图像处理中一个操作,是kernel在图像的每个像素上的操作。

2.Mat kernel_x = (Mat_<int>(3, 3) << -1, 0, 1, -2,0,2,-1,0,1);

不同的算法有着不同的效果。

如下图:

这是所有的算子都需要经过的运算:

Sum = 8x1+6x1+6x1+2x1+8x1+6x1+2x1+2x1+8x1

New pixel = sum / (m*n)

最终得到的值替换所选的值。

 

 

发布了15 篇原创文章 · 获赞 8 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章