九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開(kāi)通VIP
Python

關(guān)于邊緣檢測(cè)的基礎(chǔ)來(lái)自于一個(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è):

  1. import cv2.cv as cv
  2. im=cv.LoadImage('img/building.png', cv.CV_LOAD_IMAGE_COLOR)
  3. # Laplace on a gray scale picture
  4. gray = cv.CreateImage(cv.GetSize(im), 8, 1)
  5. cv.CvtColor(im, gray, cv.CV_BGR2GRAY)
  6. aperture=3
  7. dst = cv.CreateImage(cv.GetSize(gray), cv.IPL_DEPTH_32F, 1)
  8. cv.Laplace(gray, dst,aperture)
  9. cv.Convert(dst,gray)
  10. thresholded = cv.CloneImage(im)
  11. cv.Threshold(im, thresholded, 50, 255, cv.CV_THRESH_BINARY_INV)
  12. cv.ShowImage('Laplaced grayscale',gray)
  13. #------------------------------------
  14. # Laplace on color
  15. planes = [cv.CreateImage(cv.GetSize(im), 8, 1) for i in range(3)]
  16. laplace = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_16S, 1)
  17. colorlaplace = cv.CreateImage(cv.GetSize(im), 8, 3)
  18. cv.Split(im, planes[0], planes[1], planes[2], None) #Split channels to apply laplace on each
  19. for plane in planes:
  20. cv.Laplace(plane, laplace, 3)
  21. cv.ConvertScaleAbs(laplace, plane, 1, 0)
  22. cv.Merge(planes[0], planes[1], planes[2], None, colorlaplace)
  23. cv.ShowImage('Laplace Color', colorlaplace)
  24. #-------------------------------------
  25. cv.WaitKey(0)

效果展示

原圖

灰度化圖片檢測(cè)

原始彩色圖片檢測(cè)

0x02. Sobel 算法

Sobel 也是很常用的一種輪廓識(shí)別的算法。

關(guān)于 Sobel 導(dǎo)數(shù)原理的介紹,可參考

以下是使用 Sobel 算法進(jìn)行輪廓檢測(cè)的代碼和效果

  1. import cv2.cv as cv
  2. im=cv.LoadImage('img/building.png', cv.CV_LOAD_IMAGE_GRAYSCALE)
  3. sobx = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_16S, 1)
  4. cv.Sobel(im, sobx, 1, 0, 3) #Sobel with x-order=1
  5. soby = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_16S, 1)
  6. cv.Sobel(im, soby, 0, 1, 3) #Sobel withy-oder=1
  7. cv.Abs(sobx, sobx)
  8. cv.Abs(soby, soby)
  9. result = cv.CloneImage(im)
  10. cv.Add(sobx, soby, result) #Add the two results together.
  11. cv.Threshold(result, result, 100, 255, cv.CV_THRESH_BINARY_INV)
  12. cv.ShowImage('Image', im)
  13. cv.ShowImage('Result', result)
  14. cv.WaitKey(0)

處理之后效果圖(感覺(jué)比Laplace效果要好些)

0x03. cv.MorphologyEx

cv.MorphologyEx 是另外一種邊緣檢測(cè)的算法

  1. import cv2.cv as cv
  2. image=cv.LoadImage('img/build.png', cv.CV_LOAD_IMAGE_GRAYSCALE)
  3. #Get edges
  4. morphed = cv.CloneImage(image)
  5. cv.MorphologyEx(image, morphed, None, None, cv.CV_MOP_GRADIENT) # Apply a dilate - Erode
  6. cv.Threshold(morphed, morphed, 30, 255, cv.CV_THRESH_BINARY_INV)
  7. cv.ShowImage('Image', image)
  8. cv.ShowImage('Morphed', morphed)
  9. cv.WaitKey(0)

0x04. Canny 邊緣檢測(cè)

Canny 算法可以對(duì)直線邊界做出很好的檢測(cè);

關(guān)于 Canny 算法原理的描述,可參考:

  1. import cv2.cv as cv
  2. import math
  3. im=cv.LoadImage('img/road.png', cv.CV_LOAD_IMAGE_GRAYSCALE)
  4. pi = math.pi #Pi value
  5. dst = cv.CreateImage(cv.GetSize(im), 8, 1)
  6. cv.Canny(im, dst, 200, 200)
  7. cv.Threshold(dst, dst, 100, 255, cv.CV_THRESH_BINARY)
  8. #---- Standard ----
  9. color_dst_standard = cv.CreateImage(cv.GetSize(im), 8, 3)
  10. cv.CvtColor(im, color_dst_standard, cv.CV_GRAY2BGR)#Create output image in RGB to put red lines
  11. lines = cv.HoughLines2(dst, cv.CreateMemStorage(0), cv.CV_HOUGH_STANDARD, 1, pi / 180, 100, 0, 0)
  12. for (rho, theta) in lines[:100]:
  13. a = math.cos(theta) #Calculate orientation in order to print them
  14. b = math.sin(theta)
  15. x0 = a * rho
  16. y0 = b * rho
  17. pt1 = (cv.Round(x0 1000*(-b)), cv.Round(y0 1000*(a)))
  18. pt2 = (cv.Round(x0 - 1000*(-b)), cv.Round(y0 - 1000*(a)))
  19. cv.Line(color_dst_standard, pt1, pt2, cv.CV_RGB(255, 0, 0), 2, 4) #Draw the line
  20. #---- Probabilistic ----
  21. color_dst_proba = cv.CreateImage(cv.GetSize(im), 8, 3)
  22. cv.CvtColor(im, color_dst_proba, cv.CV_GRAY2BGR) # idem
  23. rho=1
  24. theta=pi/180
  25. thresh = 50
  26. minLength= 120 # Values can be changed approximately to fit your image edges
  27. maxGap= 20
  28. lines = cv.HoughLines2(dst, cv.CreateMemStorage(0), cv.CV_HOUGH_PROBABILISTIC, rho, theta, thresh, minLength, maxGap)
  29. for line in lines:
  30. cv.Line(color_dst_proba, line[0], line[1], cv.CV_RGB(255, 0, 0), 2, 8)
  31. cv.ShowImage('Image',im)
  32. cv.ShowImage('Cannied', dst)
  33. cv.ShowImage('Hough Standard', color_dst_standard)
  34. cv.ShowImage('Hough Probabilistic', color_dst_proba)
  35. cv.WaitKey(0)

