裁剪特定颜色区域并去除噪声区域(Python+OpenCV)

Crop the specific color region and remove the noisy regions (Python+OpenCV)

我在从彩色图像获取二值图像时遇到问题。 cv2.inRange() 函数用于获取图像的 mask (与阈值处理类似),我想删除不需要的部分,最大限度地减少对 mask 图像的侵蚀。最大的问题是 mask 没有定期提取。

示例

破解:

典型一个

理想的:

我的第一个目标是把第二张图片做成第三张。我想得到面积最大的轮廓并删除其他轮廓(也适用于 mask)是可行的。但是找不到怎么办。

第二个问题是我上面描述的想法不适用于第一张图片(破解)。这种图像可以丢弃。但无论如何它应该被标记为破解。到目前为止,我还没有这方面的想法。

我做了什么

这里是输入图片和代码42_1.jpg

class Real:
    __ex_low=np.array([100,30,60])
    __ex_high=np.array([140,80,214])

    __ob_low=np.array([25,60,50]) #27,65,100])
    __ob_high=np.array([50,255,255]) #[45,255,255])

    def __opening(self, mask):
        kernel = np.ones((3,3), np.uint8)
        op = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
        return op

    def __del_ext(self, img_got):
        img = img_got[0:300,]
        hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv, self.__ex_low, self.__ex_high)

        array1 = np.transpose(np.nonzero(mask))
        array2 = np.nonzero(mask)
        temp=array1.tolist()

        xmin=min(array2[0])     #find the highest point covered blue
        x,y,channel=img.shape
        img=img[xmin:x,]
        hsv=hsv[xmin:x,]

        return img, hsv


    def __init__(self, img_got):
        img, hsv = self.__del_ext(img_got)

        mask_temp = cv2.inRange(hsv, self.__ob_low, self.__ob_high)
        mask = self.__opening(mask_temp)

        array1 = np.transpose(np.nonzero(mask))
        array2 = np.nonzero(mask)

        ymin=min(array2[1])
        ymax=max(array2[1])
        xmin=min(array2[0])
        xmax=max(array2[0])

        self.x = xmax-xmin
        self.y = ymax-ymin
        self.ratio = self.x/self.y

       # xmargin = int(self.x*0.05)
        #ymargin = int(self.y*0.05)

        self.img = img[(xmin):(xmax),(ymin):(ymax)]
        self.mask = mask[(xmin):(xmax),(ymin):(ymax)]

#models = glob.glob("D:/Python36/images/motor/*.PNG")
img = cv2.imread("D:/Python36/images/0404/33_1.jpg")#<- input image

#last_size = get_last_size(models[-1])
#m2= Model(models[39],last_size)

r1 = Real(img)


cv2.imshow("2",r1.img)
cv2.imshow("3",r1.mask)

如果代码写成python3就好了,但是什么都行。

总的来说,你的方法是可以的,除了错误的内核去横线。

我按以下步骤完成:

(1) Read and convert to HSV

(2) Find the target yellow color region in HSV

(3) morph-op to remove horizone lines

(4) crop the region

这是结果:


代码:

#!/usr/bin/python3
# 2018/04/16 13:20:07
# 2018/04/16 14:13:03

import cv2
import numpy as np

## (1) Read and convert to HSV
img = cv2.imread("euR2X.png")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

## (2) Find the target yellow color region in HSV
hsv_lower = (25, 100, 50)
hsv_upper = (33, 255, 255)
mask = cv2.inRange(hsv, hsv_lower, hsv_upper)

## (3) morph-op to remove horizone lines
kernel = np.ones((5,1), np.uint8)
mask2 = cv2.morphologyEx(mask, cv2.MORPH_OPEN,  kernel)


## (4) crop the region
ys, xs = np.nonzero(mask2)
ymin, ymax = ys.min(), ys.max()
xmin, xmax = xs.min(), xs.max()

croped = img[ymin:ymax, xmin:xmax]

pts = np.int32([[xmin, ymin],[xmin,ymax],[xmax,ymax],[xmax,ymin]])
cv2.drawContours(img, [pts], -1, (0,255,0), 1, cv2.LINE_AA)


cv2.imshow("croped", croped)
cv2.imshow("img", img)
cv2.waitKey()

References:

  1. what are recommended color spaces for detecting orange color in open cv?