Creating filter from an ideal 2D step function

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Locked
Parshiala
Posts: 4
Joined: 2015-02-10T15:04:34-07:00
Authentication code: 6789

Creating filter from an ideal 2D step function

Post by Parshiala »

Hello everyone,

How can one generate an edge detection kernel using the ideal 2D step function which is S(x,y)={(a for (xcosθ+ysinθ)<ρ OR a+h for (xcosθ+ysinθ)≥ρ)} which is geometrically represented as follows:
Image

I actually need to generate a 5x5 kernel so that I can apply it onto an image to detect edges. I would appreciate if someone can give me a detailed or high level idea.

Thank you,

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Creating filter from an ideal 2D step function

Post by fmw42 »

Unless a is very large, you won't get an ideal circle for a spatial domain filter.

To create a circle, you just use -draw "circle ..."

see
http://www.imagemagick.org/Usage/draw/#circles

You would be better to create the JINC function as the filter in the frequency domain using -fx "jinc(x)" and apply it to the fourier transform of the image, then transform back. See

http://www.imagemagick.org/script/fx.php
http://www.fmwconcepts.com/imagemagick/ ... al_defocus

Parshiala
Posts: 4
Joined: 2015-02-10T15:04:34-07:00
Authentication code: 6789

Re: Creating filter from an ideal 2D step function

Post by Parshiala »

You're misunderstanding the question. My is how to generate a 5x5 edge detection kernel from the edge detection?

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Creating filter from an ideal 2D step function

Post by fmw42 »

Parshiala wrote:You're misunderstanding the question. My is how to generate a 5x5 edge detection kernel from the edge detection?
Now I really do not understand what you want! Please clarify.

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Creating filter from an ideal 2D step function

Post by fmw42 »

fmw42 wrote:
Parshiala wrote:You're misunderstanding the question. My is how to generate a 5x5 edge detection kernel from the edge detection?
Now I really do not understand what you want! Please clarify.
The circle kernel as an image can be created as an image by using -draw "circle ..." and saving as txt: output format. That gives you the values you will need. If the kernel is 5x5, then your constant color (white) circle will have a radius of 2 (2x2+1=diameter). But it won't look much like a circle due to the values being rasters.

You can also create it from a formula, using -fx. See http://www.imagemagick.org/script/fx.php

the value a=1 should be used. theta=atan2(j/i)

Code: Select all

convert -size 5x5 xc: -fx "x=i-2; y=j-2; theta=atan2(y,x); r=(x*cos(theta)+y*sin(theta); r<2?1:0" txt:
Note IM indices i,j start at zero. So the center pixels is at 2,2.

But all I get for this is solid 100%

Code: Select all

# ImageMagick pixel enumeration: 5,5,65535,srgb
0,0: (100%,100%,100%)  #FFFFFFFFFFFF  white
1,0: (100%,100%,100%)  #FFFFFFFFFFFF  white
2,0: (100%,100%,100%)  #FFFFFFFFFFFF  white
3,0: (100%,100%,100%)  #FFFFFFFFFFFF  white
4,0: (100%,100%,100%)  #FFFFFFFFFFFF  white
0,1: (100%,100%,100%)  #FFFFFFFFFFFF  white
1,1: (100%,100%,100%)  #FFFFFFFFFFFF  white
2,1: (100%,100%,100%)  #FFFFFFFFFFFF  white
3,1: (100%,100%,100%)  #FFFFFFFFFFFF  white
4,1: (100%,100%,100%)  #FFFFFFFFFFFF  white
0,2: (100%,100%,100%)  #FFFFFFFFFFFF  white
1,2: (100%,100%,100%)  #FFFFFFFFFFFF  white
2,2: (100%,100%,100%)  #FFFFFFFFFFFF  white
3,2: (100%,100%,100%)  #FFFFFFFFFFFF  white
4,2: (100%,100%,100%)  #FFFFFFFFFFFF  white
0,3: (100%,100%,100%)  #FFFFFFFFFFFF  white
1,3: (100%,100%,100%)  #FFFFFFFFFFFF  white
2,3: (100%,100%,100%)  #FFFFFFFFFFFF  white
3,3: (100%,100%,100%)  #FFFFFFFFFFFF  white
4,3: (100%,100%,100%)  #FFFFFFFFFFFF  white
0,4: (100%,100%,100%)  #FFFFFFFFFFFF  white
1,4: (100%,100%,100%)  #FFFFFFFFFFFF  white
2,4: (100%,100%,100%)  #FFFFFFFFFFFF  white
3,4: (100%,100%,100%)  #FFFFFFFFFFFF  white
4,4: (100%,100%,100%)  #FFFFFFFFFFFF  white
Your 5x5 gradient is perhaps too small to represent a circle reasonably or I have made a mistake.

Perhaps someone else can see what I am doing wrong.

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Creating filter from an ideal 2D step function

Post by snibgo »

Unbalanaced parentheses, and "r" is a reserved word. This works:

Code: Select all

%IM%convert -size 5x5 xc: -fx "XX=i-2;YY=j-2;theta=atan2(YY,XX);RR=XX*cos(theta)+YY*sin(theta);RR<2?1:0" txt:
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Creating filter from an ideal 2D step function

Post by fmw42 »

Yes, thanks snibgo.

This might be better as it sets values exactly equal to rad=2 to 0.5 so there is some antialiasing.

Code: Select all

