第十一课 图像上采样与降采样
1.图像金字塔概念:能够保证图像特征不变
我们在图像处理时常常会调整图像的大小,最常见的就是放大(zoom in)和缩小(zoom out)
尽管几何变换也可以实现图像放大和缩小,此处介绍金字塔,一个图像金字塔由一系列的图像组成
最底下一张是图像尺寸最大,最上方的图像尺寸最小,从空间上从上到下像一个金字塔
高斯金字塔-对图像进行降采样
从底向上,逐层降采样得到
降采样后图像大小为M/2×N/2,就是对原图像删除偶数行和列,即得到降采样之后上一层的图像
步骤:对当前层高斯模糊,删除当前层的偶数行和列
拉普拉斯金字塔-用来重建一张图片根据他的上层降采样图片
高斯不同(DOG):把同一张图像在不同的参数下做高斯模糊之后的 结果相减,得到的图像
是图像内在特征,在灰度图像增强,角点检测中经常用到。
2.采样API
上采样(cv::pyrUp)--zoom in 放大
pyrUp(Mat src,Mat dst,Size(src.cols*2,src.rows*2));
降采样(cv::pyrDown)--zoom out 缩小
pyrDown(Mat src,Mat dst,Size(src.cols/2,src.rows/ 2));
3.代码演示
#include <opencv2/opencv.hpp>
#include <iostream>
#include "math.h"
using namespace cv;
int main(int agrc, char** argv) {
Mat src, dst;
src = imread("D:/vcprojects/images/cat.jpg");
if (!src.data) {
printf("could not load image...");
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);
// DOG
Mat gray_src, g1, g2, dogImg;
cvtColor(src, gray_src, CV_BGR2GRAY);
GaussianBlur(gray_src, g1, Size(5, 5), 0, 0);
GaussianBlur(g1, g2, Size(5, 5), 0, 0);
subtract(g1, g2, dogImg, Mat());
// 归一化显示
normalize(dogImg, dogImg, 255, 0, NORM_MINMAX);
imshow("DOG Image", dogImg);
waitKey(0);
return 0;
}
第十二课 基本阈值操作
1.图像阈值(threshold)
阈值就是图像分割的标尺
2.阈值类型
阈值二值化、反二值化、截断、阈值取零、阈值反取零
3.代码演示
#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("D:/vcprojects/images/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, 0, 255, THRESH_TRIANGLE | type_value);
imshow(output_title, dst);
}