简体   繁体   中英

Opencv error while building it with c++

I am creating a code for change detection in C++ using OpenCV but this code shows runtime error if I change the the image

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);

}

when I am writing both images as the same image then no error is there but while changing them to different images it shows the error

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

Here input1 and input2 should be of the same size for the function absdiff

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM