python按钮居中,python – 如何使MRI图像居中

我在MRI上工作.问题是图像并不总是居中.此外,患者身体周围通常有黑带.

我希望能够移除黑色边框并使患者的身体居中,如下所示:

kj6eq.jpg

SoJEK.jpg

我已经尝试通过读取像素表来确定患者身体的边缘,但我还没有提出任何非常确定的结论.

事实上,我的解决方案仅适用于50%的图像…我没有看到任何其他方法来做到这一点……

开发环境:Python3.7 OpenCV3.4

最佳答案 我不确定这是执行此操作的标准或最有效的方法,但它似乎有效:

# Load image as grayscale (since it's b&w to start with)

im = cv2.imread('im.jpg', cv2.IMREAD_GRAYSCALE)

# Threshold it. I tried a few pixel values, and got something reasonable at min = 5

_,thresh = cv2.threshold(im,5,255,cv2.THRESH_BINARY)

# Find contours:

im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

# Put all contours together and reshape to (_,2).

# The first "column" will be your x values of your contours, and second will be y values

c = np.vstack(contours).reshape(-1,2)

# Extract the most left, most right, uppermost and lowermost point

xmin = np.min(c[:,0])

ymin = np.min(c[:,1])

xmax = np.max(c[:,0])

ymax = np.max(c[:,1])

# Use those as a guide of where to crop your image

crop = im[ymin:ymax, xmin:xmax]

cv2.imwrite('cropped.jpg', crop)

你到底得到的是:

8a25778a34f7be3f479302664afd43af.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值