如何在cv :: Mat上应用bitwise_and?

时间:2017-01-04 03:10:55

标签: 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::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;

}

就行了

cv::bitwise_and(image_bilateral, image_edge, image_cartoon);

以上代码给出了以下错误

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

我的问题

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

1 个答案:

答案 0 :(得分:0)

正如在OpenCV错误中明确指出的那样:&#34;输入参数的大小与#34;不匹配。即image_bilateral.size!= image_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);