введите описание изображения здесь [отредактировано] Я пытаюсь нарисовать несколько независимых полигонов через события мыши и функцию cv2.polylines (), но я не могу нарисовать их на одном и том же изображении и отделенный, всегда есть линия, соединяющая их. Кто-нибудь может мне помочь. Я новичок во всем этом. Заранее спасибо.
class PolygonDrawer(object):
def __init__(self, window_name):
self.window_name = window_name # Name for our window
self.done = False # Flag signalling we're done
self.current = (0, 0) # Current position, so we can draw the line-in-progress
self.points = [] # List of points defining our polygon
self.polygon = 1 # number of polygons
def on_mouse(self, event, x, y, buttons, user_param):
# Mouse callback that gets called for every mouse event (i.e. moving, clicking, etc.)
if self.done: # Nothing more to do
return
if event == cv2.EVENT_MOUSEMOVE:
# We want to be able to draw the line-in-progress, so update current mouse position
self.current = (x, y)
elif event == cv2.EVENT_LBUTTONDOWN:
self.current = (x, y)
# Left click means adding a point at current position to the list of points
print("Adding point #%d with position(%d,%d) to polygon number %d" % (len(self.points), x, y, self.polygon))
self.points.append((x, y))
elif event == cv2.EVENT_RBUTTONDOWN:
# Right click means we're done
print("Completing polygon with %d points." % len(self.polygons_points))
self.polygon = 1
self.done = True
elif event == cv2.EVENT_LBUTTONDBLCLK:
self.points.clear()
# double click left button adds new polygon
self.polygon = self.polygon + 1
self.points.append((x, y))
def run(self, path):
# read the image
img_run = cv2.imread(path, 0)
new_img_run = img_run.copy()
height = img_run.shape[0]
width = img_run.shape[1]
# Let's create our working window and set a mouse callback to handle events
cv2.namedWindow(self.window_name, flags=cv2.WINDOW_NORMAL)
cv2.imshow(self.window_name, img_run)
cv2.waitKey(1)
cv2.setMouseCallback(self.window_name, self.on_mouse)
while not self.done:
# This is our drawing loop, we just continuously draw new images
# and show them in the named window
if len(self.points) > 0:
# for i in range(len(self.polygons_points)):
# Draw all the current polygon segments
cv2.polylines(new_img_run, np.array([self.points]), False, FINAL_LINE_COLOR, 3)
# And also show what the current segment would look like
# cv2.line(self.list_img[1], self.points[-1], self.current, WORKING_LINE_COLOR, 5)
# Update the window
cv2.imshow(self.window_name, new_img_run)
# And wait 50ms before next iteration (this will pump window messages meanwhile)
if cv2.waitKey(50) == 27: # ESC hit
self.done = True
# User finished entering the polygon points, so let's make the final drawing
if len(self.points) > 0:
cv2.fillPoly(new_img_run, np.array([self.points]), FINAL_LINE_COLOR)
# creates the an all black image with the size of the original image
mask_poly = np.zeros((height, width), dtype=np.uint8)
# creates the polygon in the black image, creates the mask
cv2.fillPoly(mask_poly, np.array([self.points]), FINAL_LINE_COLOR)
# applies the original image over the mask, with the white polygon, making the image of the polygon
# we want
result_poly = np.bitwise_and(img_run, mask_poly)
Я включил код, который я адаптировал к своим потребностям, но он по-прежнему не рисует независимые многоугольники.