gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:22:52 31 4
gpt4 key购买 nike

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

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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com