图像预处理:轮廓扩展

时间:2020-07-25 10:10:23

标签: image opencv image-processing mask contour

我想做一些图像预处理,但是我不确定其中的最佳步骤。

我在MRI图像上标注了有趣的区域,我检测到轮廓并裁剪图像:

enter image description here

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

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)

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

enter image description here

您知道使用OpenCV进行此操作的任何本机方法吗?还是任何算法或非本地方式来实现这一目标?

非常感谢您

1 个答案:

答案 0 :(得分:1)

Yunus Temuerlenkl在评论中告诉我。

此方法的精度取决于轮廓蒙版的精度

尽管这是一种迭代方法,但对我来说并不会增加太多的处理时间。您可以做的一件事是并行处理图像/帧

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)