2017-03-12 8 views
0

Ich habe versucht, zwischen einem Rechteck, Trapez und einem Halbkreis zu unterscheiden. Also habe ich eine Kontur um die Form und dann ein gedrehtes Rechteck gezeichnet. Danach finde ich den Bereich der Kontur und des gedrehten Rechtecks ​​und nehme deren Verhältnis. Mit diesem Verhältnis werde ich die Form bestimmen, da sie für die drei zuvor erwähnten Formen unterschiedlich sein wird.Konnte keine richtige Kontur erhalten

(Es ist klar, würde, wenn jemand eine robustere Methode zur Unterscheidung zwischen diesen drei hat.)

für das Problem kommen. Ich kann keine korrekte Kontur um das Bild zeichnen. Hier sind der Eingang und das Ausgangsbild:

Input Image

Output Image

Hier ist mein Code

import cv2 
import numpy as np 

img = cv2.imread('h4.JPG') 
cv2.imshow('Input',img) 
#img = cv2.resize(img, None, fx=0.2,fy=0.2) 
img = cv2.GaussianBlur(img, (11,11), 0) 
img = cv2.fastNlMeansDenoisingColored(img,None,10,10,7,21) 
im = img.copy() 

imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
ret,thresh = cv2.threshold(imgray,0,255,cv2.THRESH_BINARY) 
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 

max = 0 

for c in contours: 
    area = cv2.contourArea(c) 
    print area 
    if(np.any(max <= area)): 
     max = c 


A, B, C = cv2.minAreaRect(c) 
rotrect = cv2.minAreaRect(c) 
box = cv2.cv.BoxPoints(rotrect) 
box = np.int0(box) 
cv2.drawContours(im, contours, 0, (0,255,0), 2) 
cv2.drawContours(im, [box], 0, (0,0,255), 2) 

areaS = cv2.contourArea(contours[0]) 
areaR = B[0]*B[1] 

Ratio = areaS/areaR 

print "Shape Area: ",areaS 
print "Shape Rect: ",areaR 
print "Ratio: ",Ratio 

cv2.imshow('Output',im) 

if cv2.waitKey() and 0xff == 27: 
    cv2.destroyAllWindows() 

Vielen Dank im Voraus.

+0

mit einer höheren Schwelle: 'ret, thresh = cv2.threshold (imgray, 127.255, cv2.THRESH_BINARY)' . Sie _denoising_ erstellt Nicht-Null-Pixel, die von findContours als _foreground_ betrachtet werden – Miki

+0

Dank @Miki. Es half. Aber könnten Sie mir bitte sagen, warum Sie 127 als Schwelle ausgewählt haben – StupidGuy

+0

Etwas hoch genug, um niedrige Werte (fast schwarz) zu entfernen, aber nicht zu hoch, um hohe Werte (fast weiß) zu entfernen. 127 ist in der Mitte;). Alles von 30 bis 220 hätte wahrscheinlich auch funktioniert – Miki

Antwort

0

Ich habe den Code mit der von Miki im Kommentarbereich bereitgestellten Lösung gepostet.

CODE:

im = cv2.imread('Figure.jpg', 1) 
gray_img = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 
im1 = im.copy()      #---copy of the original image---- 

ret, thresh = cv2.threshold(gray_img, 127, 255, 0) 
blur_img = cv2.GaussianBlur(thresh, (11,11), 0) 

#---Finding and drawing contours--- 
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 
cv2.drawContours(im1, contours, -1, (0, 255, 0), 3) 

#----Drawing a rotated rectangle---- 
cnt = contours 
rect = cv2.minAreaRect(cnt[0]) #---I used cnt[0] since there is only one contour, if there are more you can assign this within a for loop---- 
box = cv2.boxPoints(rect) 
box = np.int0(box) 
im = cv2.drawContours(im1, [box], 0, (0,0,255), 2) 

cv2.imshow("Final_Image.jpg", im1) 

ERGEBNIS:

enter image description here

Verwandte Themen