前言
这是我《OpenCV:从零到一》专栏的第十二篇博客,想看跟多请戳这。
本文概要
上采样(cv::pyrUp) – zoom in 放大
降采样 (cv::pyrDown) – zoom out 缩小
subtract
归一化 normalize
案例代码
大概内容:上采样、降采样、归一化 。
#include <opencv2/opencv.hpp>
#include <iostream>
#include "math.h"
using namespace cv;
int main(int agrc, char** argv) {
Mat src, dst;
src = imread("D:\\86186\\Documents\\opencv\\lena.jpg");
if (!src.data) {
printf("could not load image...");
return -1;
}
char INPUT_WIN[] = "input image";
char OUTPUT_WIN[] = "sample up";
namedWindow(INPUT_WIN, WINDOW_AUTOSIZE);
namedWindow(OUTPUT_WIN, 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, COLOR_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;
}
运行效果:
解析及注意事项
- 我们在图像处理中常常会调整图像大小,最常见的就是放大(zoom in)和缩小(zoom out),尽管几何变换也可以实现图像放大和缩小,但是这里我们介绍图像金字塔
- 一个图像金字塔式一系列的图像组成,最底下一张是图像尺寸最大,最上方的图像尺寸最小,从空间上从上向下看就想一个古代的金字塔。
- pyrUp(Mat src, Mat dst) 生成的图像是原图在宽与高各放大两倍
- pyrDown(Mat src, Mat dst)生成的图像是原图在宽与高各缩小1/2
- 之前有介绍过multiply,现在又有一个subtract,两个都是四则运算,很相像,但是这个函数对于不同图片处理的方式不同,具体如下
- 归一化显示图片可以用normalize(事实上不仅仅可以用来归一化)其中要注意一点的是Saturation is not applied when the output array has the depth CV_32S. You may even get result of an incorrect sign in the case of overflow. 深度为CV_32S的图片不调用saturate函数,溢出会得到错误的结果。标准化类型有如下几种(不止)
全注释代码
#include <opencv2/opencv.hpp>
#include <iostream>
#include "math.h"
using namespace cv;
int main(int agrc, char** argv) {
Mat src, dst;
src = imread("D:\\86186\\Documents\\opencv\\lena.jpg");
if (!src.data) {
printf("could not load image...");
return -1;
}
char INPUT_WIN[] = "input image";
char OUTPUT_WIN[] = "sample up";
namedWindow(INPUT_WIN, WINDOW_AUTOSIZE);
namedWindow(OUTPUT_WIN, WINDOW_AUTOSIZE);
imshow(INPUT_WIN, src);
// 上采样
pyrUp(src, dst, Size(src.cols * 2 , src.rows * 2));
//pyrUp(src, dst, dst的大小,boderType);Size大小限制如下
//|dstsize.width−src.cols∗2 | ≤(dstsize.width mod 2) 和 |dstsize.height−src.rows∗2 |≤(dstsize.height mod 2)
imshow(OUTPUT_WIN, dst);
// 降采样
Mat s_down;
pyrDown(src, s_down, Size(src.cols / 2 + 1, src.rows / 2));
//和pyrUp一模一样,Size的大小一定要满足|dstsize.width∗2−src.cols|≤2 和 |dstsize.height∗2−src.rows|≤2
//也就是说Size大小只能在两倍左右,否则会报错。宽和高不一定要一样
//这两个函数的Size的参数都有默认值,也就是我们上面填的值
imshow("sample down", s_down);
Mat gray_src, g1, g2, dogImg;
cvtColor(src, gray_src, COLOR_BGR2GRAY);
GaussianBlur(gray_src, g1, Size(5, 5), 0, 0);//高斯模糊(原图,目标,核大小,sigemaX,sigemaY)
GaussianBlur(g1, g2, Size(5, 5), 0, 0);
subtract(g1, g2, dogImg, Mat());//Calculates the per-element difference between two arrays or array and a scalar.
/*运算关系有些复杂,但是大体上是src1-src2
InputArray src1,
InputArray src2,
OutputArray dst,
InputArray mask = noArray(),//可选操作掩码; 这是一个8位单通道数组,用于指定要更改的输出数组的元素。
int dtype = -1 //输出图像的深度,-1为默认
Saturation is not applied when the output array has the depth CV_32S. You may even get result of an incorrect sign in the case of overflow.
深度为CV_32S的图片不调用saturate函数,溢出会得到错误的结果。
*/
normalize(dogImg, dogImg, 255, 0, NORM_MINMAX);// 归一化显示
/*
InputArray src,
InputOutputArray dst,
double alpha = 1,//下界
double beta = 0,//上界
int norm_type = NORM_L2,
int dtype = -1,
InputArray mask = noArray()
*/
imshow("dstImage", dogImg);
waitKey(0);
return 0;
}
翻译笔记
sample n.样品;
norm 标准;规范