肿瘤cv2周围的绘图边界矩形

问题描述 投票:0回答:1

我正在使用硬编码的CNN进行一个预测MRI是否患有肿瘤的项目,现在下一步是在肿瘤周围绘制一个边界矩形。我能够从MRI中提取肿瘤,现在我想获得矩形的相对角以将肿瘤绑定到原始图中。

我通过代码获得的进展是:

import cv2
import matplotlib.pyplot as plt


def show_image(title, image):
    cv2.imshow(title, image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def show_image_plt(title, image, cmap = None):
    plt.figure(title)
    plt.imshow(image,cmap=cmap)
    plt.axis('off')
    plt.show()

def cvt_image_colorspace(image, colorspace = cv2.COLOR_BGR2GRAY):
    return cv2.cvtColor(image, colorspace)

def median_filtering(image, kernel_size=3):
    '''
    :param image: grayscale image
    :param kernel_size: kernel size should be odd number
    :return: blurred image
    '''

    return cv2.medianBlur(image, kernel_size)


def apply_threshold(image, **kwargs):
    '''
    :param image: image object
    :param kwargs: threshold parameters - dictionary
    :return:
    '''
    threshold_method = kwargs['threshold_method']
    max_value = kwargs['pixel_value']
    threshold_flag = kwargs.get('threshold_flag', None)
    if threshold_flag is not None:
        ret, thresh1 = cv2.adaptiveThreshold(image, max_value, threshold_method,cv2.THRESH_BINARY, 
kwargs['block_size'], kwargs['const'])
    else:
        ret, thresh1 = cv2.threshold(image, kwargs['threshold'], max_value, threshold_method)
    return thresh1

def sobel_filter(img,x,y,kernel_size = 3):
    return cv2.Sobel(img, cv2.CV_8U, x,y, ksize=kernel_size)

path=r"Assignment-3\Y4.jpg"
image = cv2.imread(path, 1)
show_image('Original image', image)

#Step one - grayscale the image
grayscale_img = cvt_image_colorspace(image)
#show_image('Grayscaled image', grayscale_img)

#Step two - filter out image
median_filtered = median_filtering(grayscale_img,5)
#show_image('Median filtered', median_filtered)


#testing threshold function
bin_image = apply_threshold(median_filtered,  **{"threshold" : 160,
                                                 "pixel_value" : 255,
                                                 "threshold_method" : cv2.THRESH_BINARY})
otsu_image = apply_threshold(median_filtered, **{"threshold" : 0,
                                                 "pixel_value" : 255,
                                                 "threshold_method" : cv2.THRESH_BINARY + 
cv2.THRESH_OTSU})


#Step 3a - apply Sobel filter
img_sobelx = sobel_filter(median_filtered, 1, 0)
img_sobely = sobel_filter(median_filtered, 0, 1)

# Adding mask to the image
img_sobel = img_sobelx + img_sobely+grayscale_img
#show_image('Sobel filter applied', img_sobel)

#Step 4 - apply threshold
# Set threshold and maxValue
threshold = 160
maxValue = 255

# Threshold the pixel values
thresh = apply_threshold(img_sobel,  **{"threshold" : 160,
                                                 "pixel_value" : 255,
                                                 "threshold_method" : cv2.THRESH_BINARY})
#show_image("Thresholded", thresh)


#Step 3b - apply erosion + dilation
#apply erosion and dilation to show only the part of the image having more intensity - tumor region
#that we want to extract
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(9,9))
erosion = cv2.morphologyEx(median_filtered, cv2.MORPH_ERODE, kernel)
#show_image('Eroded image', erosion)



dilation = cv2.morphologyEx(erosion, cv2.MORPH_DILATE, kernel)
#show_image('Dilatated image', dilation)

#Step 4 - apply thresholding
threshold = 160
maxValue = 255

# apply thresholding
new_thresholding = apply_threshold(dilation,  **{"threshold" : 160,
                                                 "pixel_value" : 255,
                                                 "threshold_method" : cv2.THRESH_BINARY})
show_image('Threshold image after erosion + dilation', new_thresholding)

给定MRI的输出图像是:

Original ImageTumor

python-3.x image image-processing cv2
1个回答
1
投票

我认为最好的方法是知道像素不是黑色的地方>

pts = np.argwhere(new_thresholding>0)
y1,x1 = pts.min(axis=0)
y2,x2 = pts.max(axis=0)
new_thresholding_rect= cv2.rectangle(new_thresholding,(x1,y1),(x2,y2),(255,0,0),2)
show_image('Threshold image after erosion + dilation + Rectangle',new_thresholding_rect) 
© www.soinside.com 2019 - 2024. All rights reserved.