CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2001
    Posts
    88

    Separable kernels from OpenCV

    Hello guys,

    I am learning OpenCV by the book "OpenCV...." and cannot make one exercise there:

    *****************
    Separable kernels. Create a 3-by-3 Gaussian kernel using rows [(1/16, 2/16, 1/16), (2/16, 4/16, 2/16), (1/16, 2/16, 1/16)] and with anchor point in the middle.

    a. Run this kernel on an image and display the results.

    b. Now create two one-dimensional kernels with anchors in the center: one going “across” (1/4, 2/4, 1/4), and one going down (1/4, 2/4, 1/4). Load the same original image and use cvFilter2D() to convolve the image twice, once with the first 1D kernel and once with the second 1D kernel. Describe the results.
    ************************

    I did three those filters, but they do not change my source image, why, where is my error?

    Here is my code:

    Code:
    		IplImage* src = cvLoadImage("4.jpg");
    		cvNamedWindow("src", 1);
    		cvShowImage("src", src);
    		IplImage* rgb[3];
    		float L45[]={
    			1./16., 2./16., 1./16.,
    			2./16., 4./16., 2./16.,
    			1./16., 2./16., 1./16.};//  
    		CvMat*  rgbMat = cvCreateMat(3, 3, CV_32FC1);
    		for (int y = 0; y < 3; y++)
    		{
    			for (int x = 0; x < 3; x++)
    				cvmSet(rgbMat, y, x, L45[y*3 + x]);
    		}
    		IplImage* dst = cvCreateImage(cvSize(src->width, src->height), src->depth, 3);
    		IplImage* dstRGB[3];
    		for (int i = 0; i < 3; i++)
    		{
    			rgb[i] = cvCreateImage(cvSize(src->width, src->height), src->depth, 1);
    			dstRGB[i] = cvCreateImage(cvSize(src->width, src->height), src->depth, 1);
    		}
    		cvSplit(src, rgb[0], rgb[1], rgb[2], NULL);
    		for (int i = 0; i < 3; i++)
    		{
    			cvFilter2D(rgb[i], dstRGB[i], rgbMat);
    		}
    		cvReleaseMat(&rgbMat);
    		cvMerge(dstRGB[0], dstRGB[1], dstRGB[2], NULL, dst);
    		cvNamedWindow("dst", 1);
    		cvShowImage("dst", dst);
    		//across
    		float D[] = {1./4., 2./4., 1./4.};
    		rgbMat = cvCreateMat(1, 3, CV_32FC1);
    		for (int x = 0; x < 3; x++)
    			cvmSet(rgbMat, 0, x, D[x]);
    		for (int i = 0; i < 3; i++)
    		{
    			cvZero(dstRGB[i]);
    			cvFilter2D(rgb[i], dstRGB[i], rgbMat, cvPoint(1, 0));
    		}
    		cvReleaseMat(&rgbMat);
    		cvZero(dst);
    		cvMerge(dstRGB[0], dstRGB[1], dstRGB[2], NULL, dst);
    		cvNamedWindow("Across", 1);
    		cvShowImage("Across", dst);
    		//down
    		rgbMat = cvCreateMat(3, 1, CV_32FC1);
    		for (int y = 0; y < 3; y++)
    			cvmSet(rgbMat, y, 0, D[y]);
    		for (int i = 0; i < 3; i++)
    		{
    			cvZero(dstRGB[i]);
    			cvFilter2D(rgb[i], dstRGB[i], rgbMat, cvPoint(0, 1));
    		}
    		cvReleaseMat(&rgbMat);
    		cvZero(dst);
    		cvMerge(dstRGB[0], dstRGB[1], dstRGB[2], NULL, dst);
    		cvNamedWindow("Down", 1);
    		cvShowImage("Down", dst);
    		cvReleaseImage(&dst);
    		for (int i = 0; i < 3; i++)
    		{
    			cvReleaseImage(&rgb[i]);
    			cvReleaseImage(&dstRGB[i]);
    		}
    		cvWaitKey(0);
    		cvReleaseImage(&src);
    		cvDestroyWindow("src");
    		cvDestroyWindow("dst");
    		cvDestroyWindow("Across");
    		cvDestroyWindow("Down");

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Separable kernels from OpenCV

    I know nothing about your code, but I don't think that

    using rows [(1/16, 2/16, 1/16), (2/16, 4/16, 2/16), (1/16, 2/16, 1/16)]
    is the same as :
    Code:
    float L45[]={
    1./16., 2./16., 1./16.,
    2./16., 4./16., 2./16.,
    1./16., 2./16., 1./16.};
    L45[0] == 1 divided by 16 = 0.0625.

    ... but I could be totally wrong ofcourse

  3. #3
    Join Date
    Aug 2001
    Posts
    88

    Re: Separable kernels from OpenCV

    Quote Originally Posted by Skizmo View Post
    I know nothing about your code, but I don't think that



    is the same as :
    Code:
    float L45[]={
    1./16., 2./16., 1./16.,
    2./16., 4./16., 2./16.,
    1./16., 2./16., 1./16.};
    L45[0] == 1 divided by 16 = 0.0625.

    ... but I could be totally wrong ofcourse
    The OpenCV requires float values for future handling , so 0.0625 is a right value.

  4. #4

    Resolved Re: Separable kernels from OpenCV

    Hello,

    I ran your code... it seems to be working fine !
    I guess it is just that the kernel size is a bit small (3x3) that the change in between the source and destination images is not very noticeable.
    In order to be sure I just did a difference, in your case just do something like:
    Code:
      cvSub(src, dst, dst);
      cvNamedWindow("dst", 1);
      cvShowImage("dst", dst);
    or use a different source image with some fine lines or noise etc...

    Hope it helps, cya !

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Separable kernels from OpenCV

    Convolving a Gaussian kernel with an image simply blurs it a bit.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured