目錄
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領域值得信任、有價值的長期合作伙伴,公司提供的服務項目有:申請域名、虛擬空間、營銷軟件、網站建設、內江網站維護、網站推廣。Python OpenCV實現(xiàn)鼠標繪制矩形框和多邊形
1. OpenCV鼠標事件操作說明
(1)setMouseCallback函數(shù)說明
(2)回調函數(shù)onMouse說明
(3)event 具體說明:
(4)flags 具體說明
2. OpenCV實現(xiàn)鼠標繪制矩形框和多邊形框
(1)繪制矩形框
(2)繪制多邊形
(3)鍵盤控制
3. 完整的代碼
本篇將使用OpenCV開發(fā)一個簡易的繪圖工具,可以實現(xiàn)鼠標繪制矩形框和多邊形,先看一下Demo效果
源碼已經開源在GitHub, 開源不易,麻煩給個【Star】:
GitHub - PanJinquan/base-utils: 集成C/C++ OpenCV 常用的算法和工具
使用PIP安裝:
pip install pybaseutils
【尊重原則,轉載請注明出處】https://blog.csdn.net/guyuealian/article/details/128019461
繪制矩形框 | 繪制多邊形 |
OpenCV支持鼠標事件操作,通過setMouseCallback函數(shù)來設置鼠標事件的回調函數(shù),使用方法可見官方文檔說明
(1)setMouseCallback函數(shù)說明void setMousecallback(const string& winname, MouseCallback onMouse, void* userdata=0);
//winname:窗口的名字
//onMouse:鼠標響應函數(shù),回調函數(shù)。指定窗口里每次鼠標時間發(fā)生的時候,被調用的函數(shù)指針。
//這個函數(shù)的原型應該為void on_Mouse(int event, int x, int y, int flags, void* param);
//userdate:傳給回調函數(shù)的參數(shù)
(2)回調函數(shù)onMouse說明void onMouse(int event, int x, int y, int flags, void* param);
//event是 CV_EVENT_*變量之一
//x和y是鼠標指針在圖像坐標系的坐標(不是窗口坐標系)
//flags是CV_EVENT_FLAG的組合, param是用戶定義的傳遞到setMouseCallback函數(shù)調用的參數(shù)。
(3)event 具體說明:(4)flags 具體說明EVENT_MOUSEMOVE 0? ? ? ? ?//滑動
EVENT_LBUTTONDOWN 1? //左鍵點擊
EVENT_RBUTTONDOWN 2? //右鍵點擊
EVENT_MBUTTONDOWN 3? //中鍵點擊
EVENT_LBUTTONUP 4? ? ? ? ? //左鍵放開
EVENT_RBUTTONUP 5? ? ? ? ?//右鍵放開
EVENT_MBUTTONUP 6? ? ? ? ? //中鍵放開
EVENT_LBUTTONDBLCLK 7 //左鍵雙擊
EVENT_RBUTTONDBLCLK 8 //右鍵雙擊
EVENT_MBUTTONDBLCLK 9 //中鍵雙擊
EVENT_FLAG_LBUTTON 1? ?//左鍵拖曳
EVENT_FLAG_RBUTTON 2? ?//右鍵拖曳
EVENT_FLAG_MBUTTON 4? ?//中鍵拖曳
EVENT_FLAG_CTRLKEY 8? ? ?//(8~15)按 Ctrl 不放
EVENT_FLAG_SHIFTKEY 16 //(16~31)按 Shift 不放
EVENT_FLAG_ALTKEY 32? ? ? //(32~39)按 Alt 不放
這是實現(xiàn)繪制矩形框的關鍵代碼
def event_draw_rectangle(self, event, x, y, flags, param):
"""繪制矩形框"""
if len(self.polygons) == 0: self.polygons = np.zeros(shape=(2, 2), dtype=np.int32) # 多邊形輪廓
point = (x, y)
if event == cv2.EVENT_LBUTTONDOWN: # 左鍵點擊,則在原圖打點
print("1-EVENT_LBUTTONDOWN")
self.next = self.last.copy()
self.polygons[0, :] = point
cv2.circle(self.next, point, radius=5, color=self.focus_color, thickness=self.thickness)
elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): # 按住左鍵拖曳,畫框
print("2-EVENT_FLAG_LBUTTON")
self.next = self.last.copy()
cv2.circle(self.next, self.polygons[0, :], radius=4, color=self.focus_color, thickness=self.thickness)
cv2.circle(self.next, point, radius=4, color=self.focus_color, thickness=self.thickness)
cv2.rectangle(self.next, self.polygons[0, :], point, color=self.line_color, thickness=self.thickness)
elif event == cv2.EVENT_LBUTTONUP: # 左鍵釋放,顯示
print("3-EVENT_LBUTTONUP")
self.next = self.last.copy()
self.polygons[1, :] = point
cv2.rectangle(self.next, self.polygons[0, :], point, color=self.line_color, thickness=self.thickness)
print("location:{},have:{}".format(point, len(self.polygons)))
(2)繪制多邊形這是實現(xiàn)繪制多邊形的關鍵代碼
def event_draw_polygon(self, event, x, y, flags, param):
"""繪制多邊形"""
exceed = self.max_point >0 and len(self.polygons) >= self.max_point
self.next = self.last.copy()
point = (x, y)
text = str(len(self.polygons))
if event == cv2.EVENT_LBUTTONDOWN: # 左鍵點擊,則在原圖打點
print("1-EVENT_LBUTTONDOWN")
cv2.circle(self.next, point, radius=5, color=self.focus_color, thickness=self.thickness)
cv2.putText(self.next, text, point, cv2.FONT_HERSHEY_SIMPLEX, 0.5, self.text_color, 2)
if len(self.polygons) >0:
cv2.line(self.next, self.polygons[-1, :], point, color=self.line_color, thickness=self.thickness)
if not exceed:
self.last = self.next
self.polygons = np.concatenate([self.polygons, np.array(point).reshape(1, 2)])
else:
cv2.circle(self.next, point, radius=5, color=self.focus_color, thickness=self.thickness)
if len(self.polygons) >0:
cv2.line(self.next, self.polygons[-1, :], point, color=self.line_color, thickness=self.thickness)
print("location:{},have:{}".format(point, len(self.polygons)))
(3)鍵盤控制為了方便用戶操作,這里定義幾個常用的按鍵:
- 按空格和回車鍵表示完成繪制
- 按ESC退出程序
- 按鍵盤c重新繪制
def task(self, image, callback: Callable, winname="winname"):
"""
鼠標監(jiān)聽任務
:param image: 圖像
:param callback: 鼠標回調函數(shù)
:param winname: 窗口名稱
:return:
"""
self.orig = image.copy()
self.last = image.copy()
self.next = image.copy()
cv2.namedWindow(winname, flags=cv2.WINDOW_NORMAL)
cv2.setMouseCallback(winname, callback, param={"winname": winname})
while True:
self.key = self.show_image(winname, self.next, delay=25)
print("key={}".format(self.key))
if (self.key == 13 or self.key == 32) and len(self.polygons) >0: # 按空格32和回車鍵13表示完成繪制
break
elif self.key == 27: # ESC退出程序
exit(0)
elif self.key == 99: # 按鍵盤c重新繪制
self.clear()
# cv2.destroyAllWindows()
源碼已經開源在GitHub, 開源不易,麻煩給個【Star】:
GitHub - PanJinquan/base-utils: 集成C/C++ OpenCV 常用的算法和工具
使用PIP安裝:
pip install pybaseutils
demo測試:base-utils/mouse_utils.py at master · PanJinquan/base-utils · GitHub
# -*-coding: utf-8 -*-
"""
@Author : panjq
@E-mail : pan_jinquan@163.com
@Date : 2022-07-27 15:23:24
@Brief :
"""
import cv2
import numpy as np
from typing import Callable
from pybaseutils import image_utils
class DrawImageMouse(object):
"""使用鼠標繪圖"""
def __init__(self, max_point=-1, line_color=(0, 0, 255), text_color=(255, 0, 0), thickness=2):
"""
:param max_point: 最多繪圖的點數(shù),超過后將繪制無效;默認-1表示無限制
:param line_color: 線條的顏色
:param text_color: 文本的顏色
:param thickness: 線條粗細
"""
self.max_point = max_point
self.line_color = line_color
self.text_color = text_color
self.focus_color = (0, 255, 0) # 鼠標焦點的顏色
self.thickness = thickness
self.key = -1 # 鍵盤值
self.orig = None # 原始圖像
self.last = None # 上一幀
self.next = None # 下一幀或當前幀
self.polygons = np.zeros(shape=(0, 2), dtype=np.int32) # 鼠標繪制點集合
def clear(self):
self.key = -1
self.polygons = np.zeros(shape=(0, 2), dtype=np.int32)
if self.orig is not None: self.last = self.orig.copy()
if self.orig is not None: self.next = self.orig.copy()
def get_polygons(self):
"""獲得多邊形數(shù)據(jù)"""
return self.polygons
def task(self, image, callback: Callable, winname="winname"):
"""
鼠標監(jiān)聽任務
:param image: 圖像
:param callback: 鼠標回調函數(shù)
:param winname: 窗口名稱
:return:
"""
self.orig = image.copy()
self.last = image.copy()
self.next = image.copy()
cv2.namedWindow(winname, flags=cv2.WINDOW_NORMAL)
cv2.setMouseCallback(winname, callback, param={"winname": winname})
while True:
self.key = self.show_image(winname, self.next, delay=25)
print("key={}".format(self.key))
if (self.key == 13 or self.key == 32) and len(self.polygons) >0: # 按空格32和回車鍵13表示完成繪制
break
elif self.key == 27: # 按ESC退出程序
exit(0)
elif self.key == 99: # 按鍵盤c重新繪制
self.clear()
# cv2.destroyAllWindows()
cv2.setMouseCallback(winname, self.event_default)
def event_default(self, event, x, y, flags, param):
pass
def event_draw_rectangle(self, event, x, y, flags, param):
"""繪制矩形框"""
if len(self.polygons) == 0: self.polygons = np.zeros(shape=(2, 2), dtype=np.int32) # 多邊形輪廓
point = (x, y)
if event == cv2.EVENT_LBUTTONDOWN: # 左鍵點擊,則在原圖打點
print("1-EVENT_LBUTTONDOWN")
self.next = self.last.copy()
self.polygons[0, :] = point
cv2.circle(self.next, point, radius=5, color=self.focus_color, thickness=self.thickness)
elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): # 按住左鍵拖曳,畫框
print("2-EVENT_FLAG_LBUTTON")
self.next = self.last.copy()
cv2.circle(self.next, self.polygons[0, :], radius=4, color=self.focus_color, thickness=self.thickness)
cv2.circle(self.next, point, radius=4, color=self.focus_color, thickness=self.thickness)
cv2.rectangle(self.next, self.polygons[0, :], point, color=self.line_color, thickness=self.thickness)
elif event == cv2.EVENT_LBUTTONUP: # 左鍵釋放,顯示
print("3-EVENT_LBUTTONUP")
self.next = self.last.copy()
self.polygons[1, :] = point
cv2.rectangle(self.next, self.polygons[0, :], point, color=self.line_color, thickness=self.thickness)
print("location:{},have:{}".format(point, len(self.polygons)))
def event_draw_polygon(self, event, x, y, flags, param):
"""繪制多邊形"""
exceed = self.max_point >0 and len(self.polygons) >= self.max_point
self.next = self.last.copy()
point = (x, y)
text = str(len(self.polygons))
if event == cv2.EVENT_LBUTTONDOWN: # 左鍵點擊,則在原圖打點
print("1-EVENT_LBUTTONDOWN")
cv2.circle(self.next, point, radius=5, color=self.focus_color, thickness=self.thickness)
cv2.putText(self.next, text, point, cv2.FONT_HERSHEY_SIMPLEX, 0.5, self.text_color, 2)
if len(self.polygons) >0:
cv2.line(self.next, self.polygons[-1, :], point, color=self.line_color, thickness=self.thickness)
if not exceed:
self.last = self.next
self.polygons = np.concatenate([self.polygons, np.array(point).reshape(1, 2)])
else:
cv2.circle(self.next, point, radius=5, color=self.focus_color, thickness=self.thickness)
if len(self.polygons) >0:
cv2.line(self.next, self.polygons[-1, :], point, color=self.line_color, thickness=self.thickness)
print("location:{},have:{}".format(point, len(self.polygons)))
@staticmethod
def polygons2box(polygons):
"""將多邊形轉換為矩形框"""
xmin = min(polygons[:, 0])
ymin = min(polygons[:, 1])
xmax = max(polygons[:, 0])
ymax = max(polygons[:, 1])
return [xmin, ymin, xmax, ymax]
def show_image(self, title, image, delay=5):
"""顯示圖像"""
cv2.imshow(title, image)
key = cv2.waitKey(delay=delay) if delay >= 0 else -1
return key
def draw_image_rectangle_on_mouse(self, image, winname="draw_rectangle"):
"""
獲得鼠標繪制的矩形框box=[xmin,ymin,xmax,ymax]
:param image:
:param winname: 窗口名稱
:return: box is[xmin,ymin,xmax,ymax]
"""
self.task(image, callback=self.event_draw_rectangle, winname=winname)
polygons = self.get_polygons()
box = self.polygons2box(polygons)
return box
def draw_image_polygon_on_mouse(self, image, winname="draw_polygon"):
"""
獲得鼠標繪制的矩形框box=[xmin,ymin,xmax,ymax]
:param image:
:param winname: 窗口名稱
:return: polygons is (N,2)
"""
self.task(image, callback=self.event_draw_polygon, winname=winname)
polygons = self.get_polygons()
return polygons
def draw_image_rectangle_on_mouse_example(image_file, winname="draw_rectangle"):
"""
獲得鼠標繪制的矩形框
:param image_file:
:param winname: 窗口名稱
:return: box=[xmin,ymin,xmax,ymax]
"""
image = cv2.imread(image_file)
# 通過鼠標繪制矩形框rect
mouse = DrawImageMouse()
box = mouse.draw_image_rectangle_on_mouse(image, winname=winname)
# 裁剪矩形區(qū)域,并繪制最終的矩形框
roi: np.ndarray = image[box[1]:box[3], box[0]:box[2]]
if roi.size >0: mouse.show_image("Image ROI", roi)
image = image_utils.draw_image_boxes(image, [box], color=(0, 0, 255), thickness=2)
mouse.show_image(winname, image, delay=0)
return box
def draw_image_polygon_on_mouse_example(image_file, winname="draw_polygon"):
"""
獲得鼠標繪制的多邊形
:param image_file:
:param winname: 窗口名稱
:return: polygons is (N,2)
"""
image = cv2.imread(image_file)
# 通過鼠標繪制多邊形
mouse = DrawImageMouse(max_point=-1)
polygons = mouse.draw_image_polygon_on_mouse(image, winname=winname)
image = image_utils.draw_image_points_lines(image, polygons, thickness=2)
mouse.show_image(winname, image, delay=0)
return polygons
if __name__ == '__main__':
image_file = "../data/test.png"
# 繪制矩形框
out = draw_image_rectangle_on_mouse_example(image_file)
# 繪制多邊形
out = draw_image_polygon_on_mouse_example(image_file)
print(out)
繪制矩形框 | 繪制多邊形 |
你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
網站題目:PythonOpenCV實現(xiàn)鼠標繪制矩形框和多邊形-創(chuàng)新互聯(lián)
本文鏈接:http://muchs.cn/article26/csgecg.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供微信小程序、靜態(tài)網站、企業(yè)網站制作、全網營銷推廣、外貿建站、Google
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)