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

标签 python opencv contour binary-image

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

示例

破解:

crack

典型的

typical one

理想的:

ideal one

我的第一个目标是将第二张图片作为第三张图片。我想得到面积最大的轮廓并删除其他轮廓(也适用于 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

这是结果:

enter image description here


代码:

#!/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()

引用:

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

  2. Find single color, horizontal spaces in image

关于python - 裁剪特定颜色区域并去除噪声区域(Python+OpenCV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49848847/

相关文章:

python - 什么是 spec 和 spec_set

opencv - 在 Go 应用程序中查找内存泄漏

image - 如何使用 OpenCV 去除图像的背景?

java - NATIVE_LIBRARY_NAME 无法解析或不是字段

Python:根据 1 和 0 的数组计算结构的面积和周长

python - 自定义字体的 Kivy 标签标记

python - tensorflow 错误 : 'FileWriter' method

python - 有没有比使用循环和 iloc 更快的方法在小 Pandas 数据帧上进行整行比较?

python - 3D 轴上填充等高线图中的伪影

c++ - 如何绘制每个分割对象的轮廓