यदि वर्गों ने छवि में क्षेत्र को जोड़ा है, तो मैं उन्हें कैसे पहचान सकता हूं।उन्नत वर्ग पहचान (कनेक्टेड क्षेत्र के साथ)
मैं विधि OpenCV C++/Obj-C: Advanced square detection
में उल्लेख किया है यह अच्छी तरह से काम नहीं किया परीक्षण किया है।
कोई अच्छा विचार?
import cv2
import numpy as np
def angle_cos(p0, p1, p2):
d1, d2 = (p0-p1).astype('float'), (p2-p1).astype('float')
return abs(np.dot(d1, d2)/np.sqrt(np.dot(d1, d1)*np.dot(d2, d2)))
def find_squares(img):
squares = []
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# cv2.imshow("gray", gray)
gaussian = cv2.GaussianBlur(gray, (5, 5), 0)
temp,bin = cv2.threshold(gaussian, 80, 255, cv2.THRESH_BINARY)
# cv2.imshow("bin", bin)
contours, hierarchy = cv2.findContours(bin, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(gray, contours, -1, (0, 255, 0), 3)
#cv2.imshow('contours', gray)
for cnt in contours:
cnt_len = cv2.arcLength(cnt, True)
cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True)
if len(cnt) == 4 and cv2.contourArea(cnt) > 1000 and cv2.isContourConvex(cnt):
cnt = cnt.reshape(-1, 2)
max_cos = np.max([angle_cos(cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4]) for i in xrange(4)])
if max_cos < 0.1:
squares.append(cnt)
return squares
if __name__ == '__main__':
img = cv2.imread('123.bmp')
#cv2.imshow("origin", img)
squares = find_squares(img)
print "Find %d squres" % len(squares)
cv2.drawContours(img, squares, -1, (0, 255, 0), 3)
cv2.imshow('squares', img)
cv2.waitKey()
मैं opencv उदाहरण में कुछ विधि का उपयोग करें, लेकिन परिणाम अच्छा नहीं है।
+1 - अच्छा काम। –
हाँ, सच, वास्तव में अच्छा दृष्टिकोण, +1। इमेज इंडेक्स को सही प्रारूप में लाने के लिए यह बेहद मुश्किल है, मैं आपके द्वारा यहां दिखाए गए एक ही ज़िप अनपैक विधि का उपयोग करता था, लेकिन मुझे हाल ही में एहसास हुआ कि यह ट्रांसपोज़िंग और कॉपी करने से बहुत धीमी हो सकती है (गति महत्वपूर्ण होनी चाहिए ..) दुर्भाग्यवश, एक ओपनसीवी अपवाद (मेरे लिए कम से कम) से बचने के लिए प्रतिलिपि की आवश्यकता होती है .. – fraxel
अंतर यह है कि numpy डिफ़ॉल्ट रूप से '(y, x)' coords के साथ काम करता है, और ओपनसीवी '(x, y)' की अपेक्षा करता है। @fraxel मैंने प्रदर्शन को माप नहीं लिया, लेकिन यह संभावना है कि उस विशिष्ट बिंदु में अद्यतन कोड बेहतर होगा। – mmgp