[英]Labeling Medical Image
讓我們將您的圖像分成兩個組成部分,第一個image.png
:
其次, mask.png
:
現在您可以在終端中使用ImageMagick而無需編寫任何代碼。 你有幾個選擇。 您可以:
讓我們先讓它們透明,這樣我們就可以有效地將蒙版復制到圖像中,並將其視為 alpha/透明層:
magick image.png mask.png -compose copyalpha -composite result.png
現在讓我們通過在每個像素位置選擇原始圖像和蒙版中較暗的一個來使它們變黑 - 因此darken
混合模式:
magick image.png mask.png -compose darken -composite result.png
請注意,如果您使用第一種技術,那么看起來透明的原始信息仍然在圖像中並且可以被檢索——所以不要使用這種技術來隱藏機密信息。
如果你想在 PIL 中使用 Python 的透明方法,你可以這樣做:
from PIL import Image
# Read image and mask as PIL Images
im = Image.open('image.png').convert('RGB')
ma = Image.open('mask.png').convert('L')
# Merge in mask as alpha channel and save
im.putalpha(ma)
im.save('result.png')
或者,使用OpenCV和Numpy 的透明方法:
import cv2
import numpy as np
# Open image and mask as NMumoy arrays
im = cv2.imread('image.png')
ma = cv2.imread('mask.png', cv2.IMREAD_GRAYSCALE)
# Merge mask in as alpha channel and save
res = np.dstack((im,ma))
cv2.imwrite('result.png', res)
如果要在 Python 和 PIL/Pillow 中使用 blacken 方法,請使用:
from PIL import Image, ImageChops
# Read image and mask as PIL Images
im = Image.open('image.png').convert('RGB')
ma = Image.open('mask.png').convert('RGB')
# Choose darker image at each pixel location and save
res = ImageChops.darker(im, ma)
res.save('result.png')
如果要在OpenCV和 Numpy 中使用 blacken 方法,請使用上面的代碼,但將np.dstack()
行替換為:
res = np.minimum(im, ma[...,np.newaxis])
我強烈推薦ITK - SNAP來完成這項任務。 您可以使用某些標簽手動標記您的輸入圖像(在您的示例中為 1 為前景,0 為背景)並非常舒適地導出地面實況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.