2016-05-04 12 views
6

मुझे अजगर का उपयोग कर छवियों में छेद भरने की जरूरत है। यह उन वस्तुओं के साथ छवि है जिन्हें मैं प्राप्त करने में कामयाब रहा - वे वास्तव में उन वस्तुओं के किनारों पर हैं जिन्हें मैं चाहता हूं, इसलिए मुझे उन्हें भरना होगा। enter image description hereएक छवि में ट्रिकी भरने छेद

यह ndimage.binary_fill_holes(A) का उपयोग कर बहुत स्पष्ट लग रहा था, लेकिन समस्या यह है कि यह इस का उत्पादन (मैन्युअल रूप से लाल रंग से भरा) है:

enter image description here

लेकिन मैं इस की जरूरत है:

enter image description here

किसी भी तरह से इसे हल किया जा सकता है? enter image description here

+0

अच्छी समस्या। सोचा था कि आप ओपनसीवी से कॉन्टूर पदानुक्रम के साथ इसे हल कर सकते हैं जैसा कि यहां http://docs.opencv.org/3.1.0/d9/d8b/tutorial_py_contours_hierarchy.html#gsc.tab=0 है, लेकिन यह महत्वपूर्ण नहीं है क्योंकि महत्वपूर्ण समोच्च है कोई सच्चा बच्चा नहीं आप जल्दी से देख सकते हैं कि यह नमूना फ़ाइल का उपयोग https://github.com/Itseez/opencv/blob/master/samples/python/contours.py – tfv

+0

पर काम नहीं करता है, धन्यवाद, @tfv! नकारात्मक परिणाम भी परिणाम है ... – Phlya

+0

मैं कहूंगा कि आपके द्वारा दी गई छवि आपको वह निर्णय लेने की अनुमति नहीं देती है जिसे आप ढूंढ रहे हैं। एक सीमा है (जिसे हटाया जाना चाहिए यदि एल्गोरिदम आपको जो चाहिए वह प्राप्त करने की उम्मीद है), कि जब आप इसे पार करते हैं और फिर दूसरे को पार करते हैं, तो आप अभी भी एक छेद के अंदर हैं, जो सभी के लिए समझ में नहीं आता सामान्य मामले – roadrunner66

उत्तर

2

मुझे लगता है कि मैं एक समाधान पाया है:

यह कुल्हाड़ियों के बिना पहली छवि यदि आप इसे आजमाइए करना चाहते हैं। जब से मैं समय से बाहर भाग गया, यह थोड़ा लंबा है, लेकिन शायद यह मदद करता है। अगर मैंने केवल इस समस्या के लिए कोड किया है, लेकिन इसे कई छवियों के लिए सामान्य बनाना आसान होना चाहिए। पहले

कुछ नामकरण सम्मेलनों:

  • मैं कॉम्पैक्ट क्षेत्रों जो backround द्वारा संलग्न हैं "के रूप में प्रथम स्तर क्षेत्रों" को परिभाषित। इस तरह के पहले स्तर के क्षेत्रों में विभिन्न subregions शामिल हो सकता है।
  • एक प्रथम स्तर का क्षेत्र जिसमें एक से अधिक उपनगरीय क्षेत्र शामिल हैं, को एक महत्वपूर्ण क्षेत्र कहा जाता है।

मेरा मूल विचार दो महत्वपूर्ण क्षेत्रों के समरूपता की लंबाई की तुलना करना है जो एक महत्वपूर्ण क्षेत्र का हिस्सा हैं। हालांकि, मैं उनकी पूरी समोच्च लंबाई की तुलना नहीं करता, लेकिन केवल उस सेगमेंट जो पृष्ठभूमि के करीब है। पृष्ठभूमि के करीब छोटे समोच्च खंड वाला एक छेद माना जाता है।

मैं परिणाम परिणाम छवियों से पहले शुरू करूंगा।

क्या हम, के बारे में बात कर रहे हैं इसके बाद के संस्करण नामकरण सम्मेलनों vizualizing से कुछ अवलोकन:

enter image description here

महत्वपूर्ण क्षेत्र के दो उपक्षेत्र। पृष्ठभूमि के नजदीक के प्रत्येक क्षेत्र के दो सीमा खंड अलग-अलग रंगों में चिह्नित होते हैं (बहुत पतले, नीले और काले लाल, लेकिन दृश्यमान)। इन क्षेत्रों स्पष्ट रूप से सही नहीं कर रहे हैं ("पतली" क्षेत्रों त्रुटियों का कारण), लेकिन उनकी लंबाई की तुलना करने के लिए पर्याप्त:

enter image description here

