Skip to content
Advertisement

python – opencv morphologyEx remove specific color

After remove captcha’s background.
The image remain digits and noise.
Noise line is all in one color : RGB(127,127,127)
And then using morphology method.

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

Some part of digit will be remove.
How to use morphologyEx() remove only color in RGB(127,127,127) ?

enter image description here enter image description here

Advertisement

Answer

In order to eliminate color within a particular range you have to use cv2.inRange() function.

Here is the code:

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)

This is what I got for the two images you have:

Image 1:

enter image description here

Image 2:

enter image description here

You carry on from here.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement