OpenCV學習筆記2

1. 圖像腐蝕與膨脹

    這兩個操作都是形態學的變化,將圖像與任意的卷積核進行卷積,通常我們使用的卷積核爲正方形或者圓形。

    同時在內核中我們也要設置一個錨點,一般爲內核的中心點。

    進行腐蝕操作時,將內核 B 劃過圖像,將內核 B 覆蓋區域的最小相素值提取,並代替錨點位置的相素。

    進行膨脹操作時,將內核 B 劃過圖像,將內核 B 覆蓋區域的最大相素值提取,並代替錨點位置的相素。顯然,這一最大化操作將會導致圖像中的亮區開始”擴展” (因此有了術語膨脹 dilation )。

    1.1卷積核

    因此我們在OpenCV·中使用erode和dilate時要首先設置一個卷積核,我們使用getStructuringElement函數。

Mat getStructuringElement(int shape, Size esize, Point anchor=Point(-1, -1))

Returns the structuring element of the specified size and shape for morphological operations

  第一個參數是卷積核的形狀,有三個選擇MORPH_RECT、MORPH_ELLIPSE、MORPH_CROSS。

   第二個參數是卷積核的大小,用size()函數設置。

    第三個參數是卷積核的錨點,default的point爲point(-1,-1).

    1.2 圖像的腐蝕

        使用erode()函數,第一個參數爲原圖像,第二個參數爲腐蝕之後存入的Mat,第三個參數爲卷積核即我們之前從getStructuringElement返回的值。

            圖像腐蝕的作用:

                邊緣檢測、形態骨架的提取、噪聲的消除

     1.3 圖像的膨脹

            使用dilate()函數,參數和erode()的參數設計是一樣的。

#include<opencv.hpp>
using namespace cv;
int main(){
	Mat img = imread("1.jpg");
	//imshow(" ",img);


	Mat element = getStructuringElement(MORPH_RECT, Size(6, 6));
	Mat dis;
	erode(img, dis, element);
	imshow("", dis);
	dilate(dis, dis, element);
	imshow("dilate", dis);
	waitKey(6000);

}


2. 圖像模糊濾波

    一般來說圖像中,噪聲表現爲高頻的部分;爲了消除圖像的噪聲,我們可以使用卷積覈對圖像進行處理,卷積的過程表現爲使用一個矩陣對圖像的矩陣進行卷積運算,可參照下圖。

常用的濾波器有均值濾波器,高斯濾波器等。


#include<opencv.hpp>

using namespace cv;
int main(){
	Mat img = imread("1.jpg");
	imshow(" ",img);

	Mat element = getStructuringElement(MORPH_RECT, Size(6, 6));
	Mat dis;
	blur(img, dis, Size(5, 5));
	imshow("blur", dis);
	GaussianBlur(img, dis, Size(5, 5),0,0);
	imshow("Gaussion", dis);

	waitKey(6000);
}

3. canny邊緣檢測

先看下文檔中的介紹:

void Canny(const Mat& imageMat& edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false)

Finds edges in an image using Canny algorithm.

Parameters:
  • image – Single-channel 8-bit input image
  • edges – The output edge map. It will have the same size and the same type as image
  • threshold1 – The first threshold for the hysteresis procedure
  • threshold2 – The second threshold for the hysteresis procedure
  • apertureSize – Aperture size for the Sobel()operator
  • L2gradient – Indicates, whether the more accurateL_2 norm =\sqrt{(dI/dx)^2 + (dI/dy)^2} should be used to compute the image gradient magnitude ( L2gradient=true ), or a faster default L_1 norm=|dI/dx|+|dI/dy| is enough ( L2gradient=false )

#include<opencv.hpp>

using namespace cv;
int main(){
	Mat img = imread("1.jpg");

	Mat dstImage, edge, grayImage;
	cvtColor(img, grayImage, COLOR_RGB2GRAY);
	//imshow("", grayImage);
	blur(grayImage, edge,Size(3,3));
	Canny(edge, edge, 3, 9, 3);
	imshow("canny", edge);
	waitKey(6000);
}

4.將canny運用於視頻圖像上

#include<opencv.hpp>

using namespace cv;
int main(){
	VideoCapture cap(0);
	Mat edges;
	if (!cap.isOpened())  // check if we succeeded
		return -1;
	while (1){
		Mat frame;
		cap >> frame;
		cvtColor(frame, frame, COLOR_RGB2GRAY);
		blur(frame, frame, Size(3, 3));
		Canny(frame, frame, 0, 30, 3);
		imshow("", frame);
		if (waitKey(30) >= 0)break;
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章