我正在研究图像分类问题,现在尝试裁剪由OCT扫描的光学图像,以仅提取多个图像的视网膜层(大约85000张图像)。所有图像都是不同的(附有少量参考)
请解决我的代码以应用所有可以裁剪感兴趣区域的图像。
我试图找到轮廓并裁剪图像..但是无法这样做,因为图像的背景不是黑色,并且也有像素 (注意:图像不是灰度的)
我的代码:
new_filepath = "/home/lebenpc/magick_octid"
for new_name in os.listdir(new_filepath):
if "." not in new_name:
continue
end = new_name.split(".")[1]
if end not in ["jpeg"]:
continue
try:
# Attempt to open an image file
image= cv2.imread(os.path.join(new_filepath,new_name))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7,7), 0)
# threshold the image, then perform a series of erosions +
# dilations to remove any small regions of noise
_, thresh = cv2.threshold(blur, 40, 255, cv2.THRESH_BINARY)
# find contours in thresholded image, then grab the largest
# one
contours, _ = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_KCOS)
c = max(contours, key = cv2.contourArea)
x,y,w,h = cv2.boundingRect(c)
crop =image[y:y+h,x:x+w]
name,extension = os.path.splitext(new_name)
cv2.imwrite(os.path.join("/home/lebenpc/abc", name + '.jpeg'),crop)
except IOError:
# Report error, and then skip to the next argument
print ("Problem opening",new_filepath,":")
我期待可以应用于所有图像的代码