C++ 如何在cv::Mat上应用按位_和?

C++ 如何在cv::Mat上应用按位_和?,c++,ios,objective-c,opencv,C++,Ios,Objective C,Opencv,在OpenCV的帮助下,我正在尝试将卡通过滤器应用于UIImage。我的代码如下 + (UIImage *)createCartoonizedImageFromImage:(UIImage *)inputImage { int num_down = 2; //number of downsampling steps cv::Mat image_rgb = [self cvMatFromUIImage:inputImage]; cv::Mat image_color; cv::cvtColo

在OpenCV的帮助下,我正在尝试将卡通过滤器应用于UIImage。我的代码如下

+ (UIImage *)createCartoonizedImageFromImage:(UIImage *)inputImage {

int num_down = 2; //number of downsampling steps

cv::Mat image_rgb = [self cvMatFromUIImage:inputImage];

cv::Mat image_color;
cv::cvtColor(image_rgb, image_color, cv::COLOR_RGBA2RGB);

//downsample image using Gaussian pyramid
for(int i = 0; i < num_down; i++)
{
    cv::pyrDown(image_color, image_color);
}

// apply bilateral filter
cv::Mat image_bilateral = image_color.clone();
cv::bilateralFilter(image_color, image_bilateral, 9, 9, 7);

// upsample image to original size
for(int i = 0; i < num_down; i++)
{
    cv::pyrUp(image_color, image_color);
}

// convert to grayscale
cv::Mat image_gray;
cv::cvtColor(image_rgb, image_gray, cv::COLOR_RGB2GRAY);

// apply median blur
cv::Mat image_blur;
cv::medianBlur(image_gray, image_blur, 7);

// detect and enhance edges
cv::Mat image_edge;
cv::adaptiveThreshold(image_blur, image_edge, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 9, 2);

// convert back to color, bit-AND with color image
cv::cvtColor(image_edge, image_edge, cv::COLOR_GRAY2RGB);
cv::Mat image_cartoon;
cv::bitwise_and(image_bilateral, image_edge, image_cartoon);

UIImage *cartoonImage = [self UIImageFromCVMat:image_cartoon];
return cartoonImage;

}
上面的代码给出了以下错误

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array') in binary_op, file /Users/kyle/code/opensource/opencv/modules/core/src/arithm.cpp, line 225
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/kyle/code/opensource/opencv/modules/core/src/arithm.cpp:225: error: (-209) The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array' in function binary_op
我的问题


我知道问题在于输入数组的大小不正确。但是我如何才能在不影响最终结果的情况下纠正它们并使它们具有相同的大小呢?

正如OpenCV错误中明确指出的:“输入参数的大小不匹配”。i、 e图像_.size!=图像_edge.size()。一个简单的调试打印就可以了!所以下次尝试使用你的调试器!这是你修改过的代码

    int num_down = 2; //number of downsampling steps

    cv::Mat image_rgb = imread(FileName1,1);

    cv::Mat image_color;
    cv::cvtColor(image_rgb, image_color, cv::COLOR_RGBA2RGB);

    //downsample image using Gaussian pyramid
    for(int i = 0; i < num_down; i++)
    {
        cv::pyrDown(image_color, image_color);
    }

    // apply bilateral filter
    cv::Mat image_bilateral = image_color.clone();
    cv::bilateralFilter(image_color, image_bilateral, 9, 9, 7);

    // upsample image to original size
    for(int i = 0; i < num_down; i++)
    {
        cv::pyrUp(image_color, image_color);
        cv::pyrUp(image_bilateral, image_bilateral);//Bug <-- Here Missing to Resize the bilateralFilter image?
    }

    // convert to grayscale
    cv::Mat image_gray;
    //cv::cvtColor(image_rgb, image_gray, cv::COLOR_RGB2GRAY);//Bug <-- Using RGBA instead of RGB
    cv::cvtColor(image_color, image_gray, cv::COLOR_RGB2GRAY);

    // apply median blur
    cv::Mat image_blur;
    cv::medianBlur(image_gray, image_blur, 7);

    // detect and enhance edges
    cv::Mat image_edge;
    cv::adaptiveThreshold(image_blur, image_edge, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 9, 2);

    // convert back to color, bit-AND with color image
    cv::cvtColor(image_edge, image_edge, cv::COLOR_GRAY2RGB);
    cv::Mat image_cartoon;

    //cv::bitwise_and(image_bilateral, image_edge, image_cartoon);//Bug <-- Here Size of image_bilateral is 1/4 of the image_edge
    cv::bitwise_and(image_bilateral, image_edge, image_cartoon);

    imshow("Cartoon ",image_cartoon);
int num\u down=2//下采样步数
cv::Mat image_rgb=imread(文件名1,1);
cv::Mat图像颜色;
cv::cvtColor(图像为rgb,图像为彩色,cv::彩色为RGBA2RGB);
//高斯金字塔下采样图像
for(int i=0;i    int num_down = 2; //number of downsampling steps

    cv::Mat image_rgb = imread(FileName1,1);

    cv::Mat image_color;
    cv::cvtColor(image_rgb, image_color, cv::COLOR_RGBA2RGB);

    //downsample image using Gaussian pyramid
    for(int i = 0; i < num_down; i++)
    {
        cv::pyrDown(image_color, image_color);
    }

    // apply bilateral filter
    cv::Mat image_bilateral = image_color.clone();
    cv::bilateralFilter(image_color, image_bilateral, 9, 9, 7);

    // upsample image to original size
    for(int i = 0; i < num_down; i++)
    {
        cv::pyrUp(image_color, image_color);
        cv::pyrUp(image_bilateral, image_bilateral);//Bug <-- Here Missing to Resize the bilateralFilter image?
    }

    // convert to grayscale
    cv::Mat image_gray;
    //cv::cvtColor(image_rgb, image_gray, cv::COLOR_RGB2GRAY);//Bug <-- Using RGBA instead of RGB
    cv::cvtColor(image_color, image_gray, cv::COLOR_RGB2GRAY);

    // apply median blur
    cv::Mat image_blur;
    cv::medianBlur(image_gray, image_blur, 7);

    // detect and enhance edges
    cv::Mat image_edge;
    cv::adaptiveThreshold(image_blur, image_edge, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 9, 2);

    // convert back to color, bit-AND with color image
    cv::cvtColor(image_edge, image_edge, cv::COLOR_GRAY2RGB);
    cv::Mat image_cartoon;

    //cv::bitwise_and(image_bilateral, image_edge, image_cartoon);//Bug <-- Here Size of image_bilateral is 1/4 of the image_edge
    cv::bitwise_and(image_bilateral, image_edge, image_cartoon);

    imshow("Cartoon ",image_cartoon);