c++ - 使用 OpenCV 和 C++ 执行图像过滤,错误 : "Sizes of input arguments do not match"

标签 c++ opencv image-processing terminal

这是我如何调用我的图像并定义我的按钮:

img = imread("lena.jpg");

createButton("Show histogram", showHistCallback, NULL, QT_PUSH_BUTTON, 0);
createButton("Equalize histogram", equalizeCallback, NULL, QT_PUSH_BUTTON, 0);
createButton("Cartoonize", cartoonCallback, NULL, QT_PUSH_BUTTON, 0);

imshow("Input", img);
waitKey(0);
return 0;

我可以正确调用并显示我的图像。功能 显示直方图 均衡直方图 也可以正常工作。但是当我尝试调用 卡通化 ,我收到了这个错误:
[ WARN:0] global /home/hiro/Documents/OpenCV/opencv-4.3.0-source/modules/core/src/matrix_expressions.cpp (1334) 
assign OpenCV/MatExpr: processing of multi-channel arrays might be changed in the future: https://github.com/opencv/opencv/issues/16739
terminate called after throwing an instance of 'cv::Exception'
what():OpenCV(4.3.0) /home/hiro/Documents/OpenCV/opencv-4.3.0-source/modules/core/src/arithm.cpp:669: 
error: (-209: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 function 'arithm_op'

所以我猜我的错误来自 CartoonCallback功能, channel 错误。我已确保我的复制是在相同 channel 的图像之间,我将所有内容转换回 3 个 channel ,但我似乎无法弄清楚错误来自哪里。这是代码:
void cartoonCallback(int state, void* userdata){
    Mat imgMedian;
    medianBlur(img, imgMedian, 7);

    Mat imgCanny; 
    Canny(imgMedian, imgCanny, 50, 150); //Detect edges with canny
    Mat kernel = getStructuringElement (MORPH_RECT, Size(2,2));
    dilate(imgCanny, imgCanny, kernel); //Dilate image

    imgCanny = imgCanny/255;
    imgCanny = 1 - imgCanny;

    Mat imgCannyf; //use float values to allow multiply between 0 and 1
    imgCanny.convertTo(imgCannyf, CV_32FC3);
    blur(imgCannyf, imgCannyf, Size(5,5));

    Mat imgBF; 
    bilateralFilter(img, imgBF, 9, 150.0, 150.0); //apply bilateral filter
    Mat result = imgBF/25; //truncate color
    result = result*25;

    Mat imgCanny3c; //Create 3 channels for edges
    Mat cannyChannels[] = {imgCannyf, imgCannyf, imgCannyf};
    merge(cannyChannels, 3, imgCanny3c);

    Mat resultFloat; 
    result.convertTo(imgCanny3c, CV_32FC3); //convert result to float
    multiply(resultFloat, imgCanny3c, resultFloat);

    resultFloat.convertTo(result, CV_8UC3); //convert back to 8 bit
    imshow("Cartoonize", result);
}

有什么建议吗?

最佳答案

问题出在这个片段中:

cv::Mat resultFloat; // You prepare an output mat... with no dimensions nor type
result.convertTo(imgCanny3c, CV_32FC3); //convert result to float..ok
cv::multiply(resultFloat, imgCanny3c, resultFloat); //resultFloat is empty and has no dimensions!

如您所见,您通过 resultFloatcv::multiply(operand1, operand2, output) , 但是 resultFloat是空的,没有尺寸也没有类型,然后尝试将它与 imgCanny3c 相乘.这似乎是错误的原因。

关于c++ - 使用 OpenCV 和 C++ 执行图像过滤,错误 : "Sizes of input arguments do not match",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61312527/

相关文章:

c++ - boost spirit 语法错误 - "no type named ‘size’ 中的 ‘struct boost::spirit::unused_type’“

python - 在等高线内寻找等高线

c++ - QImage 转换返回 null

image-processing - 在numpy中使用as_strided函数滑动窗口?

c++ - 返回值的完美转发?

c++ - 斐波那契数列,二分查找

Python OpenCV将图像转换为字节字符串?

python - 检查数组行是否为None并为其赋值

c++ - 使用高度图扭曲图像?

c++ - QMap 的这种使用可能有害吗?