x射线图像感兴趣区域的检测与分割

2024-05-19 13:12:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理一些X射线图像,我想从图像中检测和分割感兴趣的区域

考虑输入图像

enter image description here

我想检测图像中的方形形状,如图像中突出显示的那样

enter image description here

输出:感兴趣的区域看起来会像这样

enter image description here

这是我的代码,到目前为止我已经完成了

import cv2
import numpy as np
import pandas as pd
import os
from PIL import Image
import matplotlib.pyplot as plt 
from skimage.io import imread, imshow


img = cv2.imread('image.jpg',0)
imshow(img)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imshow(gray)

equ = cv2.equalizeHist(img)
imshow(equ)


img_resize = cv2.resize(img, (300, 300))
print(img_resize.shape)
figure_size = 9

new_image_gauss = cv2.GaussianBlur(img_resize, (figure_size, figure_size),0)
imshow(new_image_gauss)


img_edge = cv2.Canny(equ,100,200)

# show the image edges on the newly created image window
imshow(img_edge)


kernel = np.ones((5,5), np.uint8) 
img_erosion = cv2.erode(img_edge, kernel, iterations=1) 
img_dilation = cv2.dilate(img_edge, kernel, iterations=1) 
   
imshow(img_erosion) 

我得到的结果

enter image description hereenter image description here

请引导我

短暂性脑缺血发作


Tags: 图像imageimportimgsizeasnpcv2
1条回答
网友
1楼 · 发布于 2024-05-19 13:12:15

有一件事可能会有所帮助,那就是在进行精明的边缘检测之前,先对图像进行形态学梯度处理,以强调Python OpenCV中的边缘

输入:

enter image description here

import cv2
import numpy as np

# read image
img = cv2.imread("xray2.jpg")

# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# do morphology gradient
kernel = cv2.getStructuringElement(cv2.MORPH_RECT , (3,3))
morph = cv2.morphologyEx(gray, cv2.MORPH_GRADIENT, kernel)

# apply gain
morph = cv2.multiply(morph, 5)

# write results
cv2.imwrite("xray2_gradient_edges.jpg",morph)

# show lines
cv2.imshow("morph", morph)
cv2.waitKey(0)

enter image description here

相关问题 更多 >