【问题标题】:Opencv error while building it with c++使用 C++ 构建时出现 Opencv 错误
【发布时间】:2018-01-22 05:15:21
【问题描述】:

我正在使用 OpenCV 在 C++ 中创建用于更改检测的代码,但是如果我更改图像,此代码会显示运行时错误

void MainWindow::on_pushButton_2_clicked()
{

    cv::Mat input1 = cv::imread("C:\\Users\\trainee2017233\\Desktop\\pre-post\\sulamani_ms1p1_pre_gref.tif");
    cv::Mat input2 = cv::imread("C:\\Users\\trainee2017233\\Desktop\\post-post\\sulamani_ms1p1_pre_gref.tif");

    cv::Mat diff;
    cv::absdiff(input1, input2, diff);

    cv::Mat diff1Channel;
    // WARNING: this will weight channels differently! - instead you might want some different metric here. e.g. (R+B+G)/3 or MAX(R,G,B)
    cv::cvtColor(diff, diff1Channel, CV_BGR2GRAY);

    float threshold = 30; // pixel may differ only up to "threshold" to count as being "similar"

    cv::Mat mask = diff1Channel < threshold;

    cv::imshow("similar in both images" , mask);

    // use similar regions in new image: Use black as background
    cv::Mat similarRegions(input1.size(), input1.type(), cv::Scalar::all(0));

    // copy masked area
    input1.copyTo(similarRegions, mask);


    cv::imshow("input1", input1);
    cv::imshow("input2", input2);
    cv::imshow("similar regions", similarRegions);
    cv::imwrite("../outputData/Similar_result.png", similarRegions);
    cv::waitKey(0);

}

当我将两个图像写为同一个图像时,没有错误,但是在将它们更改为不同的图像时会显示错误

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array') in arithm_op, file D:\opencv\sources\modules\core\src\arithm.cpp, line 659

【问题讨论】:

    标签: c++ qt opencv image-processing


    【解决方案1】:

    这里input1input2对于函数absdiff的大小应该相同

    ...
    cv::resize(input2, input2, input1.size());
    cv::Mat diff;
    ...
    

    【讨论】:

    • 在我的代码中添加此行后,代码会闪烁新错误** OpenCV 错误:断言失败 (dsize.area() > 0 || (inv_scale_x > 0 && inv_scale_y > 0)) 调整大小,文件D:\opencv\sources\modules\imgproc\src\imgwarp.cpp,第 3484 行**
    • 此错误是由于图像读取失败,请确保input1input2 不是空数组。
    • 不,它们不是空的。我想再问一件事,如果我想读取 .tif 文件,并且由于它们的大尺寸 imread 功能不支持这些类型的文件,所以我切换到 libtiff 库,我遇到了很多错误你能告诉我在 C++ 中打开 .tif 文件的过程吗
    猜你喜欢
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 2015-12-07
    • 2018-12-18
    • 2016-06-17
    • 2018-04-03
    相关资源
    最近更新 更多