How to smooth image after blurring?

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?".
rui_liu
Posts: 7
Joined: 2015-08-27T11:28:35-07:00
Authentication code: 1151

How to smooth image after blurring?

Post by rui_liu »

Hello, I have a problem with using blurring effect of ImageMagick. I'm building a music app and need to blur the album image as background. The processed image has to be blurry enough so that you cannot recognise the original image. I'm using command

Code: Select all

convert -blur 0x50 input_image output_image
to blur the image.

However, when the image doesn't contain too much colour, say the album cover is a sketch painting with only white and black, the colour in the result image seems not gradient.(Sorry about my english, I'm not sure how to describe this). So the image appears to be several colour blocks. What I need is to smooth the image, so that the colour will change smoothly, not rapidly...

I'm a new comer to user ImageMagick, any ideas would be appreciated!

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

Re: How to smooth image after blurring?

Post by fmw42 »

convert -blur 0x50 input_image output_image
Proper syntax would be

Code: Select all

convert input_image -blur 0x50 output_image
though that may not make a difference.

Perhaps you can post some example before and after images to some place like dropbox.com and put the URLs here.

Also please identify your version of Imagemagick and your platform.

Line art may require a much larger sigma value in your blur??

For large blur sigmas, you can do fast blurring by reducing the image first via -resize, blurring (using an appropriately smaller sigma if desired), and then resizing back to the original size.

For example, see http://www.fmwconcepts.com/imagemagick/ ... fast_gblur
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to smooth image after blurring?

Post by snibgo »

rui_liu wrote:So the image appears to be several colour blocks. What I need is to smooth the image, so that the colour will change smoothly, not rapidly...
This may be a bit-depth problem. Try adding "+depth" before writing the output:

Code: Select all

convert input_image -blur 0x50 +depth output_image
snibgo's IM pages: im.snibgo.com
rui_liu
Posts: 7
Joined: 2015-08-27T11:28:35-07:00
Authentication code: 1151

Re: How to smooth image after blurring?

Post by rui_liu »

Thanks for replying~

Hi, snibgo. I have tried your solution, but that doesn't work. Thanks anyway.

And fmw42, my operating system is Mac Yosemite. I installed ImageMagick today, the version is 6.9.1-10. I put the pictures to dropbox, please tell me if you can see it. Also note that the image after blur looks better online than on my local computer, for which I don't why. Probably because during the process of uploading, dropbox is doing some tricks on the images, and actually that's what I need.

But you can still see the problem even in the better image. The image is more white in the centre, but it goes to black not very smoothly.

Before blur: https://www.dropbox.com/s/kq4n8hc82jziq ... r.jpg?dl=0
After blur: https://www.dropbox.com/s/hz1n2qteejkhpdp/blur.jpg?dl=0

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

Re: How to smooth image after blurring?

Post by fmw42 »

Your after blur result looks totally dark on dropbox and even when I download it. I cannot see any variation unless I stretch the image to full dynamic range.

Perhaps your viewer is stretching the image to full dynamic range or adding some gain? How are you viewing this image on your system?

I am on Mac OSX Snow Leopard and Preview shows it as visually uniform black. So does GraphicConverter and IM display.

So I do not know what you are seeing that is incorrect for you.

Also I would not save intermediate images as JPG, because each time you open and save to JPG, it will change and lose information. Best to save intermediate images in your processing flow as PNG or IM MIFF or TIFF. Some non-compressed format.
rui_liu
Posts: 7
Joined: 2015-08-27T11:28:35-07:00
Authentication code: 1151

Re: How to smooth image after blurring?

Post by rui_liu »

Thanks for your reply fmw42. After I download the image from dropbox, it did show uniform black as you said. But when I view it before I upload, it looks much worse. I'm using preview to view the image.

Is there any way for me to send the file directly without uploading online? So that you can probably see what the actually problem is.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to smooth image after blurring?

Post by fmw42 »

See my private message to you on this forum.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to smooth image after blurring?

Post by snibgo »

