简体   繁体   English

图像预处理:轮廓扩展

[英]Image preprocessing: contour expansion

I want to do some image preprocessing, but there is one step which I am not sure the best way to do it.我想做一些图像预处理,但有一个步骤我不确定最好的方法。

I have MRI Images with interesting zones annotated, I detect the contour and crop the image:我有带有注释的有趣区域的 MRI 图像,我检测轮廓并裁剪图像:

在此处输入图像描述

I am going to post here my code, so you get an idea how I've done the previous steps and the data we have我将在此处发布我的代码,以便您了解我如何完成前面的步骤以及我们拥有的数据

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)

What I want to do now is to black out everything outside the contour:我现在要做的是将轮廓外的所有内容涂黑:

在此处输入图像描述

Do you know any native way to do this with OpenCV?您知道使用 OpenCV 执行此操作的任何本地方法吗? Or any algorithm or non-native way to achieve that?或者任何算法或非本地方式来实现这一点?

Thank you very much非常感谢

As Yunus Temuerlenkl told me in the comments.正如尤努斯·特穆尔伦克尔在评论中告诉我的那样。

The precision of this method depends on how accurate the mask of the contour is .这种方法的精度取决于轮廓掩码的精度

Although it is an iterative approach it doesn't add up too much time to the processing for me.尽管它是一种迭代方法,但它不会为我的处理增加太多时间。 One thing you can do is to process your images/frames in parallel .您可以做的一件事是并行处理图像/帧

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM