我从事MRI。问题在于图像并不总是居中。另外,患者身体周围经常有黑带。
我已经尝试通过读取像素表来确定患者身体的边缘,但是我还没有得出任何非常确凿的结论。
实际上,我的解决方案仅能处理50%的图像...我看不到其他方法可以实现...
开发环境:Python3.7 + OpenCV3.4
答案 0 :(得分: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)
最后得到的是:
答案 1 :(得分:2)
执行此操作的方法有多种,这就是很多计算机视觉提示和技巧。
如果质量在中心,并且外部区域始终是黑色,则可以threshold图像,然后像以前一样找到边缘像素。我会在边框上添加10个像素,以调整阈值过程中的差异。
或者如果主体的大小始终相似,则you can find the centroid of the blob (white area in the threshold image), and then crop a fixed area around it。