2017-03-04 413 views
0

删除captcha的背景后。
图像仍然有数字和噪音。
噪声线全部在一种颜色: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

我在method.I附加了文章 –

+0

后面的小调整。你的结果现在看起来很不错。做得好!!! –