python - 如何使 MRI 图像居中
问题描述
我在做核磁共振。问题是图像并不总是居中。此外,患者身体周围常有黑色条带。
我已经尝试通过读取像素表来确定患者身体的边缘,但我还没有得出任何非常确凿的结论。
事实上,我的解决方案仅适用于 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)
你最终得到的是这样的:
推荐阅读
- spring-boot - 反应式弹簧云安全(使用 Keycloak):会话到期?
- python-3.x - 在 Python 中的文本文件中搜索通配符模式
- python - 假设我的主页中有一个搜索框和一个结果页面来显示结果,我如何在另一个视图中显示 django-filter 结果?
- c# - Quartz.Net 减少日志记录/删除特定消息 Serilog
- python - Django Ckeditor 文件上传 - 路径错误?
- python - 合并开始和结束每2行python
- powershell - 大型邮箱中的 Get-EXOMailboxFolderStatistics 超时
- r - 从R中的列表中选择许多项目
- java - Intellij 检测到已删除的 sdk/jdk。如何更新智能缓存?
- python - 如何使用 python bot 将本地 html 文件发送到 Telegram