using a sobel operator - edge detection

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: using a sobel operator - edge detection

Post by fmw42 »

Here is a LOG (no zero crossing added yet) on the grayscale version of the image.

Image

For comparison, here is the DOG simulation of the LOG.
g1=1
g2=1.6
convert flowers3.jpg -colorspace gray \
\( -clone 0 -blur 0x$g1 \) \
\( -clone 0 -blur 0x$g2 \) \
-delete 0 +swap -compose minus -composite -contrast-stretch 0 -negate \
flowers3g_dog_${g1}_${g2}_b.png
Image

And here is the LOG implementing the laplacian in the frequency domain via -fft, with the best result at about c1=20 (tried 8, 16, 20, 24, 32)

g1=1
c1=20
dim=256
cxy=`convert xc: -format "%[fx:$dim/2]" info:`
cy2=`convert xc: -format "%[fx:$cxy + $c1 - 1]" info:`
echo "cxy=$cxy; cy2=$cy2"
convert flowers3.jpg -colorspace gray \
\( -clone 0 -blur 0x$g1 -fft \) \
\( -size ${dim}x${dim} xc:black -fill white \
-draw "circle $cxy,$cxy $cxy,$cy2" -alpha off -negate \) \
\( -clone 1 -clone 3 -compose multiply -composite \) \
-delete 0,1,3 +swap -ift -contrast-stretch 0 -negate \
flowers3g_log_${g1}_${c1}.png
Image

I actually thing the DOG simulation of the LOG is better (sharper and cleaner)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: using a sobel operator - edge detection

Post by fmw42 »

Trying to reproduce your LOG with zero crossing results under Q16 HDRI:

Image

filt="
-1,-1,-1,
-1,8,-1,
-1,-1,-1
"
sig=5
convert flowers3.jpg -blur 0x$sig \
-evaluate multiply 0.125 -bias 50% -convolve "$filt" \
-solarize 50% -level 0%,50% -contrast-stretch 5%x0% \
-edge 1 flowers3_log_b${sig}_l3.jpg
Image

which leaves it centered about gray. So I just use -black-threshold at about 75%

filt="
-1,-1,-1,
-1,8,-1,
-1,-1,-1
"
sig=5
convert flowers3.jpg -blur 0x$sig \
-evaluate multiply 0.125 -bias 50% -convolve "$filt" \
-solarize 50% -level 0%,50% -contrast-stretch 5%x0% \
-edge 1 -black-threshold 75% flowers3_log_b${sig}_l3_bt75.jpg
Image

Now trying a larger laplacian 5x5
filt="
-4,-1,0,-1,-4,
-1,2,3,2,-1,
0,3,4,3,0,
-1,2,3,2,-1,
-4,-1,0,-1,-4,
"
sig=5
convert flowers3.jpg -blur 0x$sig \
-evaluate multiply 0.125 -bias 50% -convolve "$filt" \
-solarize 50% -level 0%,50% -contrast-stretch 5%x0% \
-edge 1 -black-threshold 75% flowers3_log_b${sig}_l5_bt75.jpg
Image

But not much change.

So going back to the 3x3 laplacian but changing the blurring sigma to 7

filt="
-1,-1,-1,
-1,8,-1,
-1,-1,-1
"
sig=7
convert flowers3.jpg -blur 0x$sig \
-evaluate multiply 0.125 -bias 50% -convolve "$filt" \
-solarize 50% -level 0%,50% -contrast-stretch 5%x0% \
-edge 1 -black-threshold 75% flowers3_log_b${sig}_l3_bt75.jpg
Image

and making it even larger at sigma of 9

filt="
-1,-1,-1,
-1,8,-1,
-1,-1,-1
"
sig=9
convert flowers3.jpg -blur 0x$sig \
-evaluate multiply 0.125 -bias 50% -convolve "$filt" \
-solarize 50% -level 0%,50% -contrast-stretch 5%x0% \
-edge 1 -black-threshold 75% flowers3_log_b${sig}_l3_bt75.jpg
Image

and smaller at sigma of 3
filt="
-1,-1,-1,
-1,8,-1,
-1,-1,-1
"
sig=3
convert flowers3.jpg -blur 0x$sig \
-evaluate multiply 0.125 -bias 50% -convolve "$filt" \
-solarize 50% -level 0%,50% -contrast-stretch 5%x0% \
-edge 1 -black-threshold 75% flowers3_log_b${sig}_l3_bt75.jpg
Image

Interesting reference at http://www.csee.wvu.edu/~xinl/courses/e ... ection.ppt
HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

Re: using a sobel operator - edge detection

Post by HugoRune »

As reference, the DOG says to use a second gaussian that is larger than the first. (subtract larger from smaller) So I had assumed, but not investigated yet the need for the laplacian to be bigger than the gaussian.
That is a very god point!
I did not even consider the size of the laplacian kernel so far.
fmw42 wrote:Trying to reproduce your LOG with zero crossing results under Q16 HDRI:

Image
Note: I used the big version from wikipedia, and scaled all pictures down when uploading them to the web. I think you did too, but just to make sure.
filt="
-1,-1,-1,
-1,8,-1,
-1,-1,-1
"
sig=5
convert flowers3.jpg -blur 0x$sig \
-evaluate multiply 0.125 -bias 50% -convolve "$filt" \
-solarize 50% -level 0%,50% -contrast-stretch 5%x0% \
-edge 1 flowers3_log_b${sig}_l3.jpg
Image

which leaves it centered about gray.
That is very weird! after -solarize and -level 0%,50% it really should not be centered around gray anymore.

One possibility: I think I accidentally used a wrong scale: evaluate -multiply 0.0625 may be necessary to prevent all clipping. I redid the image with this scale factor, but the output looks still the same.
But maybe the -contrast-stretch on your HDRI is influenced by some stray pixels that are clipped on my version? It stil does not really make sense.
That is interesting, but depressing. Their results look very bad even for their Robust Laplacian-based Edge Detector with zero crossing.
I am not sure whether laplacian edge detection can be coerced to work well enough to compete with sobel or DOG in imagemagick.

Meanwhile I have some very nice results with a sort of "Sobelian-of-Gaussian" procedure.
it can detect strong edges while ignoring weak edges, with high pixel precision. I try to get some details about this posted soon.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: using a sobel operator - edge detection

Post by fmw42 »

For simplicity of size and for the FFT, I actually took their image and -resize 256x256! (so some distortion)

I am not sure about the mid-gray either at this point and if it is associated with HDRI. I was surprised. I get a much differernt result if I don't use the bias and all your other solarize and level processing and just assume that HDRI will take care of any negatives and save as pfm. But I did not want to confuse things by introducing those results and did not spend much time on that to investigate further, but here it is (same in png as pfm)

convert flowers3.jpg -blur 0x5 \
-evaluate multiply 1 -convolve -1,-1,-1,-1,8,-1,-1,-1,-1 \
-contrast-stretch 0x10% \
-edge 1 flowers3_log_b5_l3.png
Image

But I am suspicious that I may be still getting only one-sided edges (positives only), but have not looking into it further.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: using a sobel operator - edge detection

Post by anthony »

I do not believe -solarize is the right way to detect zero crossings (crossing gray50).
If you have two pixles gray25 and gray75 next to each other and you solarize you will get two grey25 pixels and the edge between them 'dissapeared'

The better solution for zero crossings is... threshold! Or at least stretch the image colors so as to produce a even sharper gradient along the zero crossing.

The difficult part is to get it to ignore the areas that are close to gray50 and may cross gray50 but not sharply.

NOTE: do not use -contrast stretch! It may not stretch the image equally centered on gray50!!!!!!
That is you may lose your 'zero' bias. With that in mind either -level or a -sigmoidal-contrast will work better.

Code: Select all

convert flowers.jpg -evaluate multiply 0.125 -bias 50% -convolve -1,-1,-1,-1,8,-1,-1,-1,-1 -sigmoidal-contrast 30,50% flowers_laplacian.png
NOTE: the above sigmoidal enhanced image highlights the zero crossings, but it also highlights the JPG errors that is present in the image. JPEG tends to produce ringing effects at sharp boundaries.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: using a sobel operator - edge detection

Post by fmw42 »

Anthony, you don't show a result. So if I try that with HDRI, I get the following:

Image

convert flowers3.jpg -evaluate multiply 0.125 -bias 50% \
-convolve -1,-1,-1,-1,8,-1,-1,-1,-1 -sigmoidal-contrast 30,50% \
flowers3_laplacian_sigmoid30x50.png
Image


Is this the result you got? So you still need to extract only values close to mid-gray

Your sigmoid-contrast sharpening is a good idea, though.

If I repeat without the bias, (as I am in HDRI)

convert flowers3.jpg \
-convolve -1,-1,-1,-1,8,-1,-1,-1,-1 -sigmoidal-contrast 30,50% \
flowers3_laplacian_sigmoid30x50_nobias.png
Image

And of course here I should be trying to extract values close to zero in a pfm image so that it keeps positive and negative values.

I believe this is why HugoRune added the -edge to get such values. But I have not thought much yet about getting such values.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: using a sobel operator - edge detection

Post by anthony »

fmw42 wrote:Is this the result you got?
yes
Your sigmoid-contrast sharpening is a good idea, though.

If I repeat without the bias, (as I am in HDRI)

convert flowers3.jpg \
-convolve -1,-1,-1,-1,8,-1,-1,-1,-1 -sigmoidal-contrast 30,50% \
flowers3_laplacian_sigmoid30x50_nobias.png
If you are using HDRI with a 0 bias, then -sigmodial-contrast needs to replace 50% with 0

I think it would even work as the function auto fits to go though 0,0 and 1,1 on its graph, so when used with HDRI and a bias of 0 it will then magnify both positive and negative values in the same way. I'm amased that it does work for bias 0.

