I've once been toying with filtering images in J, and came up with this:

NB. Filter with function (any rank).
NB. Function will be applied between shifted versions,
NB. with weights according to values in the kernel.
NB. If weights should not be used, use f@] (or filtm)
NB. further a "neutral" value is asked, which should be one not
influencing the behaviour of the function
NB. with respect to the other values in the image (eg. __ for >./, or _ for <./)
NB. strel ( neutral filt operation ) image

filt =: 2 : '(s2v@[ v s2i@[ |.!.m ])'
    NB. sparse kernel to offset indices for shifting
    s2i =: (4&$. -&.|: -:@<:@$)@$.
    NB. sparse kernel to values
    s2v =: 5$.$.

This filters an image of any rank (can be 3D or 4D arrays resulting
from MRI images and the like if you want).

A Gaussian filter can thus be made:

NB. Gaussian filter for noise removal
NB. sigma gauss filter_size : where sigma is the filter with, and filter_size
NB. the size of the kernel.

gauss =: +:@*:@[ (^@-@(%~ +/&:*:@]) % o.@[) (,:|:)@:(] #"0 i:@-:@<:@])

NB. Normalizes a filter, such that the sum of all elements equals 1.
normalize =: (% +/@,)

NB. Gaussian 5x5 filter, sigma = 1.4
B =: normalize 1.4 gauss 5
Bfilt =: B&(0 filt (+/@:*))

NB. Sobel operator as per [2]
NB. GTheta returns 2 bands: gradient magnitude and angle.
gx =: 1 2 1 */ _1 0 1
gy =: 1 0 _1 */ 1 2 1
sobx =: gx&(0 filt (+/@:*))
soby =: gy&(0 filt (+/@:*))
GTheta =: 2 0 1 |: *.@(soby + j.@sobx)

So this basically does the first 2 steps of [1] for you.

If anyone would like to extend this with steps 3 to 5, feel free to do so...

A little try on our good old toucan image:

load 'viewmat'
NB. Some verbs converting between integer and RGB planes.
NB. I know of the rgb script, but prefer them to be in planes.
rgb2i =: (256 256 256&#.)@:<. :. i2rgb
i2rgb =: (2 0 1 |: (256 256 256&#:)@:<.) :. rgb2i

NB. get the luminosity
L =:  (>./ -:@+ <./)

NB. make viewmat display gray values:
gray =: 3#"1 ,. i. 256
gv =: gray&viewmat

NB. get the luminosity of the toucan image:
TL =: L i2rgb readbmp jpath '~addons/graphics/bmp/toucan.bmp'

gv TL                                NB. original luminosity
gv TG =: Bfilt TL                NB. after Gaussian filter
gv {. Tgrad =: GTheta TG   NB. gradient magnitude
gv {: Tgrad                        NB. gradient angle

You see there's a strange bevel around the resulting image, this is
due to "filt" padding with 0's
A better option would be to have the image padded with pixels
mirrorred around the edges, but I have not found a general (ie.
working for any rank) way of doing this...
All comments are welcome.

Best regards,

Jan-Pieter

[0]: http://en.wikipedia.org/wiki/Sobel_operator
[1]: 
http://en.wikipedia.org/wiki/Canny_edge_detector#Process_of_Canny_edge_detection_algorithm

2015-04-27 0:03 GMT+02:00 Raul Miller <[email protected]>:
> Yes, I read it.
>
> It also includes a variety of optional or ambiguous aspects. For
> example, the edge detection algorithm should - from the description -
> include some edge around the border of the image. And yet the example
> edge-detection result shows no such edges, nor is the matter discussed
> anywhere that I noticed.
>
> See also the references to "Improvement". But note also the use of the
> word "may" in the rosettacode task page and the quantization of angles
> in step 3.
>
> I expect that most of these issues come down to which approximations
> one uses and of course how one treats "edge cases" (pun intended). But
> that's sort of the entire issue, isn't it?
>
> Thanks,
>
> --
> Raul
>
> On Sun, Apr 26, 2015 at 4:33 PM, Skip Cave <[email protected]> wrote:
>> Raul,
>>
>> Wikipedia has a pretty well-defined spec for Canny edge detection:
>>
>> http://en.m.wikipedia.org/wiki/Canny_edge_detector
>>
>> Skip
>> On Apr 26, 2015 2:39 PM, "Raul Miller" <[email protected]> wrote:
>>
>>> http://rosettacode.org/wiki/Canny_edge_detector currently does not
>>> have a J implementation.
>>>
>>> Worse, the task itself is somewhat ill-specified, with a variety of
>>> open-ended possibilities.
>>>
>>> This suggests, to me, that it would be better handled by someone who
>>> works images and/or edge detection in some other contexts (because
>>> this kind of experience would helpfully inform the implementation
>>> decisions).
>>>
>>> Anyone interested?
>>>
>>> Thanks,
>>>
>>> --
>>> Raul
>>> ----------------------------------------------------------------------
>>> For information about J forums see http://www.jsoftware.com/forums.htm
>>>
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to