網(wǎng)站怎么做移動端免費(fèi)推廣神器
ubuntu22.04@laptop OpenCV Get Started: 012_mouse_and_trackbar
- 1. 源由
- 2. mouse/trackbar應(yīng)用Demo
- 2.1 C++應(yīng)用Demo
- 2.2 Python應(yīng)用Demo
- 3. 鼠標(biāo)位置跟蹤注釋
- 3.1 注冊回調(diào)函數(shù)
- 3.2 回調(diào)操作
- 3.3 效果
- 4. 使用軌跡欄調(diào)整圖像大小
- 4.1 初始化軌跡欄&注冊回調(diào)函數(shù)
- 4.2 回調(diào)操作
- 4.3 效果
- 4. 總結(jié)
- 5. 參考資料
- 6. 補(bǔ)充
1. 源由
鼠標(biāo)指針和軌跡條是圖形用戶界面(GUI)中的關(guān)鍵組件。
如果沒有這些關(guān)鍵交互組件,就無法真正考慮與GUI交互。
因此,結(jié)合演示代碼了解OpenCV中鼠標(biāo)和軌跡條的內(nèi)置功能,對于程序交互來說至關(guān)重要。
2. mouse/trackbar應(yīng)用Demo
012_mouse_and_trackbar是OpenCV通過鼠標(biāo)指針和軌跡條與用戶交互的示例。
2.1 C++應(yīng)用Demo
C++應(yīng)用Demo工程結(jié)構(gòu):
012_mouse_and_trackbar/CPP$ tree .
.
├── Mouse
│ ├── CMakeLists.txt
│ └── mouse.cpp
└── Trackbar├── CMakeLists.txt└── trackbar.cpp2 directories, 4 files
確認(rèn)OpenCV安裝路徑:
$ find /home/daniel/ -name "OpenCVConfig.cmake"
/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
/home/daniel/OpenCV/opencv/build/OpenCVConfig.cmake
/home/daniel/OpenCV/opencv/build/unix-install/OpenCVConfig.cmake$ export OpenCV_DIR=/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
C++應(yīng)用Demo工程編譯執(zhí)行:
$ cd Mouse
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/mouse
$ cd Trackbar
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/trackbar
2.2 Python應(yīng)用Demo
Python應(yīng)用Demo工程結(jié)構(gòu):
012_mouse_and_trackbar/Python$ tree .
.
├── requirements.txt
├── mouse.py
└── trackbar.py0 directories, 3 files
Python應(yīng)用Demo工程執(zhí)行:
$ workoncv-4.9.0
$ python mouse.py
$ python trackbar.py
3. 鼠標(biāo)位置跟蹤注釋
3.1 注冊回調(diào)函數(shù)
OpenCV提供了鼠標(biāo)事件檢測功能,用于檢測各種鼠標(biāo)操作。
代碼上采用注冊回調(diào)函數(shù)方式實(shí)現(xiàn):
C++:
// highgui function called when mouse events occur
setMouseCallback("Window", drawRectangle);
Python:
# highgui function called when mouse events occur
cv2.setMouseCallback("Window", drawRectangle)
3.2 回調(diào)操作
當(dāng)有鼠標(biāo)操作時:
- EVENT_LBUTTONDOWN:記錄左上角坐標(biāo)
- EVENT_LBUTTONUP:記錄右下角坐標(biāo),并更新圖像
實(shí)現(xiàn)對左上角和右下角的框選,矩形框標(biāo)注選擇范圍。
C++:
// Points to store the center of the circle and a point on the circumference
Point top_left_corner, bottom_right_corner;
// image image
Mat image;// function which will be called on mouse input
void drawRectangle(int action, int x, int y, int flags, void *userdata)
{// Mark the center when left mouse button is pressedif( action == EVENT_LBUTTONDOWN ){top_left_corner = Point(x,y);}// When left mouse button is releasedelse if( action == EVENT_LBUTTONUP){bottom_right_corner = Point(x,y);// Draw rectanglerectangle(image, top_left_corner, bottom_right_corner, Scalar(0,255,0), 2, 8 );// Display imageimshow("Window", image);}}
Python:
# Lists to store the points
top_left_corner=[]
bottom_right_corner=[]# Define drawRectangle function
def drawRectangle(action, x, y, flags, *userdata):# Referencing global variables global top_left_corner, bottom_right_corner# Action to be taken when left mouse button is pressedif action == cv2.EVENT_LBUTTONDOWN:top_left_corner = [(x,y)]# Action to be taken when left mouse button is releasedelif action == cv2.EVENT_LBUTTONUP:bottom_right_corner = [(x,y)] # Draw the rectanglecv2.rectangle(image, top_left_corner[0], bottom_right_corner[0], (0,255,0),2, 8)cv2.imshow("Window",image)
3.3 效果
4. 使用軌跡欄調(diào)整圖像大小
4.1 初始化軌跡欄&注冊回調(diào)函數(shù)
創(chuàng)建軌跡欄對象時,代碼上采用注冊回調(diào)函數(shù)方式實(shí)現(xiàn):
C++:
int maxScaleUp = 100;
int scaleFactor = 1;string windowName = "Resize Image";
string trackbarValue = "Scale";// Create Trackbars and associate a callback function
createTrackbar(trackbarValue, windowName, &scaleFactor, maxScaleUp, scaleImage);
Python:
maxScaleUp = 100
scaleFactor = 1
windowName = "Resize Image"
trackbarValue = "Scale"# Create trackbar
cv2.createTrackbar(trackbarValue, windowName, scaleFactor, maxScaleUp, scaleImage)
4.2 回調(diào)操作
當(dāng)有拖動軌跡欄滑塊時,調(diào)用回調(diào)函數(shù)。根據(jù)滑塊位置,對圖像進(jìn)行比例縮放。
C++:
// Callback functions
void scaleImage(int, void*)
{// Read the imageMat image = imread("../../Input/sample.jpg");// Get the Scale factor from the trackbardouble scaleFactorDouble = 1 + scaleFactor/100.0;// Set the factor to 1 if becomes 0if (scaleFactorDouble == 0){scaleFactorDouble = 1;}Mat scaledImage;// Resize the imageresize(image, scaledImage, Size(), scaleFactorDouble, scaleFactorDouble, INTER_LINEAR);// Display the imageimshow(windowName, scaledImage);
}
Python:
# Callback functions
def scaleImage(*args):# Get the scale factor from the trackbar scaleFactor = 1+ args[0]/100.0# Resize the imagescaledImage = cv2.resize(image, None, fx=scaleFactor, fy = scaleFactor, interpolation = cv2.INTER_LINEAR)cv2.imshow(windowName, scaledImage)
4.3 效果
通過軌跡欄的拖動,實(shí)現(xiàn)圖像的放大縮小。
4. 總結(jié)
本文通過設(shè)置setMouseCallback
和createTrackbar
注冊鼠標(biāo)操作回調(diào)函數(shù)和軌跡欄空間回調(diào)函數(shù),實(shí)現(xiàn)對應(yīng)的OpenCV圖像操作。
- setMouseCallback(winname, onMouse, userdata)
- winname Name of the window.
- onMouse Callback function for mouse events. See OpenCV samples on how to specify and use the callback.
- userdata The optional parameter passed to the callback.
- createTrackbar( trackbarName, windowName, value, count, onChange)
- trackbarname Name of the created trackbar.
- winname Name of the window that will be used as a parent of the created trackbar.
- value Optional pointer to an integer variable whose value reflects the position of the slider. Upon creation, the slider position is defined by this variable.
- count Maximal position of the slider. The minimal position is always 0.
- onChange Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void*); , where the first parameter is the trackbar position and the second parameter is the user data (see the next parameter). If the callback is the NULL pointer, no callbacks are called, but only value is updated.
- userdata User data that is passed as is to the callback. It can be used to handle trackbar events without using global variables.
5. 參考資料
【1】ubuntu22.04@laptop OpenCV Get Started
【2】ubuntu22.04@laptop OpenCV安裝
【3】ubuntu22.04@laptop OpenCV定制化安裝
6. 補(bǔ)充
學(xué)習(xí)是一種過程,對于前面章節(jié)學(xué)習(xí)討論過的,就不在文中重復(fù)了。
有興趣了解更多的朋友,請從《ubuntu22.04@laptop OpenCV Get Started》開始,一個章節(jié)一個章節(jié)的了解,循序漸進(jìn)。