अंतिम परिणाम। यदि आप छेद "बंद" करना चाहते हैं, तो मुझे बताएं, आपको पृष्ठभूमि के बजाय क्षेत्रों में मूल काले रंगों को असाइन करना होगा ([संपादित करें] मैंने कोड की तीन चिह्नित रेखाएं शामिल की हैं जो सीमाओं को आवंटित करती हैं क्षेत्रों, के रूप में आप की कामना की) करने के लिए:

enter image description here

कोड यहाँ जुड़ा हुआ है। मैंने ओपनसीवी कॉन्टूर फ़ंक्शन का उपयोग किया है जो कि बहुत ही कठोर है, और कुछ मास्किंग तकनीकें हैं।कोड इसकी दृश्यता के कारण घातक है, इसकी सीमित पठनीयता के लिए खेद है, लेकिन इस समस्या के लिए कोई दो लाइन समाधान नहीं लगता है।

कुछ अंतिम टिप्पणियां: मैंने पहली बार बिंदुओं के सेट का उपयोग करके समोच्चों का मिलान करने की कोशिश की, जो लूप से बचें और पृष्ठभूमि के करीब दो समोच्च खंडों को निर्धारित करने के लिए set.intersection के उपयोग की अनुमति दें, लेकिन आपकी ब्लैक लाइनों के बाद से बल्कि मोटे हैं, समोच्च sligthly mismatched हैं। मैंने समोच्चों के कंकालकरण की कोशिश की, लेकिन उसने कीड़े का एक और प्रकार खोला, इसलिए मैंने समोच्च बिंदुओं के बीच एक लूप और गणना दूरी कर एक डंप दृष्टिकोण के साथ काम किया। उस हिस्से को करने का एक अच्छा तरीका हो सकता है, लेकिन यह काम करता है।

मैंने Shapely मॉड्यूल का उपयोग करने पर भी विचार किया है, इसके कुछ फायदे प्राप्त करने के तरीके हो सकते हैं, लेकिन मुझे कोई नहीं मिला, इसलिए मैंने इसे फिर से गिरा दिया।

import numpy as np 
import scipy.ndimage as ndimage 
from matplotlib import pyplot as plt 
import cv2 


img= ndimage.imread('image.png') 

# Label digfferentz original regions 
labels, n_regions = ndimage.label(img) 
print "Original number of regions found: ", n_regions 
# count the number of pixels in each region 
ulabels, sizes = np.unique(labels, return_counts=True) 
print sizes 

# Delete all regions with size < 2 and relabel 
mask_size = sizes < 2 
remove_pixel = mask_size[labels] 
labels[remove_pixel] = 0 
labels, n_regions = ndimage.label(labels) #,s) 
print "Number of regions found (region size >1): ", n_regions 
# count the number of pixels in each region 
ulabels, sizes = np.unique(labels, return_counts=True) 
print ulabels 
print sizes 


# Determine large "first level" regions 
first_level_regions=np.where(labels ==1, 0, 1) 
labeled_first_level_regions, n_fl_regions = ndimage.label(first_level_regions) 
print "Number of first level regions found: ", n_fl_regions 


# Plot regions and first level regions 
fig = plt.figure() 
a=fig.add_subplot(2,3,1) 
a.set_title('All regions') 
plt.imshow(labels, cmap='Paired', vmin=0, vmax=n_regions) 
plt.xticks([]), plt.yticks([]), plt.colorbar() 
a=fig.add_subplot(2,3,2) 
a.set_title('First level regions') 
plt.imshow(labeled_first_level_regions, cmap='Paired', vmin=0, vmax=n_fl_regions) 
plt.xticks([]), plt.yticks([]), plt.colorbar() 


