Python-OpenCV 处理图像(五):图像中边界和轮廓检测

系列文章目录

关于边缘检测的基础来自于一个事实,即在边缘部分,像素值出现”跳跃“或者较大的变化。如果在此边缘部分求取一阶导数,就会看到极值的出现。

而在一阶导数为极值的地方,二阶导数为0,基于这个原理,就可以进行边缘检测。

关于 Laplace 算法原理,可参考

  • Laplace 算子

0x01. Laplace 算法

下面的代码展示了分别对灰度化的图像和原始彩色图像中的边缘进行检测:

12345678910111213141516171819202122232425262728293031323334353637 import cv2.cv as cv im=cv.LoadImage(\’img/building.png\’, cv.CV_LOAD_IMAGE_COLOR) # Laplace on a gray scale picturegray = cv.CreateImage(cv.GetSize(im), 8, 1)cv.CvtColor(im, gray, cv.CV_BGR2GRAY) aperture=3 dst = cv.CreateImage(cv.GetSize(gray), cv.IPL_DEPTH_32F, 1)cv.Laplace(gray, dst,aperture) cv.Convert(dst,gray) thresholded = cv.CloneImage(im)cv.Threshold(im, thresholded, 50, 255, cv.CV_THRESH_BINARY_INV) cv.ShowImage(\’Laplaced grayscale\’,gray)#———————————— # Laplace on colorplanes = [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) #Split channels to apply laplace on eachfor plane in planes:    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)#————————————- cv.WaitKey(0)

效果展示

原图

灰度化图片检测

原始彩色图片检测

0x02. Sobel 算法

Sobel 也是很常用的一种轮廓识别的算法。

关于 Sobel 导数原理的介绍,可参考

  • Sobel 导数

以下是使用 Sobel 算法进行轮廓检测的代码和效果

12345678910111213141516171819202122 import cv2.cv as cv 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) #Sobel with x-order=1 soby = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_16S, 1)cv.Sobel(im, soby, 0, 1, 3) #Sobel withy-oder=1 cv.Abs(sobx, sobx)cv.Abs(soby, soby) result = cv.CloneImage(im)cv.Add(sobx, soby, result) #Add the two results together. cv.Threshold(result, result, 100, 255, cv.CV_THRESH_BINARY_INV) cv.ShowImage(\’Image\’, im)cv.ShowImage(\’Result\’, result) cv.WaitKey(0)

处理之后效果图(感觉比Laplace效果要好些)

0x03. cv.MorphologyEx

cv.MorphologyEx 是另外一种边缘检测的算法

1234567891011121314 import cv2.cv as cv image=cv.LoadImage(\’img/build.png\’, cv.CV_LOAD_IMAGE_GRAYSCALE) #Get edgesmorphed = cv.CloneImage(image)cv.MorphologyEx(image, morphed, None, None, cv.CV_MOP_GRADIENT) # Apply a dilate – Erode cv.Threshold(morphed, morphed, 30, 255, cv.CV_THRESH_BINARY_INV) cv.ShowImage(\”Image\”, image)cv.ShowImage(\”Morphed\”, morphed) cv.WaitKey(0)

0x04. Canny 边缘检测

Canny 算法可以对直线边界做出很好的检测;

关于 Canny 算法原理的描述,可参考:

  • Canny 边缘检测

123456789101112131415161718192021222324252627282930313233343536373839404142434445 import cv2.cv as cvimport math im=cv.LoadImage(\’img/road.png\’, cv.CV_LOAD_IMAGE_GRAYSCALE) pi = math.pi #Pi value dst = cv.CreateImage(cv.GetSize(im), 8, 1) cv.Canny(im, dst, 200, 200)cv.Threshold(dst, dst, 100, 255, cv.CV_THRESH_BINARY) #—- Standard —-color_dst_standard = cv.CreateImage(cv.GetSize(im), 8, 3)cv.CvtColor(im, color_dst_standard, cv.CV_GRAY2BGR)#Create output image in RGB to put red lines lines = cv.HoughLines2(dst, cv.CreateMemStorage(0), cv.CV_HOUGH_STANDARD, 1, pi / 180, 100, 0, 0)for (rho, theta) in lines[:100]:    a = math.cos(theta) #Calculate orientation in order to print them    b = math.sin(theta)    x0 = a * rho    y0 = b * rho    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) #Draw the line #—- Probabilistic —-color_dst_proba = cv.CreateImage(cv.GetSize(im), 8, 3)cv.CvtColor(im, color_dst_proba, cv.CV_GRAY2BGR) # idem