Python 如何从边界框中获取图像
如何从该图像中提取边界框? 这是我的密码Python 如何从边界框中获取图像,python,tensorflow,opencv,object-detection-api,bounding-box,Python,Tensorflow,Opencv,Object Detection Api,Bounding Box,如何从该图像中提取边界框? 这是我的密码 tf.disable_v2_behavior() import cv2 import pathlib model_path = "/Users/ebayb/Desktop/ndir/Model" # path to the folder containing images image_path = "/Users/ebayb/Desktop/top-cube/testing/2" session = tf.S
tf.disable_v2_behavior()
import cv2
import pathlib
model_path = "/Users/ebayb/Desktop/ndir/Model"
# path to the folder containing images
image_path = "/Users/ebayb/Desktop/top-cube/testing/2"
session = tf.Session(graph=tf.Graph())
tf.saved_model.loader.load(session, ['serve'], model_path)
# extract the coordinates of the rectange that's to be drawn on the image
def draw_boxes(height, width, box, img):
# starting coordinates of the box
ymin = int(max(1, (box[0] * height)))
xmin = int(max(1, (box[1] * width)))
# last coordinates of the box
ymax = int(min(height, (box[2] * height)))
xmax = int(min(width, (box[3] * width)))
coordinates_list=[ymin, ymax, xmin, xmax]
print(f"cor: {coordinates_list}")
# draw a rectange using the coordinates
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (10, 255, 0), 2)
for file in pathlib.Path(image_path).iterdir():
# get the current image path
current_image_path = r"{}".format(file.resolve())
img_bytes = open(current_image_path, 'rb').read()
result = session.run(['detection_boxes:0', 'detection_scores:0'], feed_dict={'encoded_image_string_tensor:0': [img_bytes]})
boxes = result[0][0]
scores = result[1][0]
print("For file {}".format(file.stem))
img = cv2.imread(current_image_path)
imH, imW, _ = img.shape
for i in range(len(scores)):
# only consider a detected object if it's probability is above 25%
if scores[i] > 0.258:
print("The box {} has probability {}".format(boxes[i], scores[i]))
draw_boxes(imH, imW, boxes[i], img)
cv2.imwrite("bound.jpg", img)
我想知道如何从中得到边界框?我试过在其他线程上使用其他答案的ROI,但这似乎不起作用,只会得到其他图像
(ROI执行此操作的示例)
以下是ROI的代码:
import numpy as np
# Load image, grayscale, Otsu's threshold
image = cv2.imread('test.jpg')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 100, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Find contours, obtain bounding box, extract and save ROI
ROI_number = 0
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
ROI = original[y:y+h, x:x+w]
cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
ROI_number += 1
cv2.imshow('image', image)
cv2.waitKey()
这是我正在使用的测试图像
您已经在第一个代码中绘制了矩形。如果需要矩形区域,请修改第一个代码,以使用
xmin、ymin、xmax、ymax
值裁剪原始图像
对于符合评分[i]>0.258的每个矩形,使用上述值在绘图框中裁剪原始图像的一部分并保存。那就不需要使用等高线了
对重叠的边界框(如果有)使用非最大抑制