学习OpenCV3----几个简单的图像处理示例


1.   读入图像后,可以对图像进行处理,介绍了利用高斯滤波进行简单的图像处理,显示了高斯滤波前后的图像变换,GaussianBlur()函数原型:

  voidGaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, doublesigmaY=0, int borderType=BORDER_DEFAULT);

参数详解如下:

    src,输入图像,即源图像,填Mat类的对象即可。它可以是单独的任意通道数的图片,但需要注意,图片深度应该为CV_8U,CV_16U, CV_16S, CV_32F 以及 CV_64F之一。

    dst,即目标图像,需要和源图片有一样的尺寸和类型。比如可以用Mat::Clone,以源图片为模板,来初始化得到如假包换的目标图。

    ksize,高斯内核的大小。其中ksize.width和ksize.height可以不同,但他们都必须为正数和奇数(并不能理解)。或者,它们可以是零的,它们都是由sigma计算而来。

    sigmaX,表示高斯核函数在X方向的的标准偏差。

    sigmaY,表示高斯核函数在Y方向的的标准偏差。若sigmaY为零,就将它设为sigmaX,如果sigmaX和sigmaY都是0,那么就由ksize.width和ksize.height计算出来。

    为了结果的正确性着想,最好是把第三个参数Size,第四个参数sigmaX和第五个参数sigmaY全部指定到。

    borderType,用于推断图像外部像素的某种边界模式。注意它有默认值BORDER_DEFAULT。

int main( int argc, char** argv ) {

    // Load an image specified onthe command line.

    cv::Mat image = cv::imread(argv[1],-1);

  //Create some windows to show the input

  // andoutput images in.

    cv::namedWindow( "Example 2-5-in",cv::WINDOW_AUTOSIZE );

  cv::namedWindow( "Example 2-5-out", cv::WINDOW_AUTOSIZE );

  //Create a window to show our input image

    cv::imshow( "Example2-5-in", image );

  //Create an image to hold the smoothed output

    cv::Mat out;

  // Dothe smoothing

  // (Note: Could use GaussianBlur(), blur(), medianBlur() or

  //bilateralFilter(). )

  cv::GaussianBlur( image, out, cv::Size(5,5),3, 3);

  cv::GaussianBlur( out, out, cv::Size(5,5), 3,3);

  // Showthe smoothed image in the output window

  cv::imshow( "Example2-5-out", out );

  // Waitfor the user to hit a key, windows will self destruct

  cv::waitKey( 0 );

}

2.  显示了利用图像金字塔pyrDown()对图像直接进行1/2的缩小的结果。pyrDown()能对图像进行快速缩小,每次图像的大小为原来图像的1/2

int main( int argc, char** argv ) {

cv::Mat img1,img2;

  cv::namedWindow( "Example 2-6-in",cv::WINDOW_AUTOSIZE );

  cv::namedWindow( "Example 2-6-out", cv::WINDOW_AUTOSIZE );

  img1 = cv::imread( argv[1] );

  cv::imshow( "Example2-6-in", img1 );

  cv::pyrDown( img1, img2);

  cv::imshow( "Example2-6-out", img2 );

  cv::waitKey(0);

  return 0;

};

3.   利用cvtColor()将图像在各种颜色空间上进行转换,本例直接将彩色图像转换为灰度图像

int main( int argc, char** argv ) {

 cv::Mat img_rgb, img_gry, img_cny;

  cv::namedWindow( "Example Gray",cv::WINDOW_AUTOSIZE );

  cv::namedWindow( "Example Canny",cv::WINDOW_AUTOSIZE );

  img_rgb = cv::imread( argv[1] );

  cv::cvtColor( img_rgb, img_gry,cv::COLOR_BGR2GRAY);

  cv::imshow( "ExampleGray", img_gry );

  cv::Canny( img_gry, img_cny, 10, 100, 3, true );

  cv::imshow( "ExampleCanny", img_cny );

  cv::waitKey(0);

}

源码下载: https://download.csdn.net/download/mr_liyonghong/10339021

猜你喜欢

转载自blog.csdn.net/mr_liyonghong/article/details/79890507
今日推荐