【OpenCV_09】平滑/模糊圖片 Smooth / Blur Images及 彩色圖轉 灰度圖和二值化
阿新 • • 發佈:2019-02-09
OpenCV 程式碼
下面的OpenCV 的程式碼中,核大小從1x1增加到29x29.可以觀察到影象平滑程度隨著核增大的變化程度。平滑影象的核的大小顯示在被平滑影象上。
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" using namespace cv; int main( int argc, char** argv ) { //create 2 empty windows namedWindow( "Original Image" , CV_WINDOW_AUTOSIZE ); namedWindow( "Smoothed Image" , CV_WINDOW_AUTOSIZE ); // Load an image from file Mat src = imread( "MyPic.JPG", 1 ); //show the loaded image imshow( "Original Image", src ); Mat dst; char zBuffer[35]; for ( int i = 1; i < 31; i = i + 2 ) { //copy the text to the "zBuffer" _snprintf_s(zBuffer, 35,"Kernel Size : %d x %d", i, i); //smooth the image in the "src" and save it to "dst" blur( src, dst, Size( i, i ) ); //put the text in the "zBuffer" to the "dst" image putText( dst, zBuffer, Point( src.cols/4, src.rows/8), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255) ); //show the blurred image with the text imshow( "Smoothed Image", dst ); //wait for 2 seconds int c = waitKey(2000); //if the "esc" key is pressed during the wait, return if (c == 27) { return 0; } } //make the "dst" image, black dst = Mat::zeros( src.size(), src.type() ); //copy the text to the "zBuffer" _snprintf_s(zBuffer, 35,"Press Any Key to Exit"); //put the text in the "zBuffer" to the "dst" image putText( dst, zBuffer, Point( src.cols/4, src.rows / 2), CV_FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255) ); //show the black image with the text imshow( "Smoothed Image", dst ); //wait for a key press infinitely waitKey(0); return 0; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////