Ask Your Question
0

Regarding rectangle button in opencv c++

asked 2017-11-29 07:45:21 -0600

vps gravatar image

updated 2017-12-06 07:22:38 -0600

I am making label tool in opencv with c++. I have made two buttons. If I click on button, the button is selected. The red colored rectangle is displayed around the button. when I click the another button, the previous red colored rectangle should be disappeared. I am posting useful code snipet only. Let me know in case of any information will be reuired.

image description

void callBackFunc1(int event, int x, int y, int flags, void* userdata)
{
Buttons* button = (Buttons*)userdata;
Vec3b selectButton;

if (event == EVENT_LBUTTONDOWN)
{
    if (button-> leftHand.contains(Point(x, y)))
    {
        selectButton = Vec3b(0, 0, 250);
        cout << "Select Left Hand ROI\n" << endl;
        //drawRect(button->leftHand) = Vec3b(255, 255, 255);
        rectangle(drawRect, button->leftHand, selectButton, 5, 8, 0);
    }
    else if (button->leftElbow.contains(Point(x, y)))
    {
        selectButton = Vec3b(0, 0, 250);
        cout << "Select Left Elbow ROI\n" << endl;
        rectangle(drawRect, button->leftElbow, selectButton, 5, 8, 0);
    }
imshow("Label Tool", drawRect);
  }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-11-29 08:37:00 -0600

updated 2017-11-29 08:43:47 -0600

With your current code snippet, you are simply drawing a rectangle over a selected button. To achieve what you want, two things are needed:

  1. Have a global button object/variable which keeps track of the last selected button. For this example, let's call it lastSelectedBtn

  2. Your callback manipulation will look like this

    void callBackFunc1(int event, int x, int y, int flags, void* userdata)
    {
        Buttons* button = (Buttons*)userdata;
        Vec3b selectButton;
    
        if (event == EVENT_LBUTTONDOWN)
        {
            if (button-> leftHand.contains(Point(x, y)))
            {
                lastSelectedBtn->hidden = true; // hide the last selected button
                lastSelectedBtn = button; // update it to the recently selected button
                selectButton = Vec3b(0, 0, 250);
                cout << "Select Left Hand ROI\n" << endl;
                rectangle(drawRect, button->leftHand, selectButton, 5, 8, 0);
            }
            else if (button->leftElbow.contains(Point(x, y)))
            {
                lastSelectedBtn->hidden = true; // hide the last selected button
                lastSelectedBtn = button; // update it to the recently selected button
                selectButton = Vec3b(0, 0, 250);
                cout << "Select Left Elbow ROI\n" << endl;
                rectangle(drawRect, button->leftElbow, selectButton, 5, 8, 0);
            }
        imshow("Label Tool", drawRect);
      }
    }
    

DISCLAIMER

Correct me if I am wrong but a glimpse into OpenCV High GUI shows that they do not provide a way of hiding created GUI elements. This means that the lastSelectedBtn->hidden = true logic part will have to be done at the OS level.

edit flag offensive delete link more

Comments

Hi, Sorry I am replying little bit late. Actually, I am using rectangle as button. I am not using opencv high GUI. below is the main function.

int main() { Buttons button; Colours colour; Mat3b image(600, 600, Vec3b(0, 0, 0));

// Define buttons and colours
button.leftHand = Rect(300, 0, image.cols - 300, 30);
colour.LeftHand = Rect(0, 0, image.cols - 400, 30);

    drawRect(button.leftHand) = Vec3b(200, 200, 200);
drawRect(colour.LeftHand) = Vec3b(255, 0, 0); //blue
putText(drawRect(button.leftHand), "Left hand ROI", Point(button.leftHand.width*0.35, button.leftHand.height*0.70), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 0), 1.5);

}

vps gravatar imagevps ( 2017-12-06 07:21:43 -0600 )edit

You had two questions. 1: How do I remove the red line when I press another rectangle? 2: How do I make the previous rectangle disappear? My solution completely answers the first question and even provides you with where you need to perform the second part. The logic of how to make the rectangle elements disappear has to be written by you. Even if you use OpenCV GUI, it does not offer this. One very stupid way of doing this is storing your rectangles in a vector then removing the selected rectangle from the vector then refreshing your current view. If you are going to want GUI features, I'd recommend using QT. The entire library was built for handling GUI related stuff

eshirima gravatar imageeshirima ( 2017-12-06 08:39:09 -0600 )edit

If you choose not to use it, then my previous comment still stands: You need to figure out how to remove the rectangle from the OS level. I do not know how to do that that's why I simply stated it in my answer without providing any guidelines.

eshirima gravatar imageeshirima ( 2017-12-06 08:41:17 -0600 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-11-29 07:45:21 -0600

Seen: 51 times

Last updated: Dec 06