【问题标题】:python - opencv morphologyEx remove specific colorpython - opencvmorphologyEx删除特定颜色
【发布时间】:2017-07-24 08:26:41
【问题描述】:

移除验证码的背景后。
图像仍然存在数字和噪点。
噪声线全部采用一种颜色:RGB(127,127,127)
然后使用形态学方法。

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

部分数字将被删除。
如何使用morphologyEx() 仅删除RGB(127,127,127) 中的颜色?

【问题讨论】:

    标签: python opencv image-processing


    【解决方案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:

    图 2:

    你从这里继续。

    【讨论】:

      【解决方案2】:

      这是我的解决方案。
      你的答案显然比我的好。

       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)
      

      最终结果:

      【讨论】:

      • 酷只要它有效,我就很满意。如果您不介意,也可以发布最终结果。 :D
      • 我在方法上做了一个小调整。我在文章后面附上了结果
      【解决方案3】:

      颜色范围

      color_dict_HSV = {'black': [[180, 255, 30], [0, 0, 0]],
                    'white': [[180, 18, 255], [0, 0, 231]],
                    'red1': [[180, 255, 255], [159, 50, 70]],
                    'red2': [[9, 255, 255], [0, 50, 70]],
                    'green': [[89, 255, 255], [36, 50, 70]],
                    'blue': [[128, 255, 255], [90, 50, 70]],
                    'yellow': [[35, 255, 255], [25, 50, 70]],
                    'purple': [[158, 255, 255], [129, 50, 70]],
                    'orange': [[24, 255, 255], [10, 50, 70]],
                    'gray': [[180, 18, 230], [0, 0, 40]]}
      
      

      致谢:

      阿里哈希米安

      如何使用 OPENCV 从图像中去除颜色

      由于大多数人都希望这样做,即在我的情况下,任务是从图像中删除蓝色,我使用以下代码从我的图像中删除蓝色墨水印章和蓝色刻度线,以便使用 Tesseract 进行正确的 OCR。

      [颜色去除]代码

      import cv2
      import numpy as np
      import matplotlib.pyplot as plt
      %matplotlib inline
      
      # image path:    
      #path = "D://opencvImages//"
      #fileName = "out.jpg"
      
      # Reading an image in default mode:
      inputImage = cv2.imread('0.jpg')
      
      # Convert RGB to grayscale:
      grayscaleImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
      
      # Convert the BGR image to HSV:
      hsvImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2HSV)
      
      # Create the HSV range for the blue ink:
      # [128, 255, 255], [90, 50, 70]
      lowerValues = np.array([90, 50, 70])
      upperValues = np.array([128, 255, 255])
      
      # Get binary mask of the blue ink:
      bluepenMask = cv2.inRange(hsvImage, lowerValues, upperValues)
      # Use a little bit of morphology to clean the mask:
      # Set kernel (structuring element) size:
      kernelSize = 3
      # Set morph operation iterations:
      opIterations = 1
      # Get the structuring element:
      morphKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernelSize, kernelSize))
      # Perform closing:
      bluepenMask = cv2.morphologyEx(bluepenMask, cv2.MORPH_CLOSE, morphKernel, None, None, opIterations, cv2.BORDER_REFLECT101)
      
      # Add the white mask to the grayscale image:
      colorMask = cv2.add(grayscaleImage, bluepenMask)
      _, binaryImage = cv2.threshold(colorMask, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
      cv2.imwrite('bwimage.jpg',binaryImage)
      thresh, im_bw = cv2.threshold(binaryImage, 210, 230, cv2.THRESH_BINARY)
      kernel = np.ones((1, 1), np.uint8)
      imgfinal = cv2.dilate(im_bw, kernel=kernel, iterations=1)
      cv2.imshow(imgfinal)
      
      

      [原图]之前

      蓝标提取

      最终图像

      在这里你可以看到几乎所有的刻度线都被删除了,原因是因为总是有改进的空间,但这似乎是我们能得到的最好的,因为即使删除这些小标记也不是将对使用 Tesseract 的 OCR 产生深远影响。

      希望有所帮助!

      【讨论】:

      • 嗨,您的代码对于从图像中提取一种颜色非常有帮助。但是我怎样才能同时提取两种颜色呢?说原始图像是否有红色和蓝色标记。感谢您的回复
      猜你喜欢
      • 2021-01-16
      • 1970-01-01
      • 2019-05-31
      • 2022-01-02
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      相关资源
      最近更新 更多