【问题标题】:Is there any idea to mark multiple positive objects on the same image?有没有办法在同一张图片上标记多个正面对象?
【发布时间】:2019-08-27 20:59:12
【问题描述】:

我的代码能够使用矩形框标记正样本,并在按下回车键时切换到下一个空格,但我还想在单个图像上标记多个对象该怎么办?

它会在左键单击时创建对象标记框。

import numpy as np
import cv2

class BoxSelector(object):
    def __init__(self, image, window_name,color=(0,0,255)):
        #store image and an original copy
        self.image = image
        self.orig = image.copy()

        #capture start and end point co-ordinates
        self.start = None
        self.end = None

        #flag to indicate tracking
        self.track = False
        self.color = color
        self.window_name = window_name

        #hook callback to the named window
        cv2.namedWindow(self.window_name)
        cv2.setMouseCallback(self.window_name,self.mouseCallBack)

    def mouseCallBack(self, event, x, y, flags, params):
        #start tracking if left-button-clicked down
        if event==cv2.EVENT_LBUTTONDOWN:
            self.start = (x,y)
            self.track = True

        #capture/end tracking while mouse-move or left-button-click released
        elif self.track and (event==cv2.EVENT_MOUSEMOVE or event==cv2.EVENT_LBUTTONUP):
            self.end = (x,y)
            if not self.start==self.end:
                self.image = self.orig.copy()
                #draw rectangle on the image
                cv2.rectangle(self.image, self.start, self.end, self.color, 2)
                if event==cv2.EVENT_LBUTTONUP:
                    self.track=False

            #in case of clicked accidently, reset tracking
            else:
                self.image = self.orig.copy()
                self.start = None
                self.track = False
            cv2.imshow(self.window_name,self.image)

    @property
    def roiPts(self):
        if self.start and self.end:
            pts = np.array([self.start,self.end])
            s = np.sum(pts,axis=1)
            (x,y) = pts[np.argmin(s)]
            (xb,yb) = pts[np.argmax(s)]
            return [(x,y),(xb,yb)]
        else:
            return []

这是另一个循环遍历每个图像并收集注释的 python 文件。

for imagePath in list_images(args["dataset"]):

    #load image and create a BoxSelector instance
    image = cv2.imread(imagePath)
    bs = BoxSelector(image,"Image")
    cv2.imshow("Image",image)
    cv2.waitKey(0)

    #order the points suitable for the Object detector
    pt1,pt2 = bs.roiPts
    (x,y,xb,yb) = [pt1[0],pt1[1],pt2[0],pt2[1]]
    annotations.append([int(x),int(y),int(xb),int(yb)])
    imPaths.append(imagePath)

我想标记多个对象,以便通过按任意键我可以在同一图像上标记另一个正样本。

【问题讨论】:

    标签: python numpy object-detection cv2


    【解决方案1】:

    试试这样的:

    for imagePath in list_images(args["dataset"]):
    
        #load image and create a BoxSelector instance
        image = cv2.imread(imagePath)
        bs = BoxSelector(image,"Image")
    
    
        while (1):
           cv2.imshow("Image",image)
            k = cv2.waitKey(10) & 0xFF
            if k == ord('s'): 
                # press s, do stuff
                # order the points suitable for the Object detector
                pt1,pt2 = bs.roiPts
                (x,y,xb,yb) = [pt1[0],pt1[1],pt2[0],pt2[1]]
                annotations.append([int(x),int(y),int(xb),int(yb)])
                imPaths.append(imagePath)   
    
            if k == ord('r'):
                # press 'r',do stuff
                break # break skip loop and go to next image
            elif k == 27: # press esc to exit
               exit(0)
    

    如果您想在多个会话中拆分对象标记,我还建议您枚举数据集。

    start_from_image = 0
    for counter, image in enumerate(list_images(args["dataset"])):
    
        if counter >= start_from_image:
    

    【讨论】:

      猜你喜欢
      • 2023-01-17
      • 2017-02-11
      • 2019-08-27
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 2015-12-21
      • 2016-10-23
      • 1970-01-01
      相关资源
      最近更新 更多