一、放大與縮小
代碼:
#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)
最終得到的值替換所選的值。