【发布时间】:2021-08-17 00:43:36
【问题描述】:
我需要图像预处理部分的帮助。我有一张患有阿尔茨海默病的大脑 MRI 图像。我需要从 MRI 中移除头盖骨(头骨),然后裁剪大脑周围的所有区域。我怎么能在python中做到这一点?与图像处理。我已经尝试过使用 openCV,非常感谢。
这是我尝试过的代码:
这里是代码
def crop_brain_contour(image, plot=False):
# Convert the image to grayscale, and blur it slightly
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_OTSU)
ret, markers = cv2.connectedComponents(thresh)
marker_area = [np.sum(markers==m) for m in range(np.max(markers)) if m!=0]
largest_component = np.argmax(marker_area)+1
brain_mask = markers==largest_component
brain_out = image.copy()
brain_out[brain_mask==False] = (0,0,0)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=2)
# Find contours in thresholded image, then grab the largest one
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
# extreme points
extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
# crop new image out of the original image using the four extreme points (left, right, top, bottom)
new_image = image[extTop[1]:extBot[1], extLeft[0]:extRight[0]]
return new_image
这些图片与我需要的相似:
谢谢你的帮助!!
【问题讨论】:
-
请从intro tour 重复on topic 和how to ask。 “告诉我如何解决这个编码问题”不是堆栈溢出问题。我们希望您做出诚实的尝试,然后然后就您的算法或技术提出一个具体的问题。我们修复损坏的代码。我们不会根据要求编写重要的模型。
-
请提供预期的minimal, reproducible example (MRE)。我们应该能够复制和粘贴您的代码的连续块,执行该文件,并重现您的问题以及跟踪问题点的输出。这让我们可以根据您的测试数据和所需的输出来测试我们的建议。显示中间结果与您的预期不同的地方。
-
@Prune 我已经进行了更改,并清除了问题所在!如果你能指导我,我会很有帮助。
-
提供一个高分辨率图像来处理怎么样!
-
@fmw42 我需要裁剪黑色区域以减少图像的噪点。它最终会给我很高的准确性
标签: python opencv image-processing deep-learning object-detection