Page 1 of 1

Suggested workflow to smooth/sharpen edges/lines

Posted: 2020-02-28T13:50:37-07:00
by rwillett
Hi,

First of all, thanks for putting together ImageMagick, never used it until now and didn't realise the just how good and powerful it is. Reminds me of Perl (thats a huge compliment) with the power and community.

I've been experimenting with it for a week now and I think I've only just scratched the service but done a remarkable amount of work with it, but feel I'm using about 1% of the capability.

I want to use ImageMagick to refine (I'm choosing my words carefully as I'm not sure if I want to smooth or sharpen or something else) the edge I've detected in an image. So far I can process the image, cut the background out and generate an edge (or line) with no issues. However I want to turn the detected jagged edge from a bitmap into a smooth edge. It doesn't really matter if it's a smooth bitmap or a smooth line.

As an example here's a picture on Dropbox (or I hope it is) that I've processed.

https://www.dropbox.com/s/6b7fq781bjr9p ... 6.png?dl=0

The edge of the wrench, where it's white into black, is quite jagged. I'd like to smooth it out so it's neat. I've looked at sharpening and blurring and then doing a further Canny edge detection but nothing seems to work e.g.

https://www.dropbox.com/s/ox5fss6wpz4l5 ... 1.png?dl=0

and

https://www.dropbox.com/s/k2jekq49pdxd0 ... 1.png?dl=0

I think part of my problem is that the terminology isn't clear to me, am I smoothing it (which I think is actually something like Gaussian Blur) or am I sharpening it?

I've also tried to vectorise it using potrace to see if that helps, but it didn't.

https://www.dropbox.com/s/m4apy5okkf38q ... 1.svg?dl=0

Though I felt that it did smooth it out, but I had so many broken lines I needed to fix a different problem :)

I even downloaded the libpng library and got that working to see if I could write it in C. I at least got it working but after I thought about it, I realised I was simply duplicating (very badly) other peoples work and so discarded that idea for the moment,

Just to be clear, I'm not not looking for somebody to write the script but it would be helpful to have some pointers as the direction to go:

e.g. apply a gaussian blur, negate it, apply a canny edge detection twice, stroke rabbit foot three times and then hop on left foot under a full moon would be helpful.

Many thanks

Rob

Re: Suggested workflow to smooth/sharpen edges/lines

Posted: 2020-02-28T14:29:28-07:00
by fmw42
Try this:

Code: Select all

convert wrench.png -alpha off -threshold 50% -blur 0x5 -level 40x60% x.png
Turn off the alpha channel and it will get rid of some small white spots. Threshold to be sure you have no other values but black and white. Then blur to smooth and use -level to thin the blurring back a bit by mapping some of the high and low values to white and black.

Re: Suggested workflow to smooth/sharpen edges/lines

Posted: 2020-02-28T17:08:52-07:00
by snibgo
As Fred says, though a simple blur followed by threshold works well. A larger blur, eg 0x10, has a greater effect.

However, the input image is almost entirely black or white, caused by some previous processing. That processing may have increased the noise in the edge. If so, it would be better to reduce that effect, rather than reducing the noise after black/white conversion.

Re: Suggested workflow to smooth/sharpen edges/lines

Posted: 2020-02-29T04:58:37-07:00
by rwillett
Thanks for the prompy replies.

As with these things, it's never clear how much information is too much, or in this case too little.

I'll try and draw out the steps taken til now.

1. Here's the original picture.

https://www.dropbox.com/s/gbc78u1vyhovt ... 3.png?dl=0

I chose the background to try and get some contrast between that and the wrench. I have already run

Code: Select all

--autolevel
to enhance the contrast.

2. I then ran the magicwand script to remove the background

Code: Select all

 ./magicwand 160,200 -t 10 -f image -r outside   -m transparent -c trans  test2.3.png test3.png
https://www.dropbox.com/s/gbc78u1vyhovt ... 3.png?dl=0

3. I then ran

Code: Select all

convert test3.png -convolve -1,-1,-1,-1,8,-1,-1,-1,-1 -sigmoidal-contrast 30,50% test3.1.png
to get the outline

https://www.dropbox.com/s/biq0o3otkoimg ... 1.png?dl=0

All the rest of the images and code in the original post is me failing to smooth the edges out. The ragged edge is all the way through my steps and I don't believe the processing has made it any worse. It's certainly not improved it :)

My thinking is that I now need to improve the very first steps in my workflow and try and get to get as smooth an outline there, rather than correct it in post-process (so to speak).

My target to have a smooth outline, whether this is a vestor or a bit map is immaterial to be honest. Processing time is also immaterial (within reason), 5-10 minutes is not an issue. I did wonder if Hough lines would help but they failed as well, though the output was striking.

Thanks for the help,

Rob

Re: Suggested workflow to smooth/sharpen edges/lines

Posted: 2020-02-29T05:49:09-07:00
by snibgo
The original photo is noisy, and hasn't been made worse by processing.

The background has colour, and the spanner (aka wrench) doesn't, so that is a useful way to separate them, eg with the C channel of HCL:

Code: Select all

magick test2.3.png -colorspace HCL -channel 1 -separate +channel -auto-level -threshold 65% -blur 0x10 -threshold 50% out.png

Re: Suggested workflow to smooth/sharpen edges/lines

Posted: 2020-02-29T08:40:57-07:00
by rwillett
I'll try that, thanks.

Re: Suggested workflow to smooth/sharpen edges/lines

Posted: 2020-02-29T08:43:17-07:00
by rwillett
Wow! Thats pretty damm good. Highly impressed with that.

Now all I have to do is work out what it does and why it's so effective ... :)

Many thanks,

Rob

Re: Suggested workflow to smooth/sharpen edges/lines

Posted: 2020-02-29T09:25:30-07:00
by snibgo
You can see the effect of each operation by inserting "+write x_N.png" after each operation, where N is a number.

We take just the C channel of HCL and auto-level. If the spanner had no colour, we could then "-threshold 50%". But the lower edge of the spanner reflects blue light from the background. I reckon "-threshold 65%" compensates for that.

Then we blur to smooth the spanner edge, and threshold to make it black/white.

Re: Suggested workflow to smooth/sharpen edges/lines

Posted: 2020-02-29T10:08:05-07:00
by rwillett
Thanks for the update.

Clearly there is more than one way to skin this cat.

I’ll work through this and play with parameters and