当前位置:网站首页>Opencv mouse event + interface interaction drawing rectangle polygon selection ROI
Opencv mouse event + interface interaction drawing rectangle polygon selection ROI
2022-06-26 07:44:00 【Bright moon drunk windowsill】
1.Opencv Mouse events
import cv2
d=[i for i in dir(cv2) if 'EVENT' in i]
print(d)
'EVENT_FLAG_ALTKEY': Represents drag and drop events . Hold down alt Key not put
'EVENT_FLAG_CTRLKEY' Hold down ctrl Key not put
'EVENT_FLAG_LBUTTON' Hold down the left button and drag
'EVENT_FLAG_MBUTTON' Middle drag
'EVENT_FLAG_RBUTTON' Right click and drag
'EVENT_FLAG_SHIFTKEY' Hold down shift Don't put
'EVENT_LBUTTONDBLCLK'event Mouse events . Next, double-left click on
'EVENT_LBUTTONDOWN' Press the left key
'EVENT_LBUTTONUP' Release left key
'EVENT_MBUTTONDBLCLK' Double-click the key
'EVENT_MBUTTONDOWN' Middle click
'EVENT_MBUTTONUP' Middle key release
'EVENT_MOUSEHWHEEL'
'EVENT_MOUSEMOVE' slide
'EVENT_MOUSEWHEEL',
'EVENT_RBUTTONDBLCLK', Right click double-click
'EVENT_RBUTTONDOWN', Right click on the
'EVENT_RBUTTONUP' Right click to release
The callback function uses :
cv2.setMousecallback(const string& winname, MouseCallback onMouse, void* userdata=0)
winname: The name of the window
onMouse: Mouse response function , Callback function . Specifies when each mouse time occurs in the window , Pointer to the called function . The prototype of this function should be void on_Mouse(int event, int x, int y, int flags, void* param);
userdate: Parameters passed to the callback function
Response function use :
void on_Mouse(int event, int x, int y, int flags, void* param);
event yes CV_EVENT_* One of the variables
x and y It's the coordinates of the mouse pointer in the image coordinate system ( It's not a window coordinate system )
flags yes CV_EVENT_FLAG The combination of , param It's user-defined and passed to setMouseCallback The parameters of the function call .
2. Draw a rectangle ROI
2.1 Method 1
import cv2
global img
global point1, point2
# Mouse response function
def Rectangular_box(event, x, y, flags, param):
global img, point1, point2
img2 = img.copy()
if event == cv2.EVENT_LBUTTONDOWN: # Left click
point1 = (x, y)
cv2.circle(img2, point1, 10, (0, 255, 0), 5)
cv2.imshow('img', img2)
elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): # Press and hold the left button to drag
cv2.rectangle(img2, point1, (x, y), (255, 0, 0), 5)
cv2.imshow('img', img2)
elif event == cv2.EVENT_LBUTTONUP: # Left release
point2 = (x, y)
cv2.rectangle(img2, point1, point2, (0, 255, 255), 4)
cv2.imshow('img', img2)
min_x = min(point1[0], point2[0])
min_y = min(point1[1], point2[1])
width = abs(point1[0] - point2[0])
height = abs(point1[1] - point2[1])
cut_img = img[min_y:min_y + height, min_x:min_x + width]
#cv2.imwrite('baocun.jpg', cut_img)
cv2.imshow('result',cut_img
def main():
global img
img = cv2.imread('yangmi.jpg')
img=cv2.resize(img,None,fx=0.4,fy=0.4)
cv2.namedWindow('image')
cv2.setMouseCallback('image', Rectangular_box)
cv2.imshow('image', img)
cv2.waitKey(0)
if __name__ == '__main__':
main()

2.2 Method 2
import cv2
import imutils
img = cv2.imread("./test_image.jpg")
img = imutils.resize(img, width=500)
roi = cv2.selectROI(windowName="roi", img=img, showCrosshair=True, fromCenter=False)
x, y, w, h = roi
cv2.rectangle(img=img, pt1=(x, y), pt2=(x + w, y + h), color=(0, 0, 255), thickness=2)
cv2.imshow("roi", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

3. Draw polygon ROI
polygon ROI, It mainly uses mouse interaction to draw :
- Left click , Select the points of the polygon ;
- Right click , Delete the most recent selection ;
- Middle click , determine ROI Area and visualize .
- Press ”S“ key , Put the polygon ROI Save the points of the area to the local ”config.pkl" In file .
import cv2
import imutils
import numpy as np
import joblib
pts = [] # For storage point
# A unified :mouse callback function
def draw_roi(event, x, y, flags, param):
img2 = img.copy()
if event == cv2.EVENT_LBUTTONDOWN: # Left click , Select point
pts.append((x, y))
if event == cv2.EVENT_RBUTTONDOWN: # Right click on the , Cancel the last selected point
pts.pop()
if event == cv2.EVENT_MBUTTONDOWN: # Middle click to draw the outline
mask = np.zeros(img.shape, np.uint8)
points = np.array(pts, np.int32)
points = points.reshape((-1, 1, 2))
# Draw a polygon
mask = cv2.polylines(mask, [points], True, (255, 255, 255), 2)
mask2 = cv2.fillPoly(mask.copy(), [points], (255, 255, 255)) # Used to find ROI
mask3 = cv2.fillPoly(mask.copy(), [points], (0, 255, 0)) # be used for The image displayed on the desktop
show_image = cv2.addWeighted(src1=img, alpha=0.8, src2=mask3, beta=0.2, gamma=0)
cv2.imshow("mask", mask2)
cv2.imshow("show_img", show_image)
ROI = cv2.bitwise_and(mask2, img)
cv2.imshow("ROI", ROI)
cv2.waitKey(0)
if len(pts) > 0:
# take pts Draw the last point in
cv2.circle(img2, pts[-1], 3, (0, 0, 255), -1)
if len(pts) > 1:
# Draw line
for i in range(len(pts) - 1):
cv2.circle(img2, pts[i], 5, (0, 0, 255), -1) # x ,y For the coordinates of the place where the mouse clicks
cv2.line(img=img2, pt1=pts[i], pt2=pts[i + 1], color=(255, 0, 0), thickness=2)
cv2.imshow('image', img2)
# Create images and windows and bind them to callback functions
img = cv2.imread("./test_image.jpg")
img = imutils.resize(img, width=500)
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_roi)
print("[INFO] Left click : Select point , Right click : Delete the last selected point , Middle click : determine ROI Area ")
print("[INFO] Press ‘S’ Confirm the selection area and save ")
print("[INFO] Press ESC sign out ")
while True:
key = cv2.waitKey(1) & 0xFF
if key == 27:
break
if key == ord("s"):
saved_data = {
"ROI": pts
}
joblib.dump(value=saved_data, filename="config.pkl")
print("[INFO] ROI Coordinates have been saved to local .")
break
cv2.destroyAllWindows()

Reference resources :
1. Use the mouse to draw a rectangular or polygonal box on the image
2.OpenCV-Python choice ROI( Rectangles and polygons )
边栏推荐
- 卡尔曼滤波器_Recursive Processing
- Solution to the permission problem when NPM install -g serve reports an error
- Machine learning - Iris Flower classification
- Opencv鼠标事件+界面交互之绘制矩形多边形选取感兴趣区域ROI
- ECE 9203/9023 analysis
- Pycharm settings
- QT之一个UI里边多界面切换
- Scratch program learning
- 记一次开发 pgadmin 时执行 Building the Web Assets 遇到的依赖安装问题
- Can the warehouse management system help enterprises reduce storage costs
猜你喜欢

What is the five levels of cultivation of MES management system

Deeply analyze storage costs and find cost reduction solutions

Basic use of swiperefreshlayout, local refresh of flutterprovider

Attention mechanism yyds, AI editor finally bid farewell to P and destroyed the whole picture

B站增量数据湖探索与实践

Google Earth Engine(GEE) 01-中输入提示快捷键Ctrl+space无法使用的问题

Record the dependent installation problems encountered in building the web assets when developing pgadmin

MXNet对NIN网络中的网络的实现

The "big grievances" in the workplace are not only physically tired, but also mentally emptied

A bold sounding and awesome operation - remake a Netflix
随机推荐
一文分析EventBus-事件总线的使用方法和实现原理
Tsinghua Yaoban chendanqi won Sloan award! He is a classmate with last year's winner Ma Tengyu. His doctoral thesis is one of the hottest in the past decade
手机开户哪个证券公司佣金最低?网上开户是否安全么?
JS event loop mechanism
Es string type (text vs keyword) selection
Junit
This paper analyzes the use method and implementation principle of eventbus event bus
If you don't understand, please hit me
Pycharm settings
I3wm get window class
[UVM foundation] UVM_ Driver member variable req definition
Detailed explanation of the generate go file command of import in golang (absolute detail)
Redis series - redis startup, client day1-2
Installation homebrew error summary
arduino——ATtiny85 SSD1306 + DHT
Jemter stress test - Basic request - [teaching]
Google Earth Engine(GEE) 01-中输入提示快捷键Ctrl+space无法使用的问题
OSPF design principles, commands take H3C as an example
[recommend an entity class conversion tool mapstruct, which is powerful and easy to use]
Nine hours, nine people and nine doors (01 backpack deformation) - Niuke