Opencv 如何将边界框导出为.jpg

Opencv 如何将边界框导出为.jpg,opencv,tensorflow,object-detection-api,Opencv,Tensorflow,Object Detection Api,对于我的项目,我想将对象检测API找到的边界框保存为.jpg,以便输入另一个CNN以进行进一步分类 以下是我的代码(源自EdjeeElectronics GitHub): 有人问了一个类似的问题,但我不知道如何将其应用于Tensorflow对象检测API 谢谢大家! 我在vis\u util中找到了在图像上绘制边界框的功能。试试这个: #create a white back ground image with the same shape as image white_bg_img = 255

对于我的项目,我想将对象检测API找到的边界框保存为.jpg,以便输入另一个CNN以进行进一步分类

以下是我的代码(源自EdjeeElectronics GitHub):

有人问了一个类似的问题,但我不知道如何将其应用于Tensorflow对象检测API


谢谢大家!

我在
vis\u util
中找到了在图像上绘制边界框的功能。试试这个:

#create a white back ground image with the same shape as image
white_bg_img = 255*np.ones(image.shape, np.uint8)
vis_util.draw_bounding_boxes_on_image(
    white_bg_img ,
    np.squeeze(boxes),
    color='red',
    thickness=4)
cv2.imwrite("bounding_boxes.jpg", white_bg_img )
在边界框内绘制图像

boxes = np.squeeze(boxes)
for i in range(len(boxes)):
    ymin = box[i,0]
    xmin = box[i,1]
    ymax = box[i,2]
    xmax = box[i,3]
    roi = image[ymin:ymax,xmin:xmax].copy()
    cv2.imwrite("box_{}.jpg".format(str(i)), roi)

保存文件将类似于box_1.jpg、box_2.jpg…

我在
vis_util
中找到了在图像上绘制边界框的功能。试试这个:

#create a white back ground image with the same shape as image
white_bg_img = 255*np.ones(image.shape, np.uint8)
vis_util.draw_bounding_boxes_on_image(
    white_bg_img ,
    np.squeeze(boxes),
    color='red',
    thickness=4)
cv2.imwrite("bounding_boxes.jpg", white_bg_img )
在边界框内绘制图像

boxes = np.squeeze(boxes)
for i in range(len(boxes)):
    ymin = box[i,0]
    xmin = box[i,1]
    ymax = box[i,2]
    xmax = box[i,3]
    roi = image[ymin:ymax,xmin:xmax].copy()
    cv2.imwrite("box_{}.jpg".format(str(i)), roi)
保存文件将类似于box_1.jpg、box_2.jpg…

这将起作用

enter code here
box = np.squeeze(boxes)
for i in range(len(boxes)):
    ymin = (int(box[i,0]*height))
    xmin = (int(box[i,1]*width))
    ymax = (int(box[i,2]*height))
    xmax = (int(box[i,3]*width))
    print(ymin,xmin,ymax,xmax)
    roi =image[ymin:ymax,xmin:xmax].copy()
这会奏效的

enter code here
box = np.squeeze(boxes)
for i in range(len(boxes)):
    ymin = (int(box[i,0]*height))
    xmin = (int(box[i,1]*width))
    ymax = (int(box[i,2]*height))
    xmax = (int(box[i,3]*width))
    print(ymin,xmin,ymax,xmax)
    roi =image[ymin:ymax,xmin:xmax].copy()
我遵循这一点,它起了作用。添加以下代码:

min_score_thresh=0.60
true_boxes = boxes[0][scores[0] > min_score_thresh]
for i in range(true_boxes.shape[0]):
    ymin = int(true_boxes[i,0]*height)
    xmin = int(true_boxes[i,1]*width)
    ymax = int(true_boxes[i,2]*height)
    xmax = int(true_boxes[i,3]*width)

    roi = image[ymin:ymax,xmin:xmax].copy()
    cv2.imwrite("box_{}.jpg".format(str(i)), roi)
确保定义了图像的真实高度和宽度

我遵循了这一点,它奏效了。添加以下代码:

min_score_thresh=0.60
true_boxes = boxes[0][scores[0] > min_score_thresh]
for i in range(true_boxes.shape[0]):
    ymin = int(true_boxes[i,0]*height)
    xmin = int(true_boxes[i,1]*width)
    ymax = int(true_boxes[i,2]*height)
    xmax = int(true_boxes[i,3]*width)

    roi = image[ymin:ymax,xmin:xmax].copy()
    cv2.imwrite("box_{}.jpg".format(str(i)), roi)

确保定义了图像的真实高度和宽度

您只想要边界框吗?像白色背景中的方框?可能我不够精确:我只想导出图片中方框所包含的部分。我已经编辑了我的答案,请勾选。您只想导出边框吗?像白色背景中的框?可能我不够精确:我只想导出框中包含的图片部分。我已经编辑了我的答案,请检查出来谢谢你的帮助!!:)我不得不稍微调整一下,因为盒子的坐标是标准化的。因此,您必须将这些值乘以图像的分辨率谢谢您的帮助!!:)我不得不稍微调整一下,因为盒子的坐标是标准化的。因此,您必须将这些值乘以图像的分辨率,而这个代码片段可能是解决方案,它确实有助于提高您的文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。虽然此代码片段可能是解决方案,但确实有助于提高您文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。