opencv c++初级滚动条问题


#include 
#include 
using namespace std;
using namespace cv;
int value=100;
static void callBack (int, void* )
{
    float a=value*1.0;
    Mat img1, img2;
    threshold(img1,img2,a,255,THRESH_BINARY_INV);
    imshow ( "ggb",img2);
}
int main(){
    Mat img1, img2;
    img1 = imread("1.png");
    namedWindow("ggb");
    imshow("ggb",img1);
    createTrackbar("ggb","ggb",nullptr,600,callBack,0);
    waitKey ();
}
出现报错:error: (-206:Bad flag (parameter or structure field)) Unrecognized or unsupported array type in function 'cvGetMat'
希望得到解答,谢谢

这个错误的原因是你在调用函数 threshold 时传入的第一个参数 img1 是一个未初始化的 Mat 对象。在 OpenCV 中,Mat 对象是用来存储图像的,但是你的代码中并没有给 img1 赋值。

在你的代码中,你需要给 img1 赋值,例如:

img1 = imread("1.png");

这样才能调用函数 threshold。

另外,建议你在调用函数 imread 时加入错误检测,因为如果图像文件不存在或者是一个无法解析的图像格式,那么 imread 函数就会返回一个空的 Mat 对象。例如:

Mat img1 = imread("1.png");
if (img1.empty()) {
    cerr << "Failed to read image file." << endl;
    return -1;
}

这样就可以在读取图像文件失败的情况下退出程序,避免出现运行时错误。