原圖

使用 Canny 算法處理之后

標(biāo)記出標(biāo)準(zhǔn)的直線

標(biāo)記出所有可能的直線

0x05. 輪廓檢測(cè)

OpenCV 提供一個(gè) FindContours 函數(shù)可以用來(lái)檢測(cè)出圖像中對(duì)象的輪廓:

  1. import cv2.cv as cv
  2. orig = cv.LoadImage('img/build.png', cv.CV_LOAD_IMAGE_COLOR)
  3. im = cv.CreateImage(cv.GetSize(orig), 8, 1)
  4. cv.CvtColor(orig, im, cv.CV_BGR2GRAY)
  5. #Keep the original in colour to draw contours in the end
  6. cv.Threshold(im, im, 128, 255, cv.CV_THRESH_BINARY)
  7. cv.ShowImage('Threshold 1', im)
  8. element = cv.CreateStructuringElementEx(5*2 1, 5*2 1, 5, 5, cv.CV_SHAPE_RECT)
  9. cv.MorphologyEx(im, im, None, element, cv.CV_MOP_OPEN) #Open and close to make appear contours
  10. cv.MorphologyEx(im, im, None, element, cv.CV_MOP_CLOSE)
  11. cv.Threshold(im, im, 128, 255, cv.CV_THRESH_BINARY_INV)
  12. cv.ShowImage('After MorphologyEx', im)
  13. # --------------------------------
  14. vals = cv.CloneImage(im) #Make a clone because FindContours can modify the image
  15. contours=cv.FindContours(vals, cv.CreateMemStorage(0), cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_SIMPLE, (0,0))
  16. _red = (0, 0, 255); #Red for external contours
  17. _green = (0, 255, 0);# Gren internal contours
  18. levels=2 #1 contours drawn, 2 internal contours as well, 3 ...
  19. cv.DrawContours (orig, contours, _red, _green, levels, 2, cv.CV_FILLED) #Draw contours on the colour image
  20. cv.ShowImage('Image', orig)
  21. cv.WaitKey(0)

效果圖:

原圖

識(shí)別結(jié)果

0x06. 邊界檢測(cè)

  1. import cv2.cv as cv
  2. im = cv.LoadImage('img/build.png', cv.CV_LOAD_IMAGE_GRAYSCALE)
  3. dst_32f = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_32F, 1)
  4. neighbourhood = 3
  5. aperture = 3
  6. k = 0.01
  7. maxStrength = 0.0
  8. threshold = 0.01
  9. nonMaxSize = 3
  10. cv.CornerHarris(im, dst_32f, neighbourhood, aperture, k)
  11. minv, maxv, minl, maxl = cv.MinMaxLoc(dst_32f)
  12. dilated = cv.CloneImage(dst_32f)
  13. cv.Dilate(dst_32f, dilated) # By this way we are sure that pixel with local max value will not be changed, and all the others will
  14. localMax = cv.CreateMat(dst_32f.height, dst_32f.width, cv.CV_8U)
  15. cv.Cmp(dst_32f, dilated, localMax, cv.CV_CMP_EQ) #compare allow to keep only non modified pixel which are local maximum values which are corners.
  16. threshold = 0.01 * maxv
  17. cv.Threshold(dst_32f, dst_32f, threshold, 255, cv.CV_THRESH_BINARY)
  18. cornerMap = cv.CreateMat(dst_32f.height, dst_32f.width, cv.CV_8U)
  19. cv.Convert(dst_32f, cornerMap) #Convert to make the and
  20. cv.And(cornerMap, localMax, cornerMap) #Delete all modified pixels
  21. radius = 3
  22. thickness = 2
  23. l = []
  24. for x in range(cornerMap.height): #Create the list of point take all pixel that are not 0 (so not black)
  25. for y in range(cornerMap.width):
  26. if cornerMap[x,y]:
  27. l.append((y,x))
  28. for center in l:
  29. cv.Circle(im, center, radius, (255,255,255), thickness)
  30. cv.ShowImage('Image', im)
  31. cv.ShowImage('CornerHarris Result', dst_32f)
  32. cv.ShowImage('Unique Points after Dilatation/CMP/And', cornerMap)
  33. cv.WaitKey(0)
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
編寫健壯(顏色和大小不變),圓檢測(cè)用opencv實(shí)現(xiàn)(基于Hough變換或其他特征)
生活服務(wù)
熱點(diǎn)新聞
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服