2017-03-04 19 views
0

キャプチャの背景を削除した後。
画像は数字とノイズのままです。
ノイズラインはすべて1色です:RGB(127,127,127)
そして、形態学的方法を使用します。python - opencv morphologyEx特定の色を削除する

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2)) 
    self.im = cv2.morphologyEx(self.im, cv2.MORPH_CLOSE, kernel) 

一部の桁は削除されます。
morphologyEx()を使用するには、RGB(127,127,127)の色のみを削除しますか?

enter image description here enter image description here

答えて

1

あなたがcv2.inRange()機能を使用する必要が特定の範囲内の色を排除するために。ここで

はコードです:

lower = np.array([126,126,126]) #-- Lower range -- 
upper = np.array([127,127,127]) #-- Upper range -- 
mask = cv2.inRange(img, lower, upper) 
res = cv2.bitwise_and(img, img, mask= mask) #-- Contains pixels having the gray color-- 
cv2.imshow('Result',res) 

これは、私はあなたが持っている二つの画像のために得たものである:

画像1:

enter image description here

画像2:

enter image description here

ここから進んでください。

1

私の解決策です。
あなたの答えは私よりも明らかです。

def mop_close(self): 
    def morphological(operator=min): 
     height, width, _ = self.im.shape 
     # create empty image 
     out_im = np.zeros((height,width,3), np.uint8) 
     out_im.fill(255) # fill with white 
     for y in range(height): 
      for x in range(width): 
       try: 
        if self.im[y,x][0] ==127 and self.im[y,x][1] ==127 and self.im[y,x][2] ==127: 
         nlst = neighbours(self.im, y, x) 

         out_im[y, x] = operator(nlst,key = lambda x:np.mean(x)) 
        else: 
         out_im[y,x] = self.im[y,x] 
       except Exception as e: 
        print(e) 
     return out_im 

    def neighbours(pix,y, x): 
     nlst = [] 
     # search pixels around im[y,x] add them to nlst 
     for yy in range(y-1,y+1): 
      for xx in range(x-1,x+1): 
       try: 
        nlst.append(pix[yy, xx]) 
       except: 
        pass 
     return np.array(nlst) 

    def erosion(im): 
     return morphological(min) 

    def dilation(im): 
     return morphological(max) 

    self.im = dilation(self.im) 
    self.im = erosion(self.im) 

最終結果:それは私は満足して働く限り enter image description here enter image description here

+0

クール。あなたが気にしない場合は、最終結果を投稿してください。 :D –

+0

私はメソッドで小さな調整を行いました。記事の後ろに結果を添付しました –

+0

あなたの結果はかなりまともです。すごい仕事!!! –

関連する問題