The image pyramid
image pyramid concept:
we often adjust image processing, image size, the most common is enlarged (zoom in) and out (zoom out), although the set of transformation can be achieved image zoom in and out, but here we introduce image pyramid
a picture composed of a series of image pyramid, the bottom is a maximum image size minimum image size at the top, and from the space from top to bottom like an ancient pyramid
Gaussian pyramid --- used to image down sampling
a Gaussian pyramid from the bottom up, layer by layer down sampled
image size after down-sampling the original image of MxN M / 2 * N / 2, the source image is deleted columns and even rows, i.e., on images obtained after downsampling layer
gauss generating a two-step process pyramid
of Gaussian blur current layer
to delete the current even-numbered rows and columns layer
to obtain an image on the layer, such a layer compared with the next layer, which have only 1/4 size
14 641
1/16 16. 4 24. 4 16
. 6. 6 24 36 24
. 4 16. 4 24 16
14 641
Different Gaussian (Difference of Gaussian - DOG)
defines: an image is to do with the results after the Gaussian blur at different parameters subtraction, the output image obtained. Different called Gaussian (DOG)
Gaussian inherent feature of the image is different, frequently used in the gray scale image enhancement, the focus detection
Laplacian pyramid --- used to reconstruct an image based on its upper downsampling images
sampling related API
sampling (cv :: pyrUp) on - zoom in to enlarge
downsampling (cv :: pyrDown) - zoom out narrow
pyrUp ( Mat src, Mat dest, Size ( src.cols * 2, src.rows * 2) generated image) of each picture is enlarged two times in width and height
pyrDown (Mat src, Mat dest, Size (src.cols / 2, src.rows / 2)); an image is generated twice in each reduced picture width and height
Gaussian pyramid code demonstrates:
#include<opencv2/opencv.hpp>
#include<iostream>
#include"math.h"
using namespace cv;
int main(int argc,char** argv){
Mat src,dst;
src = imread("");
if(!src.data){
printf("could not load image...\n");
return -1;
}
char INPUT_WIN[] = "input image";
char OUTPUT_WIN[] = "output image";
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(OUTPUT_WIN,s_down);
//DOG 高斯不同
Mat g1,g2,gray_src,dogImg;
cvtColor(src,gray_src,CV_BGR2GRAY);
GaussianBlur(gray_src,g1,Size(3,3),0,0);
GaussianBlur(g1,g2,Size(3,3),0,0);
subtract(g1,g2,dogImg,Mat());
normalize(dogImg,dogImg,255,0,NORM_MINMAX);
imshow("DOG Image",dogImg);
waitKey(0);
return 0;
}
The basic operation of the threshold
image threshold value (threshold)
what thresholds are? Simply put, it is the image segmentation of the scale, the scale is based on what is produced, the threshold generation algorithm? Threshold type. (Binary segmentation)
--- threshold type binarization threshold value (threshold binary)
lower left figure shows the image pixel Src (x, y) value of the distribution, the horizontal line represents the threshold blue
dst(x,y) = MaxVal ifsrc(x,y)>thresh
= 0 otherwise
--- inverse binarization threshold value (threshold binary Inverted)
DST (X, Y) = 0 ifsrc (X, Y)> Thresh
= maxVal otherwise
---截断(truncate)
dst(x,y) = threshold ifsrc(x,y)>thresh
= src(x,y) othrewise
---阈值取零(threshold to zero)
dst(x,y) = src(x,y) ifsrc(x,y) > thresh
= 0 otherwise
---阈值反取零(threshold to zero inverted)
dst(x,y) = 0 ifsrc(x,y) > thresh
= src(x,y) otherwise
--- THRESH_BINARY binarization threshold
DST (X, Y) = MaxVal ifsrc (X, Y)> Thresh
= 0 otherwise
--- THRESH_BINARY_INV inverse binarization threshold value
dst (x, y) = 0 ifsrc (x, y) > Thresh
= maxVal otherwise
--- THRESH_TRUNC truncated
DST (X, Y) = threshold ifsrc (X, Y)> Thresh
= the src (X, Y) othrewise
--- THRESH_TOZERO take zero threshold
DST (X, Y) = the src (X, Y) ifsrc (X, Y)> Thresh
= 0 otherwise
--- THRESH_TOZERO_INV take zero threshold trans
dst (x, y) = 0 ifsrc (x , Y)> Thresh
= the src (X, Y) otherwise
---THRESH_MASK
---THRESH_OTSU flag,use Otsu algorithm to choose the optimal threshold value
---THRESH_TRIANGLE flag,use Triangle algorithm to choose the optimal threshold value
Code demonstrates:
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace cv;
Mat src,dst,gray_src;
int threshold_value = 127;
int threshold_max = 255;
int type_value = 2;
int type_max = 5;
const char* output_title = "binary image";
void Threshold_Demo(int,void*);
int main(int argc,char** argv){
src = imread("");
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);
while(true){
}
waitKey(0);
return -1;
}
void Threshold_Demo(int,void*){
cvtColor(src,gray_src,CV_BGR2GRAY);
printf("%d",THRESH_BINARY);
printf("%d",THRESH_BINARY_INY);
printf("%d",THRESH_TRUNC);
printf("%d",THRESH_THZERO);
printf("%d",THRESH_TOZERO_INY);
threshold(gray_src,dst,threshold_value,threshold_max,THRESH_BINARY);
//threshold(gray_src,dst,threshold_value,threshold_max,type_value);
//threshold(gray_src,dst,0,255,THRESH_OTSU | type_value);//自动计算阈值
//threshold(gray_src,dst,0,255,THRESH_TRIANGLE | type_value);//三角法找阈值
imshow(output_title,dst);
}
Custom linear filtering
convolution concept of
convolution is an image processing operation, the operating kernel on each pixel of the image of
a fixed size matrix array essentially the kernel, its center point is called the anchor (anchor point)
How convolution work
put into the kernel over the pixel array, seeking to cover the periphery and the sum of products of pixel anchor (anchor point), to replace the pixel value of the anchor coverage process called convolution. Mathematically expressed as follows:
H (X, Y) = exp (I = 0, Mi-. 1) [exp (J = 0, Mj of-. 1) [the I (X + I-AI, Y + J-AJ) K (I , j)]]
Convolution effect
blurred image
edge extraction
for image sharpening
common operator
Robert operator
x direction, the y-direction
+ 1'd + 1'd 0 0
0 -1 -1 0
the Sobel operator
x direction, the y direction
-101-1-2-- . 1
-2 0 2 0 0 0
-1 0 2. 1. 1. 1
Laplacian
0 -1 0
-1 -1. 4
0 -1 0
custom convolutional Fuzzy
filter2D method
filter2D (
Mat the src, // input image
Mat dst, // blurred image
int depth, // image depth 32/8
Mat kernel, // convolution kernel / template
Point anchor, // anchor position of
pixels + delta double delta // calculated
convolution kernel) which can be customized kernel
codes demo:
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace cv;
int main(int argc,char** argv){
Mat src,dest;
Mat kernel;
int ksize = 0;
src = imread("");
if(!src.data){
printf("could not load image...\n");
return -1;
}
char INPUT_WIN[] = "input image";
char OUTPUT_WIN[] = "result image";
namedWindow(INPUT_WIN,CV_WINDOW_AUTOSIZE);
namedWindow(OUTPUT_WIN,CV_WINDOW_AUTOSIZE);
imshow(INPUT_WIN,src);
//Robert算子 x方向
Mat kernel_x = (Mat_<int>(2,2)<<1,0,0,-1);
filter2D(src,dst,-1,kernel_x,Point(-1,-1),0.0);
//Robert算子 y方向
Mat kernel_y = (Mat_<int>(2,2)<<0,1,-1,0);
filter2D(src,dst,-1,kernel_y,Point(-1,-1),0.0);
waitKey(0);
return 0;
}