Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Showing a rectangle on the courser and cutting out

Hi there,

I am pretty new to programming and especially to C++. I want to write a program that does the following:

  1. Scans a specified folder for images; the first one is opened and displayed.
  2. The user has a rectangle of specified size over the courser and can "place" this rectangle anywhere he wants on that image by clicking. He can place as many rectangles as he wishes.
  3. If all the rectangles are placed, pressing a button (e.g. space) cuts all the parts of the image that are within those rectangles and saves them seperately in a specified folder (so that we have x image files for the respective x rectangles). The next image is loaded and we go back to step 2.

As you can see in the code below, my program currently shows one image that is passed in the command line and logs the position of mouse clicks inside the image window (becuase I will need the mouse position).

I am currently trying to draw a rectangle around the courser that moves with it, however, all the documenatries I read use the rectangle()-function, which I cannot find. I only have the cvRectangle()-function, and this one does not accept my image of type Mat. I found in a post on Stack Overflow that said I need to convert it to IplImage, but it does not like that either. Here (http://answers.opencv.org/question/62050/why-im-getting-the-method-rectanglemat-point-point-scalar-is-undefined-for-the-type-core-in-opencv-3/) it says that the function has been removed and that i have to use Imgproc.rectangle(), but my program does not know Imgproc() (already tried to include the headerfile with that name and imgcodecs).

Another question would be how to make the rectangle move with the coursor and placing it not until the button is clicked. Since I am rather new to programming, I am not sure where the code for this would go. If I place it inside my CallBackFunc(), it does not know img. If I place it inside my main(), it cannot access x, y, ... I will probably have to pass img to the CallBackFunc, but everything in proper time. :)

I won't be able to check here every day, so I'll probably be unable to reply until monday. Thanks a lot for answers, though, it's much appreciated! Have a good weekend!

#include "stdafx.h"
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;


void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
    if (event == EVENT_LBUTTONDOWN)
    {
        cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
    }
    else if (event == EVENT_RBUTTONDOWN)
    {
        cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
    }
    else if (event == EVENT_MBUTTONDOWN)
    {
        cout << "Middle button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
    }
    //else if (event == EVENT_MOUSEMOVE)
    //{
    //  cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;

    //}


}

int main(int argc, char** argv)
{
    //cout << argc << "\n";
    //cout << argv[0];
    //cout << argv[1] << "\n";

    if (argc != 2)
    {
        cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
        return -1;
    }

    Mat image;
    image = imread(argv[1], IMREAD_COLOR); // Read the file

    if (!image.data) // Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl;
        return -1;
    }

    namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
    imshow("Display window", image); // Show our image inside it.

    setMouseCallback("Display window", CallBackFunc, NULL);

    waitKey(0); // Wait for a keystroke in the window
    return 0;
}