当前位置:网站首页>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 )
边栏推荐
- Flutter (III) - master the usage of dart language in an article
- Multisensor fusion sensing
- You can command Siri without making a sound! The Chinese team of Cornell University developed the silent language recognition necklace. Chinese and English are OK
- 数据中心灾难恢复的重要参考指标:RTO和RPO
- 蓝桥杯嵌入式学习总结(新版)
- What is the five levels of cultivation of MES management system
- JS modularization
- [NLP] vector retrieval model landing: Bottleneck and solution!
- QPS
- QT basics tutorial: qstring
猜你喜欢

Calculate division in Oracle - solve the error report when the divisor is zero

Junit

Cache usage

Jmeter压力测试-Web代理本地接口测试【教学篇】

Request&Response

多传感器融合感知

Household enterprises use WMS warehouse management system. What are the changes

Machine learning - Iris Flower classification

arduino——ATtiny85 SSD1306 + DHT

Record the dependent installation problems encountered in building the web assets when developing pgadmin
随机推荐
Jemter 壓力測試 -基礎請求-【教學篇】
[UVM basics] TLM common data receiving and sending and data receiving examples
Quickly find five channels for high-quality objects, quickly collect and avoid detours
What is the five levels of cultivation of MES management system
2021 project improvement
Household enterprises use WMS warehouse management system. What are the changes
PyTorch-12 GAN、WGAN
What are the key points of turnover box management in warehouse management
How can I find the completely deleted photos in Apple mobile phone?
B站增量数据湖探索与实践
Teach you how to use the harmonyos local simulator
Webrtc has become the official standard of W3C and IETF, and mobile terminal development
Which securities company has the lowest Commission for opening a mobile account? Is it safe to open an account online?
Yyds dry inventory executor package (executor function)
解决 psycopg2.NotSupportedError: PQconninfo not available in libpq < 9.3
Exit of shell internal value command
Yyds dry inventory kubernetes easy service discovery and load balancing (11)
Web technology sharing | webrtc recording video stream
[UVM basics] connect of UVM_ Phase execution sequence
JS modularization