關(guān)于邊緣檢測(cè)的基礎(chǔ)來自于一個(gè)事實(shí),,即在邊緣部分,,像素值出現(xiàn)”跳躍“或者較大的變化,。如果在此邊緣部分求取一階導(dǎo)數(shù),就會(huì)看到極值的出現(xiàn),。
而在一階導(dǎo)數(shù)為極值的地方,,二階導(dǎo)數(shù)為0,基于這個(gè)原理,,就可以進(jìn)行邊緣檢測(cè),。
關(guān)于 Laplace 算法原理,可參考
0x01. Laplace 算法
下面的代碼展示了分別對(duì)灰度化的圖像和原始彩色圖像中的邊緣進(jìn)行檢測(cè):
im=cv.LoadImage('img/building.png', cv.CV_LOAD_IMAGE_COLOR) gray = cv.CreateImage(cv.GetSize(im), 8, 1) cv.CvtColor(im, gray, cv.CV_BGR2GRAY) dst = cv.CreateImage(cv.GetSize(gray), cv.IPL_DEPTH_32F, 1) cv.Laplace(gray, dst,aperture) thresholded = cv.CloneImage(im) cv.Threshold(im, thresholded, 50, 255, cv.CV_THRESH_BINARY_INV) cv.ShowImage('Laplaced grayscale',gray) planes = [cv.CreateImage(cv.GetSize(im), 8, 1) for i in range(3)] laplace = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_16S, 1) colorlaplace = cv.CreateImage(cv.GetSize(im), 8, 3) cv.Split(im, planes[0], planes[1], planes[2], None) cv.Laplace(plane, laplace, 3) cv.ConvertScaleAbs(laplace, plane, 1, 0) cv.Merge(planes[0], planes[1], planes[2], None, colorlaplace) cv.ShowImage('Laplace Color', colorlaplace)
效果展示
原圖
灰度化圖片檢測(cè)
原始彩色圖片檢測(cè)
0x02. Sobel 算法
Sobel 也是很常用的一種輪廓識(shí)別的算法,。
關(guān)于 Sobel 導(dǎo)數(shù)原理的介紹,,可參考
以下是使用 Sobel 算法進(jìn)行輪廓檢測(cè)的代碼和效果
im=cv.LoadImage('img/building.png', cv.CV_LOAD_IMAGE_GRAYSCALE) sobx = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_16S, 1) cv.Sobel(im, sobx, 1, 0, 3) soby = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_16S, 1) cv.Sobel(im, soby, 0, 1, 3) result = cv.CloneImage(im) cv.Add(sobx, soby, result) cv.Threshold(result, result, 100, 255, cv.CV_THRESH_BINARY_INV) cv.ShowImage('Image', im) cv.ShowImage('Result', result)
處理之后效果圖(感覺比Laplace效果要好些)
0x03. cv.MorphologyEx
cv.MorphologyEx 是另外一種邊緣檢測(cè)的算法
image=cv.LoadImage('img/build.png', cv.CV_LOAD_IMAGE_GRAYSCALE) morphed = cv.CloneImage(image) cv.MorphologyEx(image, morphed, None, None, cv.CV_MOP_GRADIENT) cv.Threshold(morphed, morphed, 30, 255, cv.CV_THRESH_BINARY_INV) cv.ShowImage('Image', image) cv.ShowImage('Morphed', morphed)
0x04. Canny 邊緣檢測(cè)
Canny 算法可以對(duì)直線邊界做出很好的檢測(cè);
關(guān)于 Canny 算法原理的描述,,可參考:
im=cv.LoadImage('img/road.png', cv.CV_LOAD_IMAGE_GRAYSCALE) dst = cv.CreateImage(cv.GetSize(im), 8, 1) cv.Canny(im, dst, 200, 200) cv.Threshold(dst, dst, 100, 255, cv.CV_THRESH_BINARY) color_dst_standard = cv.CreateImage(cv.GetSize(im), 8, 3) cv.CvtColor(im, color_dst_standard, cv.CV_GRAY2BGR) lines = cv.HoughLines2(dst, cv.CreateMemStorage(0), cv.CV_HOUGH_STANDARD, 1, pi / 180, 100, 0, 0) for (rho, theta) in lines[:100]: pt1 = (cv.Round(x0 1000*(-b)), cv.Round(y0 1000*(a))) pt2 = (cv.Round(x0 - 1000*(-b)), cv.Round(y0 - 1000*(a))) cv.Line(color_dst_standard, pt1, pt2, cv.CV_RGB(255, 0, 0), 2, 4) color_dst_proba = cv.CreateImage(cv.GetSize(im), 8, 3) cv.CvtColor(im, color_dst_proba, cv.CV_GRAY2BGR) lines = cv.HoughLines2(dst, cv.CreateMemStorage(0), cv.CV_HOUGH_PROBABILISTIC, rho, theta, thresh, minLength, maxGap) cv.Line(color_dst_proba, line[0], line[1], cv.CV_RGB(255, 0, 0), 2, 8) cv.ShowImage('Cannied', dst) cv.ShowImage('Hough Standard', color_dst_standard) cv.ShowImage('Hough Probabilistic', color_dst_proba)
原圖
使用 Canny 算法處理之后
標(biāo)記出標(biāo)準(zhǔn)的直線
標(biāo)記出所有可能的直線
0x05. 輪廓檢測(cè)
OpenCV 提供一個(gè) FindContours 函數(shù)可以用來檢測(cè)出圖像中對(duì)象的輪廓:
orig = cv.LoadImage('img/build.png', cv.CV_LOAD_IMAGE_COLOR) im = cv.CreateImage(cv.GetSize(orig), 8, 1) cv.CvtColor(orig, im, cv.CV_BGR2GRAY) cv.Threshold(im, im, 128, 255, cv.CV_THRESH_BINARY) cv.ShowImage('Threshold 1', im) element = cv.CreateStructuringElementEx(5*2 1, 5*2 1, 5, 5, cv.CV_SHAPE_RECT) cv.MorphologyEx(im, im, None, element, cv.CV_MOP_OPEN) cv.MorphologyEx(im, im, None, element, cv.CV_MOP_CLOSE) cv.Threshold(im, im, 128, 255, cv.CV_THRESH_BINARY_INV) cv.ShowImage('After MorphologyEx', im) contours=cv.FindContours(vals, cv.CreateMemStorage(0), cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_SIMPLE, (0,0)) cv.DrawContours (orig, contours, _red, _green, levels, 2, cv.CV_FILLED) cv.ShowImage('Image', orig)
效果圖:
原圖
識(shí)別結(jié)果
0x06. 邊界檢測(cè)
im = cv.LoadImage('img/build.png', cv.CV_LOAD_IMAGE_GRAYSCALE) dst_32f = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_32F, 1) cv.CornerHarris(im, dst_32f, neighbourhood, aperture, k) minv, maxv, minl, maxl = cv.MinMaxLoc(dst_32f) dilated = cv.CloneImage(dst_32f) cv.Dilate(dst_32f, dilated) localMax = cv.CreateMat(dst_32f.height, dst_32f.width, cv.CV_8U) cv.Cmp(dst_32f, dilated, localMax, cv.CV_CMP_EQ) cv.Threshold(dst_32f, dst_32f, threshold, 255, cv.CV_THRESH_BINARY) cornerMap = cv.CreateMat(dst_32f.height, dst_32f.width, cv.CV_8U) cv.Convert(dst_32f, cornerMap) cv.And(cornerMap, localMax, cornerMap) for x in range(cornerMap.height): for y in range(cornerMap.width): cv.Circle(im, center, radius, (255,255,255), thickness) cv.ShowImage('Image', im) cv.ShowImage('CornerHarris Result', dst_32f) cv.ShowImage('Unique Points after Dilatation/CMP/And', cornerMap)
|