问题描述
这是我的代码
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,Boxes[i],img)
cv2.imwrite("bound.jpg",img)
我想知道如何从中获得边界框?我尝试从其他线程的其他答案中使用ROI,但这似乎不起作用,它只会获取其他图像
(投资回报率示例) https://gyazo.com/5478ff4715311c0422d1dfbad7f03d18
以下是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,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,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
值裁剪原始图像。
对于绘制的每个满足scores[i] > 0.258
的矩形,请使用以上值在draw_boxes
函数中裁剪并保存原始图像的一部分。那就不用轮廓了。
如果有重叠的边界框,请使用Non-max Suppression
。