using a sobel operator - edge detection
- 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
Here is a LOG (no zero crossing added yet) on the grayscale version of the 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
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
I actually thing the DOG simulation of the LOG is better (sharper and cleaner)
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
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
I actually thing the DOG simulation of the LOG is better (sharper and cleaner)
- 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
Trying to reproduce your LOG with zero crossing results under Q16 HDRI:
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
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
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
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
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
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
Interesting reference at http://www.csee.wvu.edu/~xinl/courses/e ... ection.ppt
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
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
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
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
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
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
Interesting reference at http://www.csee.wvu.edu/~xinl/courses/e ... ection.ppt
Re: using a sobel operator - edge detection
That is a very god point!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.
I did not even consider the size of the laplacian kernel so far.
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.fmw42 wrote:Trying to reproduce your LOG with zero crossing results under Q16 HDRI:
That is very weird! after -solarize and -level 0%,50% it really should not be centered around gray anymore.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
which leaves it centered about gray.
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.Interesting reference at http://www.csee.wvu.edu/~xinl/courses/e ... ection.ppt
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.
- 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
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
But I am suspicious that I may be still getting only one-sided edges (positives only), but have not looking into it further.
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
But I am suspicious that I may be still getting only one-sided edges (positives only), but have not looking into it further.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: using a sobel operator - edge detection
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.
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.
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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- 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
Anthony, you don't show a result. So if I try that with HDRI, I get the following:
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
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
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.
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
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
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.
- 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
Some zero crossing references:
http://homepages.inf.ed.ac.uk/rbf/HIPR2/zeros.htm
http://www.owlnet.rice.edu/~elec539/Pro ... acian.html
( the above suggest using the log of magnitude of laplacian to get dark regions as zero crossing and correlate wth the variance image to remove spurious noise).
http://www.ece.iastate.edu/~namrata/EE5 ... racing.pdf
http://euclid.ii.metu.edu.tr/~ion528/demo/lectures/6/3/
http://www.cs.cf.ac.uk/Dave/Vision_lecture/node29.html
http://www.cs.toronto.edu/~arnold/320/0 ... /notes.pdf
http://books.google.com/books?id=OYFYt5 ... ct&f=false
http://ling75.arts.ubc.ca/cogs//cogs300 ... reth80.pdf
http://www.csie.cyut.edu.tw/TAAI2002/TA ... )/C5-8.pdf
http://homepages.inf.ed.ac.uk/rbf/HIPR2/zeros.htm
http://www.owlnet.rice.edu/~elec539/Pro ... acian.html
( the above suggest using the log of magnitude of laplacian to get dark regions as zero crossing and correlate wth the variance image to remove spurious noise).
http://www.ece.iastate.edu/~namrata/EE5 ... racing.pdf
http://euclid.ii.metu.edu.tr/~ion528/demo/lectures/6/3/
http://www.cs.cf.ac.uk/Dave/Vision_lecture/node29.html
http://www.cs.toronto.edu/~arnold/320/0 ... /notes.pdf
http://books.google.com/books?id=OYFYt5 ... ct&f=false
http://ling75.arts.ubc.ca/cogs//cogs300 ... reth80.pdf
http://www.csie.cyut.edu.tw/TAAI2002/TA ... )/C5-8.pdf
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: using a sobel operator - edge detection
yesfmw42 wrote:Is this the result you got?
If you are using HDRI with a 0 bias, then -sigmodial-contrast needs to replace 50% with 0Your 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
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/
https://imagemagick.org/Usage/
- 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
OKIf you are using HDRI with a 0 bias, then -sigmodial-contrast needs to replace 50% with 0
convert flowers3.jpg \
-convolve -1,-1,-1,-1,8,-1,-1,-1,-1 -sigmoidal-contrast 30,0% \
flowers3_laplacian_sigmoid30x0_nobias.png
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
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
Re: using a sobel operator - edge detection
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).
edge mask -----------------------------&-------------------------- gray mask ================================ result
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
That works surprisingly well.
The same without the initial blur (no LOG) and with a bit of fiddling with contrast-stretch
(edit: simplified the command lines somewhat, results still the same)
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
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
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
(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.
- 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
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.
I have a script, morphology, that has many different filters for both binary and grayscale/color images that Anthony and I put together.
Re: using a sobel operator - edge detection
Yeah, that is were I got the dilate for imagemagick.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.
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.