1.图像的放缩与插值
void resize_demo ( )
{
Mat zoomin, zoomout;
int h = image. rows;
int w = image. cols;
resize ( image, zoomin, Size ( w/ 2 , h/ 2 ) , 0 , 0 , INTER_LINEAR) ;
imshow ( "zoomin" , zoomin) ;
resize ( image, zoomout, Size ( w* 1.5 , h* 1.5 ) , 0 , 0 , INTER_LINEAR) ;
imshow ( "zoomout" , zoomout) ;
}
void norm_demo ( Mat & image)
{
Mat dst;
cout<< image. type ( ) << endl;
image. convertTo ( image, CV_32F) ;
cout<< image. type ( ) << endl;
normalize ( image, dst, 1.0 , 0 , NORM_MINMAx) ;
cout<< dst. type ( ) << endl;
imshow ( "图像数据归一化" , dst) ;
}
void Histogram_demo ( Mat & image)
{
vector< Mat> bgr_plane;
split ( imag, bgr_plane) ;
const int channels[ 1 ] = { 0 } ;
const int bins[ 1 ] = { 256 } ;
const float * ranges[ 1 ] = { hranges} ;
Mat b_hist;
Mat g_hist;
Mat r_hist;
calcHist ( & bgr_plane[ 0 ] , 0 , Mat ( ) , b_hist, 1 , bins, ranges) ;
calcHist ( & bgr_plane[ 1 ] , 0 , Mat ( ) , g_hist, 1 , bins, ranges) ;
calcHist ( & bgr_plane[ 2 ] , 0 , Mat ( ) , r_hist, 1 , bins, ranges) ;
int hist_w = 512 ;
int hist_h = 400 ;
int bin_w = cvRound ( double ) hist_w/ bins[ 0 ] ;
Mat hisImage = Mat :: zeros ( hist_h, hist_w, CV_8UC3) ;
normalize ( b_hist, b_hist,0 , hisImage. rows, NORM_MINMAx, - 1 , Mat ( ) ) ;
normalize ( g_hist, g_hist,0 , hisImage. rows, NORM_MINMAx, - 1 , Mat ( ) ) ;
normalize ( r_hist, r_hist,0 , hisImage. rows, NORM_MINMAx, - 1 , Mat ( ) ) ;
for ( int i= 1 ; i< bins[ 0 ] ; i++ )
{
line ( hisImage, Point ( bin_w* ( i- 1 ) , hist_h - cvRound ( b_hist. at < float > ( i- 1 ) ) ) , Point ( bin_w* ( i) , hist_h- cvRound ( b_hist. at < float > ( i) ) ) , Scalar ( 255 , 0 , 0 ) , 2 , 8 , 0 ) ;
line ( hisImage, Point ( bin_w* ( i- 1 ) , hist_h - cvRound ( g_hist. at < float > ( i- 1 ) ) ) , Point ( bin_w* ( i) , hist_h- cvRound ( b_hist. at < float > ( i) ) ) , Scalar ( 0 , 255 , 0 ) , 2 , 8 , 0 ) ;
line ( hisImage, Point ( bin_w* ( i- 1 ) , hist_h - cvRound ( r_hist. at < float > ( i- 1 ) ) ) , Point ( bin_w* ( i) , hist_h- cvRound ( b_hist. at < float > ( i) ) ) , Scalar ( 0 , 0 , 255 ) , 2 , 8 , 0 ) ;
}
nameWindow ( "Histogram demo" , WINDOWS_AUTOSIZE) ;
imshow ( "Histogram demo" , hisImage) ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
4.图像卷积
void blur_demo ( Mat & image)
{
Mat dst;
blur ( image, dst, Size ( 15 , 1 ) , Point ( - 1 , - 1 ) ) ;
imshow ( "图像模糊" , dst) ;
}
void gaussian_blur_demo ( Mat & image)
{
Mat dst;
GaussianBlur ( image, dst, Size ( 5 , 5 ) , 15 ) ;
imshow ( "高斯模糊" , dst) ;
}