当前位置:网站首页>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 )
边栏推荐
- Redis(5)----浅谈压缩列表
- Mxnet implementation of network in Nin network
- Detailed explanation of the generate go file command of import in golang (absolute detail)
- Exit of shell internal value command
- Informatics Olympiad 1355: string matching problem (STRs)
- 花指令wp
- Open a file at line with'filename:line'syntax - open a file at line with'filename:line' syntax
- buuresevewp
- QT之一个UI里边多界面切换
- JMeter stress test web agent local interface test [teaching]
猜你喜欢

Mxnet implementation of network in Nin network

Important reference indicators for data center disaster recovery: RTO and RPO

GMP model

执行npm install -g serve时报错权限权限问题解决方案

C#/. Net phase VI 01C Foundation_ 02:vs2019 basic operations, excluding code files, smart tips, data types, differences between float and double, and differences between string and string

A bold sounding and awesome operation - remake a Netflix

Installation homebrew error summary

Jemter 壓力測試 -基礎請求-【教學篇】

Take you three minutes to get started typescript

Jmeter压力测试-Web代理本地接口测试【教学篇】
随机推荐
QPS
CMDA 3634 image processing
Redis series - five common data types day1-3
Systemctl PHP configuration file
个人用指南针软件买股票安全吗?怎么炒股买股票呢
Attention mechanism yyds, AI editor finally bid farewell to P and destroyed the whole picture
I3wm get window class
Liangshui Xianmu shows his personal awareness as a unity3d worker
ES cluster_ block_ exception read_ only_ allow_ Delete question
Installation homebrew error summary
PyTorch-12 GAN、WGAN
Minor problems in importing D
Okhttp3 source code explanation (IV) cache strategy, disadvantages of Android mixed development
2021 project improvement
B站增量数据湖探索与实践
The first screen time, you said you optimized it, then you calculated it and showed it to me!
Esp32-c3 introductory tutorial WiFi part ⑥ - WIFI intelligent distribution network based on serial port
Important reference indicators for data center disaster recovery: RTO and RPO
Junit
In interface testing, several methods to verify the success of deleting interfaces