You can of course then use -edge, on the symetrically enhanced image.

My warning was on using -contrast-stretch or -lienar-stretch as it many not stretch the image according to a 50% bias! Thus the 'bias' or 'zero' value would shift slightly.

You can also use -level if you are carful that the operation does not change the bias value!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: using a sobel operator - edge detection

Post by fmw42 »

If you are using HDRI with a 0 bias, then -sigmodial-contrast needs to replace 50% with 0
OK

convert flowers3.jpg \
-convolve -1,-1,-1,-1,8,-1,-1,-1,-1 -sigmoidal-contrast 30,0% \
flowers3_laplacian_sigmoid30x0_nobias.png
Image

And pure laplacian which does have both postivie and neg values. But we compute the magnitude (sqrt( pos^2 +neg^2) values, stretch so zero crossings are at zero, apply log to emphsize low values, then negate to make low values (zero crossings) white

convert \( flowers3.jpg \
-convolve -1,-1,-1,-1,8,-1,-1,-1,-1 \) \
\( -clone 0 \) -compose multiply -composite -contrast-stretch 0 -evaluate log 1000 \
-negate flowers3_laplacian_mag_log1000.png
Image

Use lap of gaussian and threshold he result :
convert \( flowers3.jpg -blur 0x5 \
-convolve -1,-1,-1,-1,8,-1,-1,-1,-1 \) \
\( -clone 0 \) -compose multiply -composite -contrast-stretch 0 -evaluate log 1000 \
-negate -black-threshold 90% flowers3_lap3_b5_mag_log1000_bt90.png
Image
HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

Re: using a sobel operator - edge detection

Post by HugoRune »

some experiments with generating a mask with all positive pixels and one with all negative pixels of a laplacian, then dilating them and finding all pixels that are in both masks (these should be the zero-crossings)

Then some filtering of close-to-zero values to exclude weak edges (low-variance filtering).

Code: Select all

convert Flowers_before_difference_of_gaussians.jpg -blur 0x3  
-evaluate multiply 0.125 -bias 50% -convolve -1,-1,-1,-1,8,-1,-1,-1,-1          //Laplacian
-bias 0% ( 
    -clone 0 ( -clone 0 -negate ) -threshold 50%
            // two binary masks, one with all positive values, one with all negative
    -convolve 1,1,1,1,1,1,1,1,1 -threshold 0         // morphological dilate
    -compose multiply -composite             // logical and --> mask with all edge pixels
)
( 
    -clone 0 -solarize 50% -level 0%,50% -contrast-stretch 50%,0% -threshold 0 -negate 
           // one binary mask with all high or low values (not gray)
    -convolve 1,1,1,1,1,1,1,1,1 -threshold 0      // morphological dilate
)
-delete 0 ( -clone 0,1 -compose multiply -composite )         // edge mask && gray mask
 +append test.png
edge mask -----------------------------&-------------------------- gray mask ================================ result
Image

This might work better with some grayscale morpholological operators, so that the hard threshold is not necessary.

a less threshold-y edge mask with blur instead of morphological operators

Code: Select all

convert Flowers_before_difference_of_gaussians.jpg -blur 0x3  
-evaluate multiply 0.125 -bias 50% -convolve -1,-1,-1,-1,8,-1,-1,-1,-1       // laplacian
-bias 0% ( 
    -clone 0 ( -clone 0 -negate )
    -level 50%,100% -contrast-stretch 0       // positive and negative parts
    -blur 0x0.5 -compose multiply -composite -contrast-stretch 1%        // combine them
)
-delete 0 test2.png
Image

That works surprisingly well.

The same without the initial blur (no LOG) and with a bit of fiddling with contrast-stretch

Code: Select all

convert Flowers_before_difference_of_gaussians.jpg  
-evaluate multiply 0.0625 -bias 50% -convolve -1,-1,-1,-1,8,-1,-1,-1,-1 
-bias 0% (
    -clone 0 ( -clone 0 -negate ) -level 50%,100% -contrast-stretch 0 
    -blur 0x0.5 -compose multiply -composite -contrast-stretch 1% 
) 
-delete 0 test2.png
Image

(edit: simplified the command lines somewhat, results still the same)
Last edited by HugoRune on 2009-08-22T09:12:00-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: using a sobel operator - edge detection

Post by fmw42 »

very nice work.

I have a script, morphology, that has many different filters for both binary and grayscale/color images that Anthony and I put together.
HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

Re: using a sobel operator - edge detection

Post by HugoRune »

fmw42 wrote:very nice work.

I have a script, morphology, that has many different filters for both binary and grayscale/color images that Anthony and I put together.
Yeah, that is were I got the dilate for imagemagick.

It looks very impressive. But adapting the grayscale operators for windows will be a project for another day :)

especially since -blur seems to work just as well for this case as dilate.
Post Reply