Opencv simulated button and opencvbutton

Source: Internet
Author: User

Opencv simulated button and opencvbutton
Introduction

There is no special button control in opencv. Generally, you can use trackbar and set the status to 0 and 1 to simulate Control triggering. However, I always think this method is a bit of a second, so I tried to simulate the key value in opencv. At the same time in the implementation process, the use of information: http://www.csdn123.com/html/mycsdn20140110/84/8403a669ceed8e896d5c3ebc45b483b4.html, thank you very much.

Implementation Process
Code
void ButtonShow(void){int i; for(i=0; i< buttonNum; i++){if(i != 0){L_width = buttonAddr[i-1][2] + 30;}b_seat1=drawButton(img, buttonName[i],Point(L_width, L_height),0);buttonAddr[i] = (int *)malloc(4); buttonAddr[i][0] = b_seat1.x;buttonAddr[i][1] = b_seat1.y;buttonAddr[i][2] = b_seat1.width + b_seat1.x;buttonAddr[i][3] = b_seat1.height+ b_seat1.y;}} void on_mouse( int event, int x, int y, int flags, void* ustc)  {int buttonNow = -3;if(event == CV_EVENT_LBUTTONDOWN){tmpAddr[0] = x;tmpAddr[1] = y;tmpAddr[2] = 0;tmpAddr[3] = 0;}else if(event == CV_EVENT_LBUTTONUP){tmpAddr[2] = x;tmpAddr[3] = y;buttonNow = choiceButton(tmpAddr, buttonAddr, buttonNum);on_button(buttonNow);}}   int main(int argc,char **argv){img=imread(back_name,1); ButtonShow();imshow(back_show, img);cvSetMouseCallback(back_show, on_mouse, NULL);waitKey();}



Subject framework
First, load an image as the background image, then use ButtonShow to draw the button on the background image, and save the coordinates of each button in the buttonAddr array, then, the background image after the button is displayed. Then, the background image is added with a mouse response and waiting for the user to operate.
        img=imread(back_name,1);        ButtonShow();imshow(back_show, img);cvSetMouseCallback(back_show, on_mouse, NULL);waitKey();

Mouse response
Save the coordinates left-click and raised by the user in tmpAddr, and then use the choiceButton and on_button functions to select and respond to the control.
void on_mouse( int event, int x, int y, int flags, void* ustc){int buttonNow = -3;if(event == CV_EVENT_LBUTTONDOWN){tmpAddr[0] = x;tmpAddr[1] = y;tmpAddr[2] = 0;tmpAddr[3] = 0;}else if(event == CV_EVENT_LBUTTONUP){tmpAddr[2] = x;tmpAddr[3] = y;buttonNow = choiceButton(tmpAddr, buttonAddr, buttonNum);on_button(buttonNow);}}

Control Selection
int choiceButton(int* tmpAddr, int** buttonAddr, int size){int i, tmp; tmp = abs(tmpAddr[0] - tmpAddr[2]) + abs(tmpAddr[1] - tmpAddr[3]);if(tmp > 20){return -2;}for(i=0; i< size; i++){if((buttonAddr[i][0] < tmpAddr[0]) && (buttonAddr[i][2] > tmpAddr[0])){if((buttonAddr[i][1] < tmpAddr[1]) && (buttonAddr[i][3] > tmpAddr[3])){return i;}}}return -1;}

Call the choiceButton function when the left mouse button is lifted. TmpAddr: coordinates when the left mouse button is pressed and when the left mouse button is lifted. ButtonAddr: the number of controls and their coordinate information. Size: number of controls. In choiceButton, 1. judge whether the coordinates of the mouse are in the same position. If not, return-2 directly. 2. Determine whether the coordinates pressed by the left mouse button are in the control range or not. If yes, the control number is directly returned. 3. If the left mouse button is not in the range of any control,-1 is returned.

Control response
void on_button(int buttonNow){char str[20];sprintf(str,"%d", buttonNow);Mat img = cv::Mat(300, 300, CV_8UC3, 1);imshow(str, img);}

Select a parameter based on the preceding control. The parameter is used as the name of the display window of the blank image and displayed.

Effect demonstration
The corresponding effect is shown as follows:

Press the control: cancel. The window 0 is displayed. Press Control: add. Window 1 is displayed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.