Linux OpenCV 错误:输入参数的大小不匹配(操作既不是“数组操作数组”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17662518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array')
提问by Dead-coder
I am working on a project that uses opencv on raspberry pi. I have run into a hurdle which looks simple, but I am unable to solve the issue. First of all, here a part of my code:
我正在开发一个在树莓派上使用 opencv 的项目。我遇到了一个看起来很简单的障碍,但我无法解决这个问题。首先,这里是我的代码的一部分:
{
gray=cvarrToMat(py);
///cvShowImage("camcvWin", py); // display only gray channel
if(img_num%2 == 1)
{
cv::imwrite("/home/pi/test/Gray_2Image1.jpg",gray);
}
else if (img_num%2 == 0)
{
cv::imwrite( "/home/pi/test/Gray_2Image2.jpg", gray );
cv::Mat img2 = cv::imread("/home/pi/test/Gray_2Image2.jpg", 0);
cv::Mat img1 = cv::imread("/home/pi/test/Gray_2Image1.jpg", 0);
diffImage = abs(img1-img2);
imshow("diffImage", diffImage);
cv::imwrite( "/home/pi/test/Diffimage.jpg", diffImage );
}
img_num++;
This code has no problem. However, if I edit the code to make a slight modification as follows:
这段代码没有问题。但是,如果我编辑代码进行如下轻微修改:
{
gray=cvarrToMat(py);
///cvShowImage("camcvWin", py); // display only gray channel
if(img_num%2 == 1)
{
cv::imwrite("/home/pi/test/Gray_2Image1.jpg",gray);
cv::Mat img1 = cv::imread("/home/pi/test/Gray_2Image1.jpg", 0);
}
else if (img_num%2 == 0)
{
cv::imwrite( "/home/pi/test/Gray_2Image2.jpg", gray );
cv::Mat img2 = cv::imread("/home/pi/test/Gray_2Image2.jpg", 0);
diffImage = abs(img1-img2);
imshow("diffImage", diffImage);
cv::imwrite( "/home/pi/test/Diffimage.jpg", diffImage );
}
img_num++;
I get the following 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 /home/pi/OpenCV-2.3.1/modules/core/src/arithm.cpp, line 1253 terminate called after throwing an instance of 'cv::Exception' what(): /home/pi/OpenCV-2.3.1/modules/core/src/arithm.cpp:1253: error: (-209) 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 function arithm_op
OpenCV 错误:输入参数的大小不匹配(操作既不是“数组操作数组”(其中数组具有相同的大小和相同的通道数),也不是“数组操作标量”或“标量操作数组”) arithm_op,文件 /home/pi/OpenCV-2.3.1/modules/core/src/arithm.cpp,第 1253 行在抛出 'cv::Exception' what() 实例后终止调用:/home/pi/OpenCV- 2.3.1/modules/core/src/arithm.cpp:1253: error: (-209) 该操作既不是“array op array”(其中数组具有相同的大小和相同的通道数),也不是“array op”函数 arithm_op 中的标量”,也不是“标量运算数组”
I am not really able to understand what is going on. img1 and img2 are declared globally as Mat. This might be a simple issue, but I am still a novice. Please help me solve the issue. Thank you for your time.
我真的无法理解发生了什么。img1 和 img2 被全局声明为 Mat。这可能是一个简单的问题,但我仍然是新手。请帮我解决问题。感谢您的时间。
采纳答案by ChronoTrigger
In the first block of code, img1
and img2
are declared and contain two gray valid images (since you do imread
). Note that these img1
and img2
are not global variables, but local. If you have global variables with the same names, the local ones shadow them.
在第一个代码块中,img1
和img2
被声明并包含两个灰色有效图像(因为你这样做了imread
)。请注意,这些img1
和img2
不是全局变量,而是局部变量。如果您有同名的全局变量,则本地变量会隐藏它们。
In the second block, you define img2
in the else
and later do img1 - img2
, but you don't show to us the value of img1
(in this case, the global variable). The img1
that is in the if
is local to that if
and is not visible in the else
. Probably, you defined cv::Mat img1
in the global scope but you didn't give it any value. This would cause an error in img1-img2
because they are of different size (img1
would be empty).
在第二个块中,您img2
在else
do 中定义img1 - img2
,但没有向我们显示img1
(在本例中为全局变量)的值。该img1
是在if
是局部的if
,不可见else
。可能你cv::Mat img1
在全局范围内定义了但你没有给它任何价值。这会导致错误,img1-img2
因为它们的大小不同(img1
将为空)。
Update: Something like this should fix it.
更新:这样的事情应该解决它。
// global scope
cv::Mat img1, img2, diffImage;
void yourFunction()
{
...
img1 = cv::imread("/home/pi/test/Gray_2Image1.jpg", 0);
img2 = cv::imread("/home/pi/test/Gray_2Image2.jpg", 0);
diffImage = abs(img1-img2);
...
}
Update again: you can load the images in different if-else blocks as long as their declaration is visible.
再次更新:只要它们的声明可见,您就可以在不同的 if-else 块中加载图像。
This is ok:
这是确定:
// global scope
cv::Mat img1, img2, diffImage;
void yourFunction()
{
...
if(condition)
{
img1 = cv::imread("/home/pi/test/Gray_2Image1.jpg", 0);
}
else
{
img2 = cv::imread("/home/pi/test/Gray_2Image2.jpg", 0);
}
...
diffImage = abs(img1-img2); // make sure img1 and img2 are loaded first
...
}
And this is wrong:
这是错误的:
// global scope
cv::Mat img1, img2, diffImage;
void yourFunction()
{
...
if(condition)
{
// wrong: you are creating a local variable that shadows the global one
cv::Mat img1 = cv::imread("/home/pi/test/Gray_2Image1.jpg", 0);
}
...
diffImage = abs(img1-img2); // img1 is the global variable and not the local one in the previous if block!
...
}
回答by drahnr
In the else if
block - img1
is not defined or empty - to give a better diagnosis, more code is required.
在else if
块中 -img1
未定义或为空 - 为了提供更好的诊断,需要更多代码。
Also: why do you store gray
to disk just in order to read it again?
另外:为什么要存储gray
到磁盘只是为了再次读取它?