for region_label in range(1,n_fl_regions): 
    mask= labeled_first_level_regions!=region_label 
    result = np.copy(labels) 
    result[mask]=0  
    subregions = np.unique(result).tolist()[1:] 
    print region_label, ": ", subregions 

    if len(subregions) >1: 
     print " Element 4 is a critical element: ", region_label 
     print " Subregions: ", subregions 

     #Critical first level region 
     crit_first_level_region=np.ones(labels.shape) 
     crit_first_level_region[mask]=0 

     a=fig.add_subplot(2,3,4) 
     a.set_title('Crit. first level region') 
     plt.imshow(crit_first_level_region, cmap='Paired', vmin=0, vmax=n_regions) 
     plt.xticks([]), plt.yticks([]) 

     #Critical Region Contour 
     im = np.array(crit_first_level_region * 255, dtype = np.uint8) 
     _, contours0, hierarchy = cv2.findContours(im.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) 
     crit_reg_contour = [contours0[0].flatten().tolist()[i:i+2] for i in range(0, len(contours0[0].flatten().tolist()), 2)] 
     print crit_reg_contour 
     print len(crit_reg_contour) 



     #First Subregion 
     mask2= labels!=subregions[1] 
     first_subreg=np.ones(labels.shape) 
     first_subreg[mask2]=0 

     a=fig.add_subplot(2,3,5) 
     a.set_title('First subregion: '+str(subregions[0])) 
     plt.imshow(first_subreg, cmap='Paired', vmin=0, vmax=n_regions) 
     plt.xticks([]), plt.yticks([])   

     #First Subregion Contour 
     im = np.array(first_subreg * 255, dtype = np.uint8) 
     _, contours0, hierarchy = cv2.findContours(im.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) 
     first_sub_contour = [contours0[0].flatten().tolist()[i:i+2] for i in range(0, len(contours0[0].flatten().tolist()), 2)] 
     print first_sub_contour 
     print len(first_sub_contour) 




     #Second Subregion 
     mask3= labels!=subregions[0] 
     second_subreg=np.ones(labels.shape) 
     second_subreg[mask3]=0 

     a=fig.add_subplot(2,3,6) 
     a.set_title('Second subregion: '+str(subregions[1])) 
     plt.imshow(second_subreg, cmap='Paired', vmin=0, vmax=n_regions) 
     plt.xticks([]), plt.yticks([])  

     #Second Subregion Contour 
     im = np.array(second_subreg * 255, dtype = np.uint8) 
     _, contours0, hierarchy = cv2.findContours(im.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) 
     second_sub_contour = [contours0[0].flatten().tolist()[i:i+2] for i in range(0, len(contours0[0].flatten().tolist()), 2)] 
     print second_sub_contour 
     print len(second_sub_contour) 


     maxdist=6 
     print "Points in first subregion close to first level contour:" 
     close_1=[] 
     for p1 in first_sub_contour: 
      for p2 in crit_reg_contour: 
       if (abs(p1[0]-p2[0])+abs(p1[1]-p2[1]))<maxdist: 
        close_1.append(p1) 
        break 

     print close_1 
     print len(close_1) 

     print "Points in second subregion close to first level contour:" 
     close_2=[] 
     for p1 in second_sub_contour: 
      for p2 in crit_reg_contour: 
       if (abs(p1[0]-p2[0])+abs(p1[1]-p2[1]))<maxdist: 
        close_2.append(p1) 
        break 

     print close_2 
     print len(close_2)  


     for p in close_1: 
      result[p[1],p[0]]=1 

     for p in close_2: 
      result[p[1],p[0]]=2 


     if len(close_1)>len(close_2): 
      print "first subregion is considered a hole:", subregions[0] 
      hole=subregions[0] 
     else:    
      print "second subregion is considered a hole:", subregions[1] 
      hole=subregions[1] 


     #Plot Critical region with subregions 
     a=fig.add_subplot(2,3,3) 
     a.set_title('Critical first level region with subregions') 
     plt.imshow(result, cmap='Paired', vmin=0, vmax=n_regions) 
     plt.xticks([]), plt.yticks([]) 

     result2=result.copy() 


#Plot result 
fig2 = plt.figure() 
a=fig2.add_subplot(1,1,1) 
a.set_title('Critical first level region with subregions and bordering contour segments') 
plt.imshow(result2, cmap='flag', vmin=0, vmax=n_regions) 
plt.xticks([]), plt.yticks([]) 


#Plot result 
mask_hole=np.where(labels ==hole, True, False) 
labels[mask_hole]=1 
labels=np.where(labels > 1, 2, 1) 

# [Edit] Next two lines include black borders into final result 
mask_borders=np.where(img ==0, True, False) 
labels[mask_borders]=2 


fig3 = plt.figure() 
a=fig3.add_subplot(1,1,1) 
a.set_title('Final result') 
plt.imshow(labels, cmap='flag', vmin=0, vmax=n_regions) 
plt.xticks([]), plt.yticks([]) 


plt.show() 
+0

धन्यवाद! ऐसा लगता है कि आपको वही करने में कामयाब रहा जो मुझे चाहिए! (बस समोच्चों को क्षेत्रों का एक हिस्सा होने की आवश्यकता है, जो कि 'काफी महत्वपूर्ण है) मैं इसे बाद में अधिक विस्तार से देखूंगा और शायद आपका उत्तर स्वीकार करूंगा। – Phlya

+1

मैंने ऐसा करने के लिए तीन चिह्नित लाइनें शामिल की हैं और तदनुसार अंतिम छवि को बदल दिया है। – tfv

संबंधित मुद्दे