我正在尝试使用bitwise_and
以根据阈值排除图像的所有其余部分,但是当我尝试时它给出:
OpenCV 错误:输入参数的大小不匹配(操作既不是“array op array”(其中数组具有相同的大小和类型),也不是“array op scalar”,也不是“scalar op array”)在 binary_op,文件 / home/schirrel/Github/opencv/opencv-2.4.10/modules/core/src/arithm.cpp,第 1021 行在抛出 'cv::Exception' what() 实例后终止调用:/home/schirrel/Github/ opencv/opencv-2.4.10/modules/core/src/arithm.cpp:1021: error: (-209) 操作既不是“array op array”(其中数组具有相同的大小和类型),也不是“array op”函数 binary_op 中的标量”,也不是“标量运算数组”
我的代码是
Mat src, src_gray, dst;
int main()
{
src = imread("robosoccer.jpg", 1);
cvtColor(src, src_gray, CV_BGR2GRAY);
threshold(src_gray, dst, 150, 255, 1);
Mat res;
bitwise_and(src, dst, res);
imshow("AND", res);
("hold", res);
waitKey(0);
return (0);
}
Run Code Online (Sandbox Code Playgroud)
你能帮助我吗?
你应该使用:
bitwise_and(src_gray, dst, res);
Run Code Online (Sandbox Code Playgroud)
该错误意味着两个图像src
和dst
维度不相等,因为它们的通道数不同。
你也可以写:
Mat res = src_gray & dst;
Run Code Online (Sandbox Code Playgroud)
或者:
Mat res = src_gray.clone();
res.setTo(Scalar(0), ~dst);
Run Code Online (Sandbox Code Playgroud)
如果你需要彩色图像,你可以像@sturkmen建议的那样做
bitwise_and( src, src, res, dst );
Run Code Online (Sandbox Code Playgroud)
或者:
Mat res = src.clone();
res.setTo( Scalar( 0, 0, 0 ), ~dst);
Run Code Online (Sandbox Code Playgroud)