【发布时间】: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