pythonopencv鼠标画矩形框之angle()函数

更新时间:2023-07-08 09:31:11 阅读: 评论:0

pythonopencv⿏标画矩形框之angle()函数⽬录
参数说明
利⽤⿏标回调函数交互式画矩形框
总结
关于⿏标回调函数的说明可以参考:
参数说明
导⼊cv2后,通过angle)可以看到函数的帮助⽂档如下:
rectangle(...)
材质报告rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) -> img
.
  @brief Draws a simple, thick, or filled up-right rectangle.
.
.  The function cv::rectangle draws a rectangle outline or a filled rectangle who two opposite corners
.  are pt1 and pt2.
.
deallocate.  @param img Image.
.  @param pt1 Vertex of the rectangle.
.  @param pt2 Vertex of the rectangle opposite to pt1 .
.  @param color Rectangle color or brightness (grayscale image).
.  @param thickness Thickness of lines that make up the rectangle. Negative values, like #FILLED,
.  mean that the function has to draw a filled rectangle.
.  @param lineType Type of the line. See #LineTypes
.  @param shift Number of fractional bits in the point coordinates.
rectangle(img, rec, color[, thickness[, lineType[, shift]]]) -> img
.  @overload
.
.  u `rec` parameter as alternative specification of the drawn rectangle: `r.tl() and
.  r.br()-Point(1,1)` are opposite corners
其中四个参数必选:
img:底图,uint8类型的ndarray
pt1:矩形框的⼀个顶点坐标,是⼀个包含两个数字的tuple(必需是tuple),表⽰(x, y)
pt2:pt1的对⾓线顶点坐标,类型同pt1
independent是什么意思color:颜⾊,是⼀个包含三个数字的tuple或list,表⽰(b, g, r);如果图⽚是灰度图的话,color也可以是⼀个数字
其他参数说明如下:
来料加工账务处理
thickness:线宽,默认值是1,数值越⼤表⽰线宽越宽;如果取值为负数或者cv2.FILLED,那么将画⼀个填充了的矩形lineType:可以取的值有cv2.LINE_4,cv2.LINE_8,cv2.LINE_AA。其中cv2.LINE_AA的AA表⽰抗锯齿,线会更平滑。
注意:pt1和pt2表⽰任意⼀对对⾓线上的点,不⼀定要求pt1必需左上⾓,pt2必需右下⾓。另外pt1和pt2可以互换顺序⽽不影响结果。
下⾯是⼀个⾮交互式的程序⽰例
# -*- coding: utf-8 -*-
import cv2
import numpy as np
if __name__ == '__main__':
image = np.zeros((256, 256, 3), np.uint8)
color = (0, 255, 0)
cv2.namedWindow('rect', 1)
cv2.imshow('rect', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
第⼀个矩形框pt1 = 左上⾓,pt2 = 右下⾓
第⼆个矩形框pt1 = 右下⾓,pt2 = 左上⾓
第三个矩形框pt1 = 右上⾓,pt2 = 左下⾓
得到的结果如下:
利⽤⿏标回调函数交互式画矩形框
为了容易理解下⾯程序,建议先参考下⾯⽂章的例2
下⾯程序的操作⽅法是:
⿏标左键按下开始画当前矩形框
jita移动⿏标进⾏绘画
弹起左键当前矩形框绘画结束,并把当前矩形框加⼊列表
⿏标右键按下是删除矩形框列表中的最后⼀个对象
编程注意事项:
矩形框绘画过程中需要记录⼀个⿏标左键按下的状态标志。常规状态下该标志设为Fal,⿏标移动不
aboutme进⾏绘画;当⿏标左键按下后,标志设为True,此时移动⿏标将进⼊绘画状态;左键弹起后,标志恢复Fal。
为了在⿏标移动过程中实时显⽰绘画状态,需要不停地重置⽤来显⽰的图像,并不停地重画所有已保存的矩形框
矩形框的第⼆个点使⽤shrink_point获取,确保不超出图像边界
# -*- coding: utf-8 -*-
import copy
import cv2
import numpy as np
WIN_NAME = 'draw_rect'
class Rect(object):
def __init__(lf):
idiocracy
lf.tl = (0, 0)
lf.br = (0, 0)
def regularize(lf):
"""
make sure tl = TopLeft point, br = BottomRight point
"""
pt1 = (min(lf.tl[0], lf.br[0]), min(lf.tl[1], lf.br[1]))
pt2 = (max(lf.tl[0], lf.br[0]), max(lf.tl[1], lf.br[1]))
lf.tl = pt1
lf.br = pt2
class DrawRects(object):
def __init__(lf, image, color, thickness=1):
lf.image_for_show = py()
lf.thickness = thickness
cloud是什么意思lf.rects = []
lf.current_rect = Rect()
lf.left_button_down = Fal
@staticmethod
def __clip(value, low, high):
"""
clip value between low and high
Parameters
----------
value: a number
value to be clipped
low: a number
low limit
high: a number
high limit
Returns
-------
output: a number
clipped value
"""
output = max(value, low)
output = min(output, high)
return output
def shrink_point(lf, x, y):
"""
shrink point (x, y) to inside image_for_show
Parameters
----------
x, y: int, int
coordinate of a point
Returns
-------
x_shrink, y_shrink: int, int
shrinked coordinate
"""
height, width = lf.image_for_show.shape[0:2]
x_shrink = lf.__clip(x, 0, width)
y_shrink = lf.__clip(y, 0, height)
return (x_shrink, y_shrink)
def append(lf):
"""
经典英文电影add a rect to rects list
"""
def pop(lf):
"""
pop a rect from rects list
"""
rect = Rect()
s:
rect = lf.rects.pop()
return rect
def ret_image(lf):
"""
bernardret image_for_show using original image
"""
lf.image_for_show = lf.py()
def draw(lf):
"""
draw rects on image_for_show
"""
for rect s:
"""
draw current rect on image_for_show
"""
lf.current_rect.tl, lf.current_rect.br,
lor, thickness=lf.thickness) def onmou_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 cond point of rect and draw current rect
draw_rects.current_rect.br = draw_rects.shrink_point(x, y)
_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 = Fal
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.ularize()
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()
_image()
draw_rects.draw()
if __name__ == '__main__':
image = np.zeros((256, 256, 3), np.uint8)
draw_rects = DrawRects(image, (0, 255, 0), 2)
cv2.namedWindow(WIN_NAME, 0)
cv2.tMouCallback(WIN_NAME, onmou_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()
终端输出:
pt1: x = 55, y = 68
pt2: x = 0, y = 0
pt1: x = 195, y = 60
pt2: x = 256, y = 0
pt1: x = 59, y = 192
pt2: x = 0, y = 256
pt1: x = 194, y = 190
pt2: x = 256, y = 256
pt1: x = 94, y = 111
pt2: x = 170, y = 168
结果如下:
总结
到此这篇关于python opencv⿏标画矩形框之angle()函数的⽂章就介绍到这了,更多相关opencv⿏标画矩形框angle()内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

本文发布于:2023-07-08 09:31:11,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/1085224.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:矩形框   绘画   状态   函数
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图