【问题标题】:Image preprocessing: contour expansion图像预处理:轮廓扩展
【发布时间】:2020-11-15 02:47:35
【问题描述】:

我想做一些图像预处理,但有一个步骤我不确定最好的方法。

我有带有注释的有趣区域的 MRI 图像,我检测轮廓并裁剪图像:

我将在此处发布我的代码,以便您了解我如何完成前面的步骤以及我们拥有的数据

lower_orange = np.array([0, 80, 50],np.uint8)
upper_orange = np.array([255, 255, 255],np.uint8)

for frame in frames:

    cv2.imshow('Original frame',frame)
    cv2.waitKey(0)

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    contour = cv2.inRange(hsv, lower_orange, upper_orange)

    x,y,w,h = cv2.boundingRect(contour)

    mask_inv = cv2.bitwise_not(contour)
    frame = cv2.bitwise_and(hsv,hsv,mask = mask_inv)

    cv2.imshow('Contoured frame',frame)
    cv2.waitKey(0)

    croped = frame[y:y+h,x:x+w]

    resized = cv2.resize(croped,(240,240))

    gray = resized[:,:,2]

    cv2.imshow('Grayscale frame',gray)
    cv2.waitKey(0)

    feature.append(gray)

我现在要做的就是把轮廓外的所有东西都涂黑:

您知道使用 OpenCV 执行此操作的任何本地方法吗?或者任何算法或非本地方式来实现这一点?

非常感谢

【问题讨论】:

  • 如果您获得了该轮廓: 1- 检查每一行并为每一行指向该轮廓的 firstlast 像素。 2- 将 first one 设为黑色,在 last one 之后设为黑色
  • 哇。这是一个简单的解决方案,我认为会完美运行。我要测试一下,我会在几分钟内告诉你。谢谢!!

标签: image opencv image-processing mask contour


【解决方案1】:

正如Yunus Temuerlenkl 在 cmets 中告诉我的那样。

此方法的精度取决于轮廓的掩码有多准确

虽然它是一种迭代方法,但对我来说并没有增加太多的处理时间。您可以做的一件事是并行处理图像/帧

for idx, frame in enumerate(frames):

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    contour = cv2.inRange(hsv, lower_orange, upper_orange)

    x,y,w,h = cv2.boundingRect(contour)

    croped_img = frame[y:y+h,x:x+w]
    croped_mask = contour[y:y+h,x:x+w]

    resized_gray_img = cv2.resize(croped_img,(dim,dim))[:,:,2]
    resized_mask = cv2.resize(croped_mask,(dim,dim))

    for row in range(dim):
        i = 0
        is_contour = False

        while((i < dim) & (not is_contour)):
            if(resized_mask[row,i]):
                is_contour = True
            resized_gray_img[row,i] = 0
            i+=1
        
        if not is_contour: continue
        is_contour = False

        i = dim -1

        while((i >= 0) & (not is_contour)):
            if(resized_mask[row,i]):
                is_contour = True
            resized_gray_img[row,i] = 0
            i-=1
   
    mask_inv = cv2.bitwise_not(resized_mask) 
    img = cv2.bitwise_and(resized_gray_img,resized_gray_img,mask = mask_inv)

    feature.append(img)

【讨论】:

    猜你喜欢
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 2019-07-18
    • 2012-10-26
    • 2011-12-30
    • 1970-01-01
    • 2021-01-05
    相关资源
    最近更新 更多