समस्या पहली बार मेरे लिए सीधी लगती थी, मेरा विचार अलग-अलग ब्लब्स का पता लगाने के लिए ब्लॉब विश्लेषण का उपयोग करना था, उन्हें आकार के अनुसार समूहित करना और उन्हें रंग देने के लिए बाढ़फिल एल्गोरिदम का उपयोग करना था।
हालांकि, मैंने ब्लॉब विश्लेषण के लिए डिफ़ॉल्ट मानों के साथ कुछ समस्याओं में भाग लिया है, जिसे मैंने संशोधित नहीं किया था, जिसकी लागत कुछ समय है। इसके अलावा, मुझे ओपनसीवी के साथ बाढ़ भरने या रंगीन ब्लॉब्स के लिए कोई पायथन कोड स्निपेट नहीं मिला है, और पुराने संस्करणों की तुलना में सरलब्लोबडेटेशन का उपयोग करने के लिए कुछ वाक्यविन्यास परिवर्तन हुए हैं जिसके लिए मुझे केवल छोटे दस्तावेज़ और नमूना कोड मिल सकते हैं। तो शायद यह सब कोड अन्य उपयोगकर्ताओं के लिए भी उपयोगी हो सकता है।
आशा है कि मैंने उन सेगमेंट की सही पहचान की है जिन्हें आप ढूंढना चाहते हैं। यदि आप बड़े अंधेरे बाहरी पत्तों को शामिल नहीं करना चाहते हैं, तो टिप्पणी करने के लिए एक पंक्ति है।
दृश्य के लिए, आप छवि का आकार बदल सकते
कोड है (इस समय बाहर टिप्पणी की, 4 * 4 = 16 का एक पहलू से तदनुसार आकार थ्रेसहोल्ड अनुकूल करने के लिए याद) इन सभी विकल्पों के साथ कुछ हद तक लंबा, लेकिन पढ़ने के लिए उम्मीद है कि आसान है। मैंने इस समस्या पर काम कर रहे ओपनसीवी के साथ ब्लॉब विश्लेषण पर बहुत कुछ सीखा है, धन्यवाद!
अच्छी छवि, वैसे भी।
import numpy as np
import cv2
im = cv2.imread('tricky.png')
# For better visibility, resize image to better fit screen
#im= cv2.resize(im, dsize=(0,0),fx=0.25, fy=0.25)
#convert to gray value for blob analysis
imgray= cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
#### Blob analysis to find inner white leaves
# SimpleBlobDetector will find black blobs on white surface, this is why type=cv2.THRESH_BINARY_INV is necessary
ret,imthresh = cv2.threshold(imgray,160, 255,type=cv2.THRESH_BINARY_INV)
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
# Filter by Area.
params.filterByArea = True
params.minArea = 15000
params.maxArea = 150000
# Create a detector with the parameters
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
keypoints = detector.detect(imthresh)
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
# the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(imthresh, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Show blobs
cv2.imshow("Keypoints", im_with_keypoints)
####floodfill inner white leaves with blue
#http://docs.opencv.org/3.0-beta/modules/imgproc/doc/miscellaneous_transformations.html
#Create a black mask for floodfill. Mask needs to be 2 pixel wider and taller
maskborder=imgray.copy()
maskborder[:] = 0
bordersize=1
maskborder=cv2.copyMakeBorder(maskborder, top=bordersize, bottom=bordersize, left=bordersize, right=bordersize, borderType= cv2.BORDER_CONSTANT, value=[255,255,255])
print imgray.shape[:2]
print maskborder.shape[:2]
#Create result image for floodfill
result = im.copy()
#fill white inner segments with blue color
for k in keypoints:
print int(k.pt[0]),int(k.pt[1])
seed_pt = int(k.pt[0]),int(k.pt[1])
cv2.floodFill(result, maskborder, seed_pt, (255,0, 0))
#### Blob analysis to find small triangles
# SimpleBlobDetector will find black blobs on white surface, this is why type=cv2.THRESH_BINARY_INV is necessary
ret,imthresh2 = cv2.threshold(imgray,150, 255,type=cv2.THRESH_BINARY)
ret,imthresh3 = cv2.threshold(imgray,140, 255,type=cv2.THRESH_BINARY_INV)
imthresh4 = cv2.add(imthresh2,imthresh3)
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
# Filter by Area.
params.filterByArea = True
params.minArea = 20
params.maxArea = 1000
params.maxArea = 50000 #Using this line includes the outer dark leaves. Comment out if necessary
# Don't filter by Circularity
params.filterByCircularity = False
# Don't filter by Convexity
params.filterByConvexity = False
# Don't filter by Inertia
params.filterByInertia = False
# Create a detector with the parameters
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
keypoints = detector.detect(imthresh4)
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
# the size of the circle corresponds to the size of blob
im_with_keypoints2 = cv2.drawKeypoints(imthresh4, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Show blobs
cv2.imshow("Keypoints2", im_with_keypoints2)
####floodfill triangles with green
#http://docs.opencv.org/3.0-beta/modules/imgproc/doc/miscellaneous_transformations.html
#Create a black mask for floodfill. Mask needs to be 2 pixel wider and taller
maskborder=imgray.copy()
maskborder[:] = 0
bordersize=1
maskborder=cv2.copyMakeBorder(maskborder, top=bordersize, bottom=bordersize, left=bordersize, right=bordersize, borderType= cv2.BORDER_CONSTANT, value=[255,255,255])
print imgray.shape[:2]
print maskborder.shape[:2]
#Create result image for floodfill
result2 = result.copy()
#fill triangles with green color
for k in keypoints:
print int(k.pt[0]),int(k.pt[1])
seed_pt = int(k.pt[0]),int(k.pt[1])
cv2.floodFill(result2, maskborder, seed_pt, (0,255, 0))
#cv2.imshow('main',im)
#cv2.imshow('gray',imgray)
#cv2.imshow('borders',maskborder)
#cv2.imshow('threshold2',imthresh2)
#cv2.imshow('threshold3',imthresh3)
#cv2.imshow('threshold4',imthresh4)
cv2.imshow("Result", result2)
cv2.imwrite("result.png",result2)
cv2.waitKey(0)
cv2.destroyAllWindows()