Using custom kernel in opencv 2DFilter - causing crash … convolution how?

asked 2015-03-27 06:43:11 -0500

Lamar Latrell gravatar image

updated 2015-09-26 07:11:15 -0500

Hello all,

Thought I'd try my hand at a little (auto)correlation/convolution today in openCV and make my own 2D filter kernel.

Following openCV's 2D Filter Tutorial I discovered that making your own kernels for openCV's Filter2D might not be that hard. However I'm getting unhandled exceptions when I try to use one.

Code with comments relating to the issue here:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {

    //Loading the source image
    Mat src;
    src = imread( "1.png" );

    //Output image of the same size and the same number of channels as src.
    Mat dst;
    //Mat dst = src.clone();   //didn't help...

    //desired depth of the destination image
    //negative so dst will be the same as src.depth()
    int ddepth = -1;        

    //the convolution kernel, a single-channel floating point matrix:
    Mat kernel = imread( "kernel.png" );
    kernel.convertTo(kernel, CV_32F);     //<<not working
    //normalize(kernel, kernel, 1.0, 0.0, 4, -1, noArray());  //doesn't help

    //cout << kernel.size() << endl;  // ... gives 11, 11

    //however, the example from tutorial that does work:
    //kernel = Mat::ones( 11, 11, CV_32F )/ (float)(11*11);

    //default value (-1,-1) here means that the anchor is at the kernel center.
    Point anchor = Point(-1,-1);

    //value added to the filtered pixels before storing them in dst.
    double delta = 0;

    //alright, let's do this...
    filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT );

    imshow("Source", src);     //<< unhandled exception here
    imshow("Kernel", kernel);
    imshow("Destination", dst);
    waitKey(1000000);

    return 0;
}

As you can see, using the tutorials kernel works fine, but my image will crash the program, I've tried changing the bit-depth, normalizing, checking size and lots of commenting out blocks to see where it fails, but haven't cracked it yet.

The image is, '1.png':

1.png

And the kernel I want 'kernel.png':

kernel.png

I'm trying to see if I can get a hotspot in dst at the point where the eye catchlight is (the kernel I've chosen is the catchlight). I know there are other ways to do this, but I'm interested to see how effective convolving the catchlight over itself is. (autocorrelation I think that's called?)

Direct questions:

  • why the crash?
  • is the crash indicating a fundamental conceptual mistake?
  • or (hopefully) is it just some (silly) fault in the code?

Thanks in advance for any help :)

edit retag flag offensive close merge delete

Comments

try: src = imread( "1.png", 0 ); and Mat kernel = imread( "kernel.png", 0 );

(without the '0', the images will be loaded as 24bit color images)

berak gravatar imageberak ( 2015-03-27 07:11:55 -0500 )edit

yes, it's true ! the kernel needs to be 1 channel only ... (and I even wrote that in the comments). The error gave me no indication of this, I need to look into exception handling settings (are there any in VS?). Put it up as an answer and I'll upvote and accept :)

Lamar Latrell gravatar imageLamar Latrell ( 2015-03-27 19:30:12 -0500 )edit