opencv安上后编译运行的奇怪问题

这是网上剽的一段基于opencv的人脸识别项目,原来跑在树梅派是OK的,服务器安完后直接跑居然出问题

#include 
#include 
#include 

#include 
#include 

using namespace std;
using namespace cv;

int main()
{

    // 【1】加载分类器
    CascadeClassifier cascade;
    cascade.load("/home/pi/opencv/data/haarcascades/haarcascade_frontalface_alt2.xml");

    Mat srcImage, grayImage, dstImage;
    // 【2】读取图片
    srcImage = imread("./woman.jpeg");
    dstImage = srcImage.clone();
    imshow("【原图】", srcImage);

    grayImage.create(srcImage.size(), srcImage.type());
    cvtColor(srcImage, grayImage, COLOR_BGR2GRAY); // 生成灰度图,提高检测效率

    // 定义7种颜色,用于标记人脸
    Scalar colors[] =
        {
            // 红橙黄绿青蓝紫
            CV_RGB(255, 0, 0),
            CV_RGB(255, 97, 0),
            CV_RGB(255, 255, 0),
            CV_RGB(0, 255, 0),
            CV_RGB(0, 255, 255),
            CV_RGB(0, 0, 255),
            CV_RGB(160, 32, 240)};

    // 【3】检测
    vector<Rect> rect;
    cascade.detectMultiScale(grayImage, rect, 1.1, 3, 0); // 分类器对象调用

    printf("检测到人脸个数:%d\n", rect.size());

    // 【4】标记--在脸部画圆
    for (int i = 0; i < rect.size(); i++)
    {
        Point center;
        int radius;
        center.x = cvRound((rect[i].x + rect[i].width * 0.5));
        center.y = cvRound((rect[i].y + rect[i].height * 0.5));

        radius = cvRound((rect[i].width + rect[i].height) * 0.25);
        circle(dstImage, center, radius, colors[i % 7], 2);
    }

    // 【5】显示
    imshow("【人脸识别detectMultiScale】", dstImage);
    imwrite("./res.jpg", dstImage);
    waitKey(0);
    return 0;
}

问题:
terminate called after throwing an instance of 'cv::Exception' what(): OpenCV(4.5.5-dev) /home/yaoxuetao/opencv-master/modules/highgui/src/window.cpp:1267: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
原以为是安包的问题,安了提示的libgtk2.0-dev后未果,偶然间尝试换掉
cascade.load(“/home/pi/opencv/data/haarcascades/haarcascade_frontalface_alt2.xml”);
中的训练数据,换到安装目录下的数据,对于我的:
cascade.load(“/usr/local/share/opencv4/haarcascades/haarcascade_frontalface_alt2.xml”);
这个make的时候能够看到安了哪些东西(这个设计模式注意学习,安的路径打出来)

这个其实就是训练好的数据,根据后缀名有人脸,微笑,全身等等,但是至于为什么版本不匹配居然会出现找不到回调函数确实想不明白,软件的这种bug还是应该注意的,看起来风马牛不相及的两件事!
改对路径后运行ok。

你可能感兴趣的:(linux,opencv)