分享一个图像上画框程序

news/2024/11/9 2:59:27/

一、前言

分享一个图像上画框程序,代码转自 python opencv鼠标画矩形框之cv2.rectangle()函数 - 华域联盟

二、代码

# -*- coding: utf-8 -*-
"""
Created on Fri Jul 23 14:35:37 2021"""# -*- coding: utf-8 -*-import copy
import cv2
import numpy as npWIN_NAME = 'draw_rect'class Rect(object):def __init__(self):self.tl = (0, 0)self.br = (0, 0)def regularize(self):"""make sure tl = TopLeft point, br = BottomRight point"""pt1 = (min(self.tl[0], self.br[0]), min(self.tl[1], self.br[1]))pt2 = (max(self.tl[0], self.br[0]), max(self.tl[1], self.br[1]))self.tl = pt1self.br = pt2class DrawRects(object):def __init__(self, image, color, thickness=1):self.original_image = imageself.image_for_show = image.copy()self.color = colorself.thickness = thicknessself.rects = []self.current_rect = Rect()self.left_button_down = Falseself.boxes = []  #保存好画好的框@staticmethoddef __clip(value, low, high):"""clip value between low and highParameters----------value: a numbervalue to be clippedlow: a numberlow limithigh: a numberhigh limitReturns-------output: a numberclipped value"""output = max(value, low)output = min(output, high)return outputdef shrink_point(self, x, y):"""shrink point (x, y) to inside image_for_showParameters----------x, y: int, intcoordinate of a pointReturns-------x_shrink, y_shrink: int, intshrinked coordinate"""height, width = self.image_for_show.shape[0:2]x_shrink = self.__clip(x, 0, width)y_shrink = self.__clip(y, 0, height)return (x_shrink, y_shrink)def append(self):"""add a rect to rects list"""self.rects.append(copy.deepcopy(self.current_rect))def pop(self):"""pop a rect from rects list"""rect = Rect()if self.rects:rect = self.rects.pop()return rectdef reset_image(self):"""reset image_for_show using original image"""self.image_for_show = self.original_image.copy()def draw(self):"""draw rects on image_for_show"""for rect in self.rects:cv2.rectangle(self.image_for_show, rect.tl, rect.br,color=self.color, thickness=self.thickness)def draw_current_rect(self):"""draw current rect on image_for_show"""cv2.rectangle(self.image_for_show,self.current_rect.tl, self.current_rect.br,color=self.color, thickness=self.thickness)def onmouse_draw_rect(event, x, y, flags, draw_rects):if event == cv2.EVENT_LBUTTONDOWN:# pick first point of rectprint('pt1: x = %d, y = %d' % (x, y))draw_rects.left_button_down = Truedraw_rects.current_rect.tl = (x, y)if draw_rects.left_button_down and event == cv2.EVENT_MOUSEMOVE:# pick second point of rect and draw current rectdraw_rects.current_rect.br = draw_rects.shrink_point(x, y)draw_rects.reset_image()draw_rects.draw()draw_rects.draw_current_rect()if event == cv2.EVENT_LBUTTONUP:# finish drawing current rect and append it to rects listdraw_rects.left_button_down = Falsedraw_rects.current_rect.br = draw_rects.shrink_point(x, y)print('pt2: x = %d, y = %d' % (draw_rects.current_rect.br[0],draw_rects.current_rect.br[1]))draw_rects.boxes.append([ draw_rects.current_rect.tl[0],  draw_rects.current_rect.tl[1], draw_rects.current_rect.br[0]  , draw_rects.current_rect.br[1] ])draw_rects.current_rect.regularize()draw_rects.append()if (not draw_rects.left_button_down) and event == cv2.EVENT_RBUTTONDOWN:# pop the last rect in rects listdraw_rects.pop()draw_rects.reset_image()draw_rects.draw()if __name__ == '__main__':image = np.zeros((256, 256, 3), np.uint8)draw_rects = DrawRects(image, (0, 255, 0), 1)cv2.namedWindow(WIN_NAME, 0)cv2.setMouseCallback(WIN_NAME, onmouse_draw_rect, draw_rects)while True:cv2.imshow(WIN_NAME, draw_rects.image_for_show)key = cv2.waitKey(30)if key == 27:  # ESCbreakcv2.destroyAllWindows()print( draw_rects.boxes )

三结果

运行,可在黑色图像上画2个框,并得到2个框的左上角和右下角坐标

 


http://www.ppmy.cn/news/544419.html

相关文章

opencv--在图片上画框(2)

参考网站&#xff1a;参考网站 //编译步骤&#xff1a; mkdir build // cd build // sudo cmake .. // sudo make // ./draw // 运行----点击任意非ESC按键----鼠标开始框选 #include<iostream> #include <cstdio> #include <cstdlib> #include <opencv…

矩形画框控件代码

wpf中的代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using …

创意画框设计

完成如下画框&#xff0c;需要用到线性渐变background-image:linear-gradient&#xff0c;盒子阴影设置box-shadow以及透明度设置opacity。 代码详细描述请看下图&#xff1a; 代码块 <!doctype html> <html> <head> <meta charset"utf-8"&g…

opencv如何在图像上画框

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、基于普通矩形Rect画框二、基于旋转矩形RotatedRect画框 前言 第一种方法基于矩形rect画框&#xff0c;第二种基于旋转矩形rotatedrect画框 一、基于普通矩…

第37.1节 框选-绘制框选框

目录 首先祝大家国庆快乐本节内容实现要点以下为全部代码 首先祝大家国庆快乐 本节内容 本节开始介绍鼠标选择的操作。正常情况下我们都会在场景中使用鼠标框选一些物体。本节我们绘制了框选框&#xff0c;程序启动后&#xff0c;按住键盘上的左CTRL键&#xff0c;然后鼠标在…

python PyQt5如何绘制矩形框?(画框/绘框)

参考代码&#xff1a; from PyQt5.QtWidgets import QWidget, QApplication, QLabel from PyQt5.QtCore import QRect, Qt from PyQt5.QtGui import QImage, QPixmap, QPainter, QPen, QGuiApplication import cv2 import sys class MyLabel(QLabel):x0 0y0 0x1 0y1 0flag…

使用opencv画框

在平常使用图像处理中的时候&#xff0c;例如检测追踪的时候&#xff0c;我们经常需要用到画图函数&#xff0c;例如手动标定一个目标&#xff0c;调用鼠标时间&#xff0c;然后进行追踪&#xff0c;或者检测完一个目标&#xff0c;画上矩形或者其他形状&#xff0c;然后进行追…

一系列点画框(不规则框)

做个记录&#xff1a;先求点的凸包&#xff0c;再画 void draw_hull() {int w 600;int h 500;cv::Mat polyImg cv::Mat::zeros(w,h,CV_8UC3);std::vector<Point> planevKeys {{159 , 64}, {136 , 74 }, {178 , 34 }, {132 , 89 },{124 , 112}, {178 , 50 },{101 , …