2017-01-03 164 views
0
# import the necessary packages 
import decimal 
import imutils 
import cv2 
import numpy as np 
import matplotlib.pyplot as plt 

# load the image, convert it to grayscale, and blur it slightly 
image = cv2.imread("hand.jpg",0) 

# threshold the image, then perform a series of erosions + 
# dilations to remove any small regions of noise 
thresh = cv2.threshold(image, 45, 255, cv2.THRESH_BINARY)[1] 
thresh = cv2.erode(thresh, None, iterations=2) 
thresh = cv2.dilate(thresh, None, iterations=2) 

# find contours in thresholded image, then grab the largest one 
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, 
    cv2.CHAIN_APPROX_SIMPLE) 
cnts = cnts[0] if imutils.is_cv2() else cnts[1] 
c = max(cnts, key=cv2.contourArea) 
size = len(c); 

refer_point = (207,130) 

data = np.genfromtxt("data.txt", delimiter=',') 

X = data[:,][:,0] 

Y = data[:,][:,1] 

for i in range(0,size): 
    dist1= (((abs(207-X))**2)+((abs(130-Y))**2))**(1.0/2.0) 

dist3 = round(dist1,2) 
print dist3 

plt.plot([dist3]) 

plt.show() 

我正在研究上述代碼。代碼執行完美,但圖像的輪廓點完全錯誤。繪製圖後我觀察到了這個錯誤。在這個問題上幫助我。查找圖像的輪廓Python OpenCV

+1

什麼是此代碼段的輸入圖像和輸出? – ZdaR

+0

他們是圖像,我不能在評論欄中添加任何圖像。 –

+1

然後上傳它們並在這裏提供鏈接 – ZdaR

回答

0

1)如果沒有指定侵蝕和膨脹的內核,則圖像不會改變。用內核嘗試它們。

kernel = np.ones((3,3),np.uint8) 
thresh = cv2.erode(thresh, kernel, iterations=1) 
thresh = cv2.dilate(thresh, kernel, iterations=1) 

2)cnts包含上找到的輪廓的各點((X,Y)的元組)。 size只是該輪廓上的點數。你獲得它的大小並且不處理這些點,而是你讀取一個數據文件並繪製完全不同的東西。要正確地看到輪廓,請嘗試以下findContours後:

# Load the image in color 
image = cv2.imread("hand.jpg",cv2.IMREAD_COLOR) 
# Draw the contours 
cv2.drawContours(image, cnts, -1, (0,255,0), 3) 
# Show the image with the contours 
cv2.imshow("contours",image) 

然後嘗試涉及您的數據文件與輪廓點。