The input is mostly very dark, with thin light lines. When this is blurred heavily, naturally the entire result is very dark. JPEG is 8 bits, so only 256 possible values in total. We get only a few of them, 38 to be exact.

Why are you using JPEG for this? Why is the input JPEG? Does the output need to be JPEG?

Rule of thumb: don't ever use JPEG for anything at all unless you are really sure you need to, AND you know why it is a bad idea.

My "+depth" suggestion won't help with JPEG, because JPEG is limited to 8 bits.
snibgo's IM pages: im.snibgo.com
rui_liu
Posts: 7
Joined: 2015-08-27T11:28:35-07:00
Authentication code: 1151

Re: How to smooth image after blurring?

Post by rui_liu »

Thanks for your reply snibgo.

I tried to change the format to png, and it did help! However, I can still see the image is not completely smooth. I wonder is there an option like convert -smooth. Or other image processing tools to smooth the image with only a few colors?

What you said makes a lot of sense. The problem most possibly lies in that the color number is not enough.

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

Re: How to smooth image after blurring?

Post by snibgo »

Code: Select all

convert before_blur.jpg -blur 0x50 -auto-level +depth sm_blur.png
Image
This looks smooth to me.

What result do you get, from what command?
snibgo's IM pages: im.snibgo.com
rui_liu
Posts: 7
Joined: 2015-08-27T11:28:35-07:00
Authentication code: 1151

Re: How to smooth image after blurring?

Post by rui_liu »

I'm using the same command but without -auto-level option.

However, the result image you posted is not what I want. I need the overall color tune remains black, and smooth enough to be background. But I think you get the problem I mean.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to smooth image after blurring?

Post by snibgo »

Without "-auto-level", the result is dark, of course. But it is smooth.

Code: Select all

convert before_blur.jpg -blur 0x50 +depth sm_blur2.png
Image
However, the maximum value is about 10,000 (out of 65535). There are 9490 different values. Your screen may not distinguish well between black values. Try adjusting contrast and brightness levels on your screen.

You are using Q16 or higher, I hope, with a fairly recent version? What does "convert -version" show?
snibgo's IM pages: im.snibgo.com
rui_liu
Posts: 7
Joined: 2015-08-27T11:28:35-07:00
Authentication code: 1151

Re: How to smooth image after blurring?

Post by rui_liu »

Yes, I downloaded ImageMagick today, and it's 6.9.1-10. After using your command and change the output format to png, it becomes better, however, I still want it to be more smooth, is it possible? As you can see, in the image you just posted, if you watch closely, you can still find the color is not very smooth. And since this image will then be enlarged to fit the background, that imperfection will become worse.

Is there any way to make it more smooth?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to smooth image after blurring?

Post by snibgo »

What do you mean by "not smooth"? What does that mean? Can you quantify it?

97% of pixels are different to the pixel on the right, and 98.6% are different to the pixel beneath. It can't get much smoother.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to smooth image after blurring?

Post by fmw42 »

Copy of earlier PM for others to follow the thread.


If you apply auto-level, you will see what I think you are getting, stair-stepping. I am not sure you can do better unless you use a much larger sigma.

Code: Select all

convert before_blur.jpg -auto-level before_blur_al.png

You can make a larger blur by resizing the image down, blurring and resizing back up. But it will now have broader steps. The following is equivalent to 5*50=250 sigma, but will process very fast.

Code: Select all

convert before_blur.jpg -resize 20% -blur 0x50 -resize 500% blur_fred1.png

If you want a perfect blur, then just get the average value of the image and use that. The following will do that by scaling the image down to one pixel and then scaling that pixel back up to the image original size.

Code: Select all

convert before_blur.jpg -scale 1x1! -scale 500x500! blur_fred2.png
But in this case, you might as well just create a constant mid gray image.

Or perhaps just use a smaller blur, so you get the idea of the white lines, but they are not distinct, such as

Code: Select all

convert before_blur.jpg -blur 0x5 blur_fred3.png
Post Reply