我是初學者,我正在嘗試為左側的白色遙控器應用輪廓,該遙控器與背景顏色相同。
a = cv2.imread(file_name)
imgGray = cv2.cvtColor(a,cv2.COLOR_BGR2GRAY)
imgGray = cv2.GaussianBlur(imgGray,(11,11),20)
k5 = np.array([[-1,-1,-1],[-1,9,-1],[-1,-1,-1]])
imgGray = cv2.filter2D(imgGray,-1,k5)
cv2.namedWindow("Control")
cv2.createTrackbar("blocksize","Control",33,1000,f)
cv2.createTrackbar("c","Control",3,100,f)
while True:
strel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
blocksize = cv2.getTrackbarPos("blocksize","Control")
c = cv2.getTrackbarPos("c","Control")
if blocksize%2==0:
blocksize = 1
thrash = cv2.adaptiveThreshold(imgGray,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV,blockSize=blocksize,C=c)
thrash1 = cv2.adaptiveThreshold(imgGray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,blockSize=blocksize,C=c)
cv2.imshow("mean",thrash)
cv2.imshow("gaussian",thrash1)
#r,thrash = cv2.threshold(imgGray,150,255,cv2.THRESH_BINARY_INV)
key = cv2.waitKey(1000)
if key == 32 or iter == -1:
break
edges = cv2.Canny(thrash,100,200)
cv2.imshow('sharpen',sharpen)
cv2.imshow('edges',edges)
cv2.imshow('grey ',imgGray)
cv2.imshow('thrash ',thrash)
cv2.waitKey(0)
circles = cv2.HoughCircles(imgGray,cv2.HOUGH_GRADIENT,1,60,param1=240,param2=50,minRadius=0,maxRadius=0)
contours,_ = cv2.findContours(thrash,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
putlabel(circles,a,contours)
這些都是我嘗試過的,我也嘗試過膨脹,腐蝕,開閉等形態學操作,但我仍然無法獲得結果。
下面是我最好的結果,但噪音太嚴重,遙控器沒有完全勾勒出來。
uj5u.com熱心網友回復:
我認為簡單的影像處理無法隔離與背景顏色相同的物件。因此,我們必須切換到深度/機器學習。這個想法是
如您所見,它用作邊緣檢測器。您可以改變內核大小 ( k1, k2
) 和 sigma 值 ( s1, s2
)
# Applying Otsu Threshold and finding contours
th = cv2.threshold(DoG_img ,127,255,cv2.THRESH_BINARY cv2.THRESH_OTSU)[1]
contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# Create copy of original image
img1 = img.copy()
# for each contour above certain area and extent, draw minimum bounding box
for c in contours:
area = cv2.contourArea(c)
if area > 1500:
x,y,w,h = cv2.boundingRect(c)
extent = int(area)/w*h
if extent > 2000:
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img1,[box],0,(0,255,0),4)
如您所見,結果并不完美。在邊緣檢測程序(高斯差)期間也會捕獲物件的陰影。您可以嘗試改變引數以檢查結果是否變得更好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/468865.html
標籤:Python opencv 图像处理 计算机视觉 图像阈值