物體角落(點)的辨識 --- cv2.cornerHarris & cv2.dilate

★cv2.cornerHarris的使用&格式


★範例所使用的圖檔下載位置

http://imgur.com/F05kwRB

★程式範例執行


import cv2
import numpy as np

filename = 'chessboard.png'
img = cv2.imread(filename)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)

#result is dilated for marking the corners, not important
dst = cv2.dilate(dst,None)

# Threshold for an optimal value, it may vary depending on the image.
img[dst>0.01*dst.max()]=[0,0,255]

cv2.imshow('dst',img)
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()


★範例執行(已灰階)前後之比較


★套用於齒輪辨識

有包含圓和圓心的辨識&單獨作角落抓點
import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread("gear2.png", 1)
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite('graygear.png', imgGray)

# 抓點
gray = np.float32(imgGray)
dst = cv2.cornerHarris(gray,2,3,0.04)

# result is dilated for marking the corners, not important
dst = cv2.dilate(dst,None)

# Threshold for an optimal value, it may vary depending on the image.
img[dst>0.01*dst.max()]=[0,0,255]

cv2.imshow('dst',img)

# 抓圓
circles = cv2.HoughCircles(imgGray,cv2.HOUGH_GRADIENT,1,20,
                            param1=60,param2=50,minRadius=0,maxRadius=60)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(img,(i[0],i[1]),i[2],(255,0,255),2)
    # draw the center of the circle
    cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('detected circles',img)


cv2.waitKey(0)


cv2.destroyAllWindows()

★齒輪辨識目的

配合之前的圓心可以初步抓出齒輪的外徑、內徑,方便之後直行計算相關程式的條件。


★齒輪辨識結果



Comments

comments powered by Disqus