python - opencv morphologyEx删除特定的颜色

时间:2017-03-04 04:53:16

标签: python opencv image-processing

删除验证码的背景。
图像保留数字和噪音 噪音线全部为一种颜色: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)?

enter image description here enter image description here

3 个答案:

答案 0 :(得分: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 :(得分: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

答案 2 :(得分:1)

颜色范围

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)

[原始图片]之前

Original Image

蓝标提取

Blue Tick Marks Determined

最终图像

enter image description here

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

希望有所帮助!