OpenCV拆分和合并程序显示错误

时间:2014-01-09 05:14:27

标签: opencv

我编写了以下程序,将image1分成三个通道,然后将image2添加到image1的蓝色通道。我正在使用代码块编译器并且它没有显示任何错误,但是当我执行它时,命令提示符显示运行时错误并强制关闭我的程序。有人能告诉我我的程序有什么问题吗? 这是我得到的错误的屏幕截图的链接 https://dl.dropboxusercontent.com/u/13916799/Capture4.JPG

using namespace std;
using namespace cv;

void addImages(Mat &image1,Mat &image2,Mat &result)
{
    result.create(image1.size(),image1.type());

    vector<Mat> planes;

    split(image1,planes);

    planes[0] += image2;

    merge(planes,result);
}


int main()
{
    Mat image1 = imread("C:\\castle.jpg",CV_LOAD_IMAGE_UNCHANGED);
    Mat image2 = imread("C:\\rain.jpg",CV_LOAD_IMAGE_UNCHANGED);
    Mat result;

    addImages(image1,image2,result);

    namedWindow("vOut",CV_WINDOW_AUTOSIZE);
    imshow("vOut",result);

    waitKey(0);
    destroyAllWindows();
}

2 个答案:

答案 0 :(得分:2)

您无法将单通道图像添加到三通道图像

planes[0] += image2;

你可以在arithm.cpp

中找到你的错误信息
 else if( !checkScalar(src2, src1.type(), kind2, kind1) )
 CV_Error( CV_StsUnmatchedSizes,
                 "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'" );

将image2添加到image1的蓝色通道?我不太明白。

拆分image2并使用相同的频道,类型,大小等添加它们......

答案 1 :(得分:1)

错误状态:输入参数的大小不匹配

要解决此错误,请检查以下内容:

  • image1和image2的大小(cols和行)必须相同
  • image2必须是NxMx1(1个频道)

我猜这个错误在于你对image2的imread调用。 您使用CV_LOAD_IMAGE_UNCHANGED读取图像,这可能会加载rgb,bgr,rgba [...]格式的图像,所有图像都包含多个频道。

要使用灰度读取图像(一个频道,请尝试:CV_LOAD_IMAGE_GRAYSCALE) 检查所有图像(和平面)是否具有正确的尺寸,并且您的代码应该有效。