2017-01-03 5 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() 

ich auf dem obigen Code arbeite. Der Code wurde perfekt ausgeführt, aber die Konturpunkte des Bildes waren völlig falsch. Ich habe diesen Fehler nach dem Zeichnen des Graphen beobachtet. Hilf mir bei diesem Thema.Finding Konturen eines Bildes Python OpenCV

+1

Wie lautet das Eingabebild und die Ausgabe dieses Code-Snippets? – ZdaR

+0

Sie sind Bilder und ich kann keine Bilder in der Kommentarzeile anhängen. –

+1

Dann laden Sie sie hoch und stellen Sie hier Links zur Verfügung – ZdaR

Antwort

0

1) Wenn es kein Kernel für Erosion und Dilatation angegeben ist, wird das Bild nicht ändern. Probieren Sie sie mit einem Kernel aus.

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

2) cnts enthält die Punkte ((x, y) Tupeln) auf der Kontur gefunden. size ist nur die Anzahl der Punkte auf dieser Kontur. Sie erfassen die Größe und verarbeiten die Punkte nicht, stattdessen lesen Sie eine Datendatei und zeichnen etwas völlig anderes auf. Um richtig die Konturen zu sehen, die versuchen, die folgenden nach 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) 

Dann versuchen Sie zu Ihrer Datendatei mit den Konturpunkten beziehen.

Verwandte Themen