Home > Software design >  Repeated drawings of bounding box
Repeated drawings of bounding box

Time:09-05

I would like to simply make a bounding box that uses points(any amount) to follow my mouse around.

Error being when I draw the bounding box currently it repeats all the previous drawings and have repeated bounding boxes enter image description here

import cv2
import numpy as np

width=60
height=80

    
class BoundingBoxWidget(object):
    def __init__(self):
        self.original_image = cv2.imread('parking_lot.png')
        self.clone = self.original_image.copy()
        cv2.namedWindow('image')
        cv2.setMouseCallback('image', self.extract_coordinates)

        # Bounding box reference points
        self.image_coordinates = []

    def extract_coordinates(self, event, x, y, flags, parameters):
        self.image_coordinates=[[x,y],[x width,y],[x (2*width),y height],[(x width),y height]]
        print(self.image_coordinates)
        points = np.array(self.image_coordinates)
        #points = points.reshape((-1, 1, 2))
        cv2.polylines(self.clone,[points] ,True,(255,0,0),2)
        cv2.imshow("image", self.clone) 
    def show_image(self):
        return self.clone

def main():
    boundingbox_widget = BoundingBoxWidget()

    while True:
        cv2.imshow('image', boundingbox_widget.show_image())
        key = cv2.waitKey(1)

        # Close program with keyboard 'q'
        if key == ord('q'):
            cv2.destroyAllWindows()
            exit(1)
if __name__ == '__main__':
    main()

CodePudding user response:

You had already the right idea creating a clone from the original image, but just forgot to set the clone back to the original image before painting the new bounding-box upon it. Adding one line at the position as below solves the problem:

        self.clone = self.original_image.copy()
        cv2.polylines(self.clone,[points] ,True,(255,0,0),2)
        cv2.imshow("image", self.clone) 

and ... if you change the line to the line below the bounding box will perfectly fit to the parking slots:

        self.image_coordinates=[[x,y],[x width,y],[x (2*width) 20,y height],[(x width) 20,y height]]
  • Related