[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [XForms] image convolution

From: Jens Thoms Toerring
Subject: Re: [XForms] image convolution
Date: Thu, 26 Apr 2012 19:25:42 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

Hi Al,

On Thu, Apr 26, 2012 at 06:01:08PM +0200, alessandro basili wrote:
> I'm writing an application to display an image and do some processing on
> it. Since the images are severely affected by 'salt and pepper noise' I
> wanted to use a gaussian filter to get rid of it.
> Once I defined the kernel I wanted to use the flimage_convolve function,
> but apparently I get segmentation fault.
> What looks to me strange in the function is that a variable /weight/ is
> calculated out of the kernel but in the following way:
> >     for (weight = i = 0; i < kcol * krow; i++)
> >         weight += kernel[0][i];
> where kcol and krow are the columns and rows of the kernel. Since the
> kernel is allocated with fl_get_matrix function the variable /i/ will
> certainly go beyond the array dimensions.
> Is this a potential problem or I'm missing something?

What you get back from fl_get_matrix() is actually (a pointr
to) an array of pointers (lets call ir 'result'), with result[0]
being a pointer to memory of kcols*krows times the size of the
individual elements bytes. result[1] is a pointer to the start
of the second row of the elements, resut[2] the thri row etc.

This is actually a not too uncommon trick to create something
that can be used like a normal two-dimensional matrix (even
though it isn't one), see e.g. "Numerical Recipes". It all
works in the end due to the fact that the compiler will al-
ways evaluate

   a[i]    as   *(a + i)

And since 'result[0]' points to the start of all memory needed
for the two dimensions, using 'i' to iterate over all elements
(instead of iterating over e.g. 'i' and 'j' for rows and col-
umns) actually is ok - the memory pointed to is actually large
enough so that 'i' won't point beyond the allocated region.

I'll try to illustrate it with a bit of "ASCII art" for a 3x3
matrix - you will have to use a fixed-sized font to be able
to see it properly:

    ---------       - - - - - - - - - 
   |result[0]| ==> | | | | | | | | | |     points to 1. element
    ---------       - - - - - - - - - 
   |result[1]| ===========^     ^          points to 4. element
    ---------                   |
   |result[2]| =================           points to 7. elemeny

> By the way, my pseudo code is the following:
> > 
> > int kcol = 5, krow = 5;
> > int **kernel = fl_get_matrix(kcol, krow, sizeof(**kernel));
> > 
> > init_kernel(kernel, "gaussian", kcol, krow);
> > flimage_convolve(im, kernel, kcol, krow);

This looks quite ok to me, but I can't tell what you're
doing in init_kernel();-) Do you have something I could
debug on? I have spent only minmal amount of time on the
flimage stuff, so there could still lurk a lot of bugs...

                            Best regards, Jens
  \   Jens Thoms Toerring  ________      address@hidden
   \_______________________________      http://toerring.de

reply via email to

[Prev in Thread] Current Thread [Next in Thread]