Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为多幅图像提取固定大小的ROI?_Python_Opencv_Machine Learning_Image Processing_Opencv Contour - Fatal编程技术网

Python 如何为多幅图像提取固定大小的ROI?

Python 如何为多幅图像提取固定大小的ROI?,python,opencv,machine-learning,image-processing,opencv-contour,Python,Opencv,Machine Learning,Image Processing,Opencv Contour,我有一个图像文件夹。我想将对象提取为固定大小的ROI,例如(100*100),并提取该对象的位置。我使用以下代码。但它只能将轮廓裁剪成各种矩形。我的目标是将对象提取到大小相等的帧中。我需要像下面的例子一样,输出补丁的形状是相等的。 在调用crop_brain_contour函数的位置进行以下更改: desired_width, desired_height = (100, 100) final_img = np.zeros((desired_height, desired_width, 3),

我有一个图像文件夹。我想将对象提取为固定大小的ROI,例如(100*100),并提取该对象的位置。我使用以下代码。但它只能将轮廓裁剪成各种矩形。我的目标是将对象提取到大小相等的帧中。我需要像下面的例子一样,输出补丁的形状是相等的。


在调用crop_brain_contour函数的位置进行以下更改:

desired_width, desired_height = (100, 100)
final_img = np.zeros((desired_height, desired_width, 3), dtype="uint8")
img_crop = crop_brain_contour(cv_img)
h,w = img_crop.shape[:2]
#Make sure h < desired_height and w < desired_width
x1 = int(desired_width/2 - w/2)
y1 = int(desired_height/2 - h/2)
x2 = x1 + w
y2 = y1 + h

final_img[y1:y2, x1:x2, :] = img_crop

# Write the final image
cv2.imwrite("./extracted_data_1/image%04i.bmp" %i,final_img)
所需的_宽度,所需的_高度=(100100)
final\u img=np.zero((所需高度,所需宽度,3),dtype=“uint8”)
img\u裁剪=裁剪大脑轮廓(cv\u img)
h、 w=img_裁剪形状[:2]
#确保h<所需高度,w<所需宽度
x1=int(所需宽度/2-w/2)
y1=整数(所需高度/2-h/2)
x2=x1+w
y2=y1+h
最终img[y1:y2,x1:x2,:]=img作物
#写出最终的图像
cv2.imwrite(“./提取的数据\u 1/image%04i.bmp”%i,最终\u img)

我不确定你所说的“我需要为所有图像固定形状”是什么意思。也许可以重新设计这个问题,以便更清楚地理解你想要什么。非常感谢你的善意指导。我不清楚你想要小、中、大的是什么?是否希望每个输入图像有3个输出图像?问题是某物是正方形的,但你想要它是矩形的吗?对不起,我弄错了。现在我说得更清楚了。实际上,我需要一个图像一个输出,所有输出图像的形状/大小都相同,例如(100*100)。非常感谢,非常感谢。我正试着告诉你。塔妮亚,你能找到它工作吗?我对我的答案作了修改。确保您通过了所有3个通道的crop_brain_轮廓功能。如果您觉得答案有帮助,请向上投票并接受它,以便其他人可以轻松找到。final_img[y1:y2,x1:x2]=img_裁剪值错误:无法将输入数组从形状(113,77,3)广播到形状(6,77,3)。我有这些类型的错误。分叉,非常感谢。现在我想提取物体的位置。塔妮娅,你说的物体的位置是什么意思?对象相对于原始图像的位置已经通过x,y,w,h=cv2.boundingRect(c)提取出来。您可以使用元组将该信息返回给调用代码,例如返回(ROI,(x,y,w,h))
desired_width, desired_height = (100, 100)
final_img = np.zeros((desired_height, desired_width, 3), dtype="uint8")
img_crop = crop_brain_contour(cv_img)
h,w = img_crop.shape[:2]
#Make sure h < desired_height and w < desired_width
x1 = int(desired_width/2 - w/2)
y1 = int(desired_height/2 - h/2)
x2 = x1 + w
y2 = y1 + h

final_img[y1:y2, x1:x2, :] = img_crop

# Write the final image
cv2.imwrite("./extracted_data_1/image%04i.bmp" %i,final_img)