convert -size 5x5 xc: -fx "xx=i-2; yy=j-2; theta=atan2(yy,xx); rr=xx*cos(theta)+yy*sin(theta); rr<2?1:rr>2?0:rr==2?0.5:0" txt:

Code: Select all

# ImageMagick pixel enumeration: 5,5,65535,srgb
0,0: (0%,0%,0%)  #000000000000  black
1,0: (0%,0%,0%)  #000000000000  black
2,0: (50.0008%,50.0008%,50.0008%)  #800080008000  srgb(50.0008%,50.0008%,50.0008%)
3,0: (0%,0%,0%)  #000000000000  black
4,0: (0%,0%,0%)  #000000000000  black
0,1: (0%,0%,0%)  #000000000000  black
1,1: (100%,100%,100%)  #FFFFFFFFFFFF  white
2,1: (100%,100%,100%)  #FFFFFFFFFFFF  white
3,1: (100%,100%,100%)  #FFFFFFFFFFFF  white
4,1: (0%,0%,0%)  #000000000000  black
0,2: (50.0008%,50.0008%,50.0008%)  #800080008000  srgb(50.0008%,50.0008%,50.0008%)
1,2: (100%,100%,100%)  #FFFFFFFFFFFF  white
2,2: (100%,100%,100%)  #FFFFFFFFFFFF  white
3,2: (100%,100%,100%)  #FFFFFFFFFFFF  white
4,2: (50.0008%,50.0008%,50.0008%)  #800080008000  srgb(50.0008%,50.0008%,50.0008%)
0,3: (0%,0%,0%)  #000000000000  black
1,3: (100%,100%,100%)  #FFFFFFFFFFFF  white
2,3: (100%,100%,100%)  #FFFFFFFFFFFF  white
3,3: (100%,100%,100%)  #FFFFFFFFFFFF  white
4,3: (0%,0%,0%)  #000000000000  black
0,4: (0%,0%,0%)  #000000000000  black
1,4: (0%,0%,0%)  #000000000000  black
2,4: (50.0008%,50.0008%,50.0008%)  #800080008000  srgb(50.0008%,50.0008%,50.0008%)
3,4: (0%,0%,0%)  #000000000000  black
4,4: (0%,0%,0%)  #000000000000  black

Parshiala
Posts: 4
Joined: 2015-02-10T15:04:34-07:00
Authentication code: 6789

Re: Creating filter from an ideal 2D step function

Post by Parshiala »

@fmw42: Thank you so much :) I scanned through your post again, implemented and got just what I was looking for. Thanks again.

Just one theoretical question, I was wondering how the values within an edge detection kernel be interpreted. For instance, in a simple edge detection matrix as this: [ -2 -2 -2 -2 -2 ; -1 -1 -1 -1 -1 ; 0 0 0 0 0 ; 1 1 1 1 1 ; 2 2 2 2 2], I can see that the sum of all values within the matrix equals zero. Also, the values are adjusted such that zeros fall horizontally in middle of the overlapped image. And the intensities change as we move towards the top and bottom of the matrix. This is take care of the intensity variations and distinguishing the edge pixels from the ones immediately next to those pixels, am I correct?

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Creating filter from an ideal 2D step function

Post by fmw42 »

I just posted what you requested by the formula you provided. I am not sure what you are being asked to do. A circle of with constant white interior and constant black outside is a low pass filter. If you make it black inside and white outside, it is a high pass filter. Neither are edge detectors. Do you have more of a reference.

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Creating filter from an ideal 2D step function

Post by fmw42 »

Perhaps what you want is directional derivative. See

http://www.fmwconcepts.com/imagemagick/ ... /index.php
and
the directional derivative section of
http://www.fmwconcepts.com/imagemagick/ ... tering.pdf
where it has the same formula, but uses unit derivative x'=d/dx and y'=d/dy rather than x and y.

Pick any of the prewitt or sobel, etc d/dx and d/dy or my 5x5 binomial weighted digital kernels and use those with your formula, element by element.

My script above will compute it for the prewitt type filter

Parshiala
Posts: 4
Joined: 2015-02-10T15:04:34-07:00
Authentication code: 6789

Ideal two dimensional step function

Post by Parshiala »

I have asked the implementation of this part before. However, I am confused about the understanding the representation.

So currently implementing a 5x5 kernel using the following ideal two-dimensional step function:
Image

My first question is: Is this ideal 2D step function based on Heuckel's algorithm?

This function is geometrically represented as follows:
Image

I have generated the 5x5 by iterating the rows and columns up to 5 units. However, I am still trying to understand the geometric representation. Given I want to construct a 5x5 kernel and 'a' here denotes the diameter, what would be the radius? And what is 'h' in 'a+h'. Overall, how can one interpret this geometric representation?

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Ideal two dimensional step function

Post by snibgo »

I am merging this thread with your other one.
Parshiala wrote:... and 'a' here denotes the diameter,
No, it doesn't.

'a' represents the value returned for the unhatched area, where (x.cosTheta + y.sinTheta) < rho.

'a+h' represents the value returned for the hatched area.

Hueckel's name is spelled with UE, not EU.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Creating filter from an ideal 2D step function

Post by fmw42 »

Using a step function is not going to create an edge image as far as I know. It is going to blur your image, since it is a low pass filter. I think you need the directional derivative, which uses the same type formula, but x is the x first derivative (x') and y is the y first derivative (y'). Look at my reference to digital image filtering as listed above.

Locked