I am fairly new to C/C++ and have the following problem. As part of an exectuable, I want to draw some rectangles on a picture using OpenCV. For this, I have defined a separate header file to keep the .cpp executable as short as possible. It looks like this:
typedef struct Rectangle {
cv::Point startPoint;
cv::Point endPoint;
};
class drawSpaces {
private:
Mat img;
int ix = 1;
int iy = 1;
std::list<Rectangle> rectList;
public:
//mouse callback function
void drawRect(int event, int x, int y, int, void *param) {
if (event == CV_EVENT_LBUTTONDOWN) {
//Save first point of rect
ix = x;
iy = y;
} else if (event == CV_EVENT_LBUTTONUP) {
//Save 2nd point of rect
cv::rectangle(img, Point(ix, iy), Point(x, y), cv::Scalar(0, 255, 0));
Rectangle rect;
rect.startPoint = Point(ix, iy);
rect.endPoint = Point(x, y);
rectList.push_back(rect);
}
}
}
int draw(Mat image) {
img = image;
if (img.empty()) {
cout << "\nerror reading image" << endl;
return -1;
}
namedWindow("Image", 1);
imshow("Image", img);
setMouseCallback("Image", drawRect);
while (waitKey(20) != 27) // wait until ESC is pressed
{
imshow("Image", img);
}
//save image with rectangles
imwrite( "../pics/new_Image.jpg", img );
return 0;
}
};
I now want to create an object of class drawSpaces and a Mat image in my main and run draw on it to get the new image. However, upon building, I get the Error Message
error: invalid use of non-static member function setMouseCallback("Image", drawRect);
with the compiler pointing at the drawRect funtion.
I have looked at other answers at this question and the majority seem to suggest to change drawRect to static. But I don't want my drawRect function to have the functionality of a static function, i.e. being able to be called without an actual drawSpaces object being present.
Any help, also on the style of coding, is appreciated!
EDIT: Using
setMouseCallback("Image", drawSpaces::drawRect);
does not help either.