资源简介

基于opencv窗口的多边型标注工具。输入bmp,jpg,jpeg等格式图片,鼠标点击生成多边型区域,空格保存json文本文件。适用于多边型标注,或生成mask图。

资源截图

代码片段和文件信息

# coding=utf-8
import numpy as np
import cv2
import glob
import os
import json


class Draw(object):
    def __init__(self img save_path d=2 draw=False):
        self.img = img.copy()
        self.img_copy = img.copy()
        self.img_add = img.copy()
        self.draw = draw
        self.img_path = save_path
        self.file_type = os.path.splitext(save_path)[-1].lower()
        self.save_path_json = save_path.replace(self.file_type ‘.json‘)
        self.save_path_mask = save_path.replace(self.file_type ‘.jpg‘)
        self.img_show = img.copy()
        self.cls = 0
        self.annotate = []
        self.alpha = 0.7
        self.beta = 1 - self.alpha
        self.gamma = 0
        self.annotates = []

    def draw_rectangle(self event x y flags param):
        if event == cv2.EVENT_LBUTTONDBLCLK:
            if len(self.annotate) > 0:
                if ((x - self.annotate[0][0]) ** 2 + (y - self.annotate[0][1]) ** 2 < 300):
                    (x y) = self.annotate[0]
                    self.draw = True
            self.annotate.append((x y))
            if self.draw == True:
                self.annotates.append([self.cls self.annotate])
                self.annotate = []
            # print(self.annotate)
            # self.start_x self.start_y = x y

        elif flags == cv2.EVENT_FLAG_CTRLKEY + cv2.EVENT_LBUTTONDOWN:
            self.cls += 1
            # print(‘Current cls:%d‘ % (self.cls))
        elif flags == cv2.EVENT_FLAG_CTRLKEY + cv2.EVENT_RBUTTONDOWN:
            if self.cls >= 1:
                self.cls -= 1
            else:
                self.cls = 0
            # print(‘Current cls:%d‘ % (self.cls))

    def draw_window(self):
        cv2.namedWindow(‘image‘ 2)
        cv2.setMouseCallback(‘image‘ self.draw_rectangle)
        while True:
            # annotate = np.array(self.annotate)
            img_show = self.img_copy.copy()
            for i in range(len(self.annotate)):
                cv2.circle(img_show self.annotate[i] 1 (0 255 255) -1)
            for i in range(len(self.annotate) - 1):
                cv2.line(img_show self.annotate[i] self.annotate[i + 1] (0 255 255) 10)
            self.img_show = img_show
            for cls annotate in self.annotates:
                for i in range(len(annotate) - 1):
                    cv2.line(img_show annotate[i] annotate[i + 1] (0 255 255) 1)
                    mask = cv2.polylines(img_show [np.array(annotate)] True (0 255 255))
                    # cv2.imshow(‘mask‘ mask)
                    mask2 = cv2.

评论

共有 条评论