【发布时间】:2019-10-07 06:10:58
【问题描述】:
我正在做一个有趣的项目来了解有关 OpenCV 的更多信息。目前我想在给定图像的情况下绘制一个框。我有一个可以完成这项工作的工作函数,但是我在将 mouse_control 函数从 OpenCV 传递到 setMouseCallback 函数时遇到了一些问题。
我收到的错误是这样的:
error C3867: 'Box::mouse_control': non-standard syntax; use '&' to create a pointer to member
当我尝试放置建议的参考时,我收到此错误:
error C2276: '&': illegal operation on bound member function expression
我是 OpenCV 的新手,所以我不确定如何调试和修复我拥有的代码。
这是我正在处理的类的头文件:
#pragma once
#include "Image.h"
class Box : public Image
{
public:
Box() = default;
Mat draw_box(Mat& img, Rect box);
void mouse_control(int event, int x, int y, int flag, void* param);
private:
Rect g_rectangle;
bool g_drawbox;
};
这是与头文件关联的 .cpp 文件:
#include "Box.h"
#define WINDOW_NAME "Drawing Rectangle"
void Box::mouse_control(int event, int x, int y, int flag, void* param)
{
Mat& image = *(cv::Mat*) param;
switch (event)
{
case EVENT_MOUSEMOVE:
{ // When mouse moves, get the current rectangle's width and height
if (g_drawbox)
{
g_rectangle.width = x - g_rectangle.x;
g_rectangle.height = y - g_rectangle.y;
}
}
break;
case EVENT_LBUTTONDOWN:
{ // when the left mouse button is pressed down,
// get the starting corner's coordinates of the rectangle
g_drawbox = true;
g_rectangle = Rect(x, y, 0, 0);
}
break;
case EVENT_LBUTTONUP:
{ // when the left mouse button is released,
// draw the rectangle
g_drawbox = false;
if (g_rectangle.width < 0)
{
g_rectangle.x += g_rectangle.width;
g_rectangle.width *= -1;
}
if (g_rectangle.height < 0)
{
g_rectangle.y += g_rectangle.height;
g_rectangle.height *= -1;
}
draw_box(image, g_rectangle);
}
break;
}
}
Mat Box::draw_box(Mat& img, Rect box)
{
// Get input from user to draw box around object of interest
rectangle(img, box.tl(), box.br(), Scalar(0, 255, 255), FILLED, LINE_8);
Mat tempImage;
//Mat srcImage(600, 800, CV_8UC3);
//srcImage = Scalar::all(0);
namedWindow(WINDOW_NAME);
setMouseCallback(WINDOW_NAME, mouse_control, (void*)& img);
while (1) {
img.copyTo(tempImage);
if (g_drawbox)
draw_box(tempImage, g_rectangle);
imshow(WINDOW_NAME, tempImage);
if (waitKey(10) == 27) // stop drawing rectanglge if the key is 'ESC'
break;
}
return img;
}
错误发生在这里:
setMouseCallback(WINDOW_NAME, mouse_control, (void*)& img);
这是复制错误所需的主文件和其他头文件和cpp文件:
#include <iostream>
#include "Box.h"
#include "Image.h"
int main()
{
// Declare image object and the name of the image (store image in project file)
Image img_obj;
std::string image_name = "hawaii.png";
// Read and display image
auto img1 = img_obj.get_image(image_name);
img_obj.display(img1);
Rect g_rectangle;
Box b;
auto new_img = b.draw_box(img1, g_rectangle);
b.display(new_img);
std::cin.get();
}
#pragma once
#include <opencv2/opencv.hpp>
using namespace cv;
class Image
{
public:
Image() = default;
// Member functions
const Mat get_image(std::string& image_name);
void display(Mat& img);
};
#include "Image.h"
const Mat Image::get_image(std::string& image_name)
{
Mat img = imread(image_name);
return img;
}
void Image::display(Mat& img)
{
namedWindow("image", WINDOW_NORMAL);
imshow("image", img);
waitKey(0);
}
【问题讨论】: