【问题标题】:how do crop specific part of image based on the coordinate boxes?如何根据坐标框裁剪图像的特定部分?
【发布时间】: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


【解决方案1】:

你的循环不正确。

classID= [1]
for i in classID:

在这里你基本上说for i in [1]:,它只为你的第一次检测而裁剪。相反,您应该遍历所有检测。假设您的其余代码是正确的,以下循环遍历所有登录 classID 的检测,并且仅在它属于 1 类时才进行裁剪。

for i in classID:
    if i!=1:
        continue

【讨论】:

  • 谢谢,我也这样做了,不幸的是,它仍然裁剪其中之一。
  • (x, y)(w, h) 的缩进不正确。
【解决方案2】:

问题是您在以下行中丢失了对原始图像的引用。

image = image[y:y + h, x:x + w]

相反,您可以为每个狗图像创建新变量

dog_img = image[y:y + h, x:x + w]

另外在写的时候,你写的是同名,所以会覆盖之前的图片实例,所以尽量把图片的名字改成动态的,比如dog1.jpg,dog2.jpg ...

    c = 0
    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
        dog_img = image[y:y + h, x:x + w]

        cv.imshow("Image", dog_img )

        path = '/path to folder'
        c +=1
        cv.imwrite(os.path.join(path, 'PImage'+str(c)+'.jpg'), dog_img )

        #
        cv.waitKey(0)

【讨论】:

  • 你能用代码解释一下动态图像输出吗,我解决这个问题的努力没有得到回报。
  • 我已经编辑了代码,如果问题解决,请采纳解决方案。
【解决方案3】:

我确实输入了 print(type(classID)) 它告诉我们

0
<class 'numpy.int64'>
1
<class 'numpy.int64'>
1
<class 'numpy.int64'>
0
<class 'numpy.int64'>

据我所知,我可以迭代可迭代对象,例如列表,但 int64 等单个值是不可迭代的。

我该如何解决这个问题?

【讨论】:

    猜你喜欢
    • 2022-07-24
    • 2020-04-16
    • 1970-01-01
    • 2021-09-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    相关资源
    最近更新 更多