opencv在图像上画矩形,python opencv画图

  opencv在图像上画矩形,python opencv画图

  这篇文章主要介绍了如何利用计算机编程语言和开放计算机视觉实现在图像上绘制任意大小的矩形,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下

  话不多说,直接上代码

  导入副本

  导入cv2

  将数组作为铭牌导入

  WIN_NAME=draw_rect

  类矩形(对象):

  def __init__(self):

  self.tl=(0,0)

  self.br=(0,0)

  定义正规化(自我):

  确保tl=左上点,br=右下点

  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=pt1

  self.br=pt2

  类DrawRects(对象):

  def __init__(self,image,color,thickness=1):

  self.original_image=图像

  self.image_for_show=image.copy()

  自我颜色=颜色

  自身厚度=厚度

  self.rects=[]

  self.current_rect=Rect()

  self.left_button_down=False

  @静态方法

  定义_ _剪辑(值,低,高):

  在低和高之间限幅值

  因素

  -

  值:一个数字

  要剪裁的值

  :一个号码

  最小尺寸

  高:一个号

  上限

  返回

  -

  输出:一个数字

  削波值

  输出=最大值(值,低)

  输出=最小值(输出,高)

  返回输出

  def shrink_point(self,x,y):

  将点(x,y)收缩到图像内部

  因素

  -

  x,y: int,int

  点的坐标

  返回

  -

  x_shrink,y_shrink: int,int

  收缩坐标

  高度,宽度=自身。图片展示。形状[0:2]

  x_shrink=self ._ _剪辑(x,0,宽度)

  y_shrink=self ._ _剪辑(y,0,高度)

  return (x_shrink,y_shrink)

  定义附加(自身):

  向作为列表添加矩形

  自我。直肠。追加(复制。深度复制(自我。current _ rect))

  定义弹出(自身):

  从作为列表中弹出矩形

  rect=Rect()

  if self.rects:

  rect=self.rects.pop()

  退货记录

  t

   def 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 rect

   print(pt1: x = %d, y = %d % (x, y))

   draw_rects.left_button_down = True

   draw_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 rect

   draw_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 list

   draw_rects.left_button_down = False

   draw_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.current_rect.regularize()

   draw_rects.append()

   if (not draw_rects.left_button_down) and event == cv2.EVENT_RBUTTONDOWN:

   # pop the last rect in rects list

   draw_rects.pop()

   draw_rects.reset_image()

   draw_rects.draw()

  if __name__ == __main__:

   #image = np.zeros((256, 256, 3), np.uint8)

   image = cv2.imread("111.jpg")

   draw_rects = DrawRects(image, (0, 255, 0), 2)

   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: # ESC

   break

   cv2.destroyAllWindows()

  运行效果

  

 

  补充

  当然Python+OpenCV不仅能做到在图像上绘制任意大小矩形,还能实现鼠标点击图像时会显示其坐标值

  下面是实现代码

  

import cv2

 

  运行结果:

  

 

  以上就是Python+OpenCV实现在图像上绘制矩形的详细内容,更多关于Python OpenCV的资料请关注盛行IT软件开发工作室其它相关文章!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: