【发布时间】:2020-06-22 21:40:01
【问题描述】:
我想根据检测到的对象(classID=1)的坐标框裁剪图像。
可能有多个具有相同 id 或其他类的对象。 我的问题是我的代码只返回一个裁剪图像,我如何返回所有 ClassID=1 的裁剪图像?
我对 ClassID=1 感兴趣的课程共有 6 个。
# initializing bounding boxes, confidences, and classIDs.
boxes = []
confidences = []
classIDs = []
for output in layersOutputs:
# loop over each of the detections
for detection in output:
# extract the class ID and confidence
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
# filter out weak predictions
if confidence > c_threshold:
box = detection[0:4] * np.array([W, H, W, H])
(centerX, centerY, width, height) = box.astype("int")
#coordinates
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))
# update bounding box coordinates, confidences, classIDs
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)
# applying non maximum suppression
ind = cv.dnn.NMSBoxes(boxes, confidences, c_threshold, nms)
if len(ind) > 0:
# loop over the indexes that we want to keep
for i in ind.flatten():
# extract the bounding box coordinates
(x, y) = (boxes[1][0], boxes[1][1])
(w, h) = (boxes[1][2], boxes[1][3])
for i in classIDs:
if i != 1:
continue
# extract the bounding box coordinates
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
# crop that part of image which contains desired object
image = image[y:y + h, x:x + w]
cv.imshow("Image", image)
path = '/path to folder'
cv.imwrite(os.path.join(path, 'PImage.jpg'), image)
#
cv.waitKey(0)
已编辑: 正如您所见,这张图片中有多种动物,我正在尝试裁剪包含狗的图像的一部分。我已经得到了与狗部分相关的坐标边界框(这意味着我知道照片中包含狗的矩形的位置在哪里)
我想裁剪我在图像中指示的那些矩形。狗的类 id=1。我有类 cat 和其他具有不同索引的动物。
【问题讨论】:
-
请添加示例图像和最小可重现示例
-
我编辑了代码并放了图片
标签: python numpy opencv image-processing deep-learning