我有一个图像文件夹。我想将对象提取为固定大小的ROI,例如(100*100),并提取该对象的位置。我使用以下代码。但它只能将轮廓裁剪成各种矩形。我的目标是将对象提取到大小相等的帧中。我需要像下面的例子一样,输出补丁的形状是相等的。
import cv2
import glob
def crop_brain_contour(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # grayscale
cnts = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
#ROI_number = 0
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
ROI = image[y:y+h, x:x+w]
#cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
#ROI_number += 1
return ROI
i=0
for img in glob.glob('./image_data/*.bmp'):
cv_img = cv2.imread(img)
img_crop = crop_brain_contour(cv_img)
#img_resize = cv2.resize(img_crop,(224,224))
cv2.imwrite("./extracted_data_1/image%04i.bmp" %i,img_crop)
i += 1
在调用crop_brain_contour函数的位置进行以下更改:
相关问题 更多 >
编程相关推荐