2017-01-04 21 views
0

私はOpenCVの助けを借りてUIImageに漫画のフィルターを適用しようとしています。私のコードは、私が知っている上記のコードは私に次のエラーを与える次cv :: Matでbitwise_andを適用するには?

+ (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 

ですそのth問題は入力配列のサイズが間違っていることです。どのように私はそれらを修正し、最終結果に影響を与えずに同じサイズのそれらを作ることができますか?

答えて

0

明らかに、OpenCVエラー:「入力引数のサイズが一致しません」と記載されています。すなわち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); 
関連する問題