C++ Opencv运行简单程序时抛出一个奇怪的错误

C++ Opencv运行简单程序时抛出一个奇怪的错误,c++,opencv,C++,Opencv,我编译了以下简单的opencv代码: #include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include <boost/random.hpp> #include <ctime> #include <time.h> #include <stdlib.h> #include <math.h> #include <iostr

我编译了以下简单的opencv代码:

#include "opencv2/core/core.hpp"        
#include "opencv2/highgui/highgui.hpp"  
#include <boost/random.hpp>
#include <ctime>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>

/*Compile using 

g++ -I /usr/local/boost CreateShares.cpp -o createShares `pkg-config --cflags --libs opencv` 

*/
using namespace cv;

class CreateShares
{
private:
    cv::Mat CoreImage;

    cv::Mat CoreImageR;
    cv::Mat CoreImageG;
    cv::Mat CoreImageB; 

    cv::Mat share0;
    cv::Mat share1;
    cv::Mat share2;
    cv::Mat share3;

public:
    CreateShares();
    CreateShares(std::string path);

    void showCoreImage();
    void showCoreImageR();
    void showCoreImageG();
    void showCoreImageB();

};

CreateShares::CreateShares()
{

}

CreateShares::CreateShares(std::string path)
{
    CoreImage = cv::imread(path, 1);

    if (CoreImage.empty())
    {
        std::cout<<"Could not read image !"<<std::endl;
        return;
    }
    std::vector<cv::Mat> temp_mat_vector; 
    cv::split(CoreImage, temp_mat_vector);

    //CoreImageB = (temp_mat_vector[0]).clone();
    temp_mat_vector[0].copyTo(CoreImageB);
    temp_mat_vector[1].copyTo(CoreImageB);
    temp_mat_vector[2].copyTo(CoreImageB);


}

void CreateShares::showCoreImage()
{
    cv::namedWindow("Core Image", WINDOW_AUTOSIZE);
    cv::imshow("Core Image", CoreImage);
}

void CreateShares::showCoreImageR()
{
    cv::namedWindow("Core Image R", WINDOW_AUTOSIZE);
    cv::imshow("Core Image R", CoreImageR);
}

void CreateShares::showCoreImageG()
{
    cv::namedWindow("Core Image G", WINDOW_AUTOSIZE);
    cv::imshow("Core Image G", CoreImageG);
}

void CreateShares::showCoreImageB()
{
    cv::namedWindow("Core Image B", WINDOW_AUTOSIZE);
    cv::imshow("Core Image B", CoreImageB);
}

int main()
{
    std::string path = "/home/r/l33t/Secret Sharing/nature.jpg";

    CreateShares* cs = new CreateShares(path);
    cs->showCoreImage();

    return 0;
}
我正在粘贴gdb输出,直到出现错误,以帮助您分析:

(gdb) next
[New Thread 0xb4af2b40 (LWP 17550)]
[New Thread 0xb40ffb40 (LWP 17551)]
65  s::showCoreImage()
(gdb) step
CreateShares::showCoreImageR (this=0x80516e8) at CreateShares.cpp:78
78  ", CoreImageR);
(gdb) step
79  G()
(gdb) step
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /build/buildd/opencv-2.3.1/modules/core/src/array.cpp, line 2482
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.3.1/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat


Program received signal SIGABRT, Aborted.
0xb7fdd424 in __kernel_vsyscall ()
(gdb) 

你知道这里有什么问题吗?我的代码是否有问题,或者opencv是否有问题,或者我是否没有在某个地方包含某些opencv路径,或者opencv是否没有正确安装?我没有主意了。请帮帮我

这是一个数据类型不匹配的问题

在我的系统上,当我运行程序时,只要我将路径更改为系统上的映像,它就可以正常工作。如果我没有提供实际存在的图像的路径,则会出现如您所示的错误。当您按通道分割图像时,您会将每个通道分配给
CoreImageB
而不是相应的通道。因此,您的
CoreImageR
CoreImageG
保持为空,这可能会导致
imshow
行为不当。谢谢。我把这条路径搞砸了。另外,你可以说
CoreImageB=temp\u mat\u vector[0],等等@SchighSchagh实际上是赋值操作符wwas给了我错误,所以我不得不切换到copyto和clone。但让我检查一下你指出的空洞问题。
(gdb) next
[New Thread 0xb4af2b40 (LWP 17550)]
[New Thread 0xb40ffb40 (LWP 17551)]
65  s::showCoreImage()
(gdb) step
CreateShares::showCoreImageR (this=0x80516e8) at CreateShares.cpp:78
78  ", CoreImageR);
(gdb) step
79  G()
(gdb) step
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /build/buildd/opencv-2.3.1/modules/core/src/array.cpp, line 2482
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.3.1/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat


Program received signal SIGABRT, Aborted.
0xb7fdd424 in __kernel_vsyscall ()
(gdb)