High Pass filter for a batch?

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?".
Post Reply
philipmorg

High Pass filter for a batch?

Post by philipmorg »

I have a sequence of Photoshop actions that produces an effect that I like very much. I only have Photoshop Elements, so I'm not aware of a way to automate this within Photoshop. I'd like to create the same effect using one or more ImageMagick commands (preferably using a script that can process a whole folder of images). Can anyone help?

Here are the Photoshop actions:
  • Create duplicate layer
    Run the High Pass filter on this layer using a radius of 60 pixels
    Set the layer to Hard Light, 40% opacity
    Create a second duplicate layer
    Run the High Pass filter on this layer using a radius of 3 pixels
    Set the layer to Overlay, 50% opacity
    Flatten the image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: High Pass filter for a batch?

Post by snibgo »

"High pass filter" is probably another version of "sharpen".
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: High Pass filter for a batch?

Post by fmw42 »

post links to an input and output example so we can see if it can be closely duplicated

it is hard to tell without testing, but the PS high pass filter, from appearances, could be just a laplacian or LoG or the image minus a gaussian low pass filter.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: High Pass filter for a batch?

Post by fmw42 »

I have determined from some testing that the PS high pass is equivalent to the image minus an IM -blur radiusxsigma with radius=0 and sigma=photoshop radius at 50% bias (in non-HDRI)

Here is a test image:
Image

Here is the photoshop high pass result at radius=60
Image

Here is my IM high pass processing command and result:

infile="birdofparadise.jpg"
blur=60
convert $infile \
\( -clone 0 -blur 0x60 \) \
\( -clone 0 -clone 1 +swap -compose mathematics \
-set option:compose:args "0,1,-1,0.5" -composite \) \
-delete 0,1 birdofparadise_im_hp${blur}.jpg

Image

Here is the photoshop high pass result at radius=3
Image

Here is my IM high pass processing command and result:

infile="birdofparadise.jpg"
blur=3
convert $infile \
\( -clone 0 -blur 0x60 \) \
\( -clone 0 -clone 1 +swap -compose mathematics \
-set option:compose:args "0,1,-1,0.5" -composite \) \
-delete 0,1 birdofparadise_im_hp${blur}.jpg

Image


Here is doing all the processing requested.


infile="birdofparadise.jpg"
outfile="birdofparadise_processed.jpg"
convert $infile \
\( -clone 0 -blur 0x60 \) \
\( -clone 0 -clone 1 +swap -compose mathematics \
-set option:compose:args "0,1,-1,0.5" -composite \
-matte -channel A -evaluate set 40% +channel \) \
\( -clone 0 -blur 0x3 \) \
\( -clone 0 -clone 3 +swap -compose mathematics \
-set option:compose:args "0,1,-1,0.5" -composite \
-matte -channel A -evaluate set 50% +channel \) \
\( -clone 0 -clone 2 -compose hardlight -composite \
-clone 4 -compose overlay -composite \) \
-delete 0-4 $outfile

Image

A before and after example from the requestor would be nice to see and test the kind of imagery that is important to him.
philipmorg

Re: High Pass filter for a batch?

Post by philipmorg »

Thanks for the attempts to help!

You can see an example of the effects of this sequence of actions here: http://philipmorgan.net/HPFilterExamples
philipmorg

Re: High Pass filter for a batch?

Post by philipmorg »

Wow! That was exactly what I'm looking for FMW42! Thank you so much for putting that together. I tried it on my sample file (you can see the comparison to the Photoshop filtered version here: http://philipmorgan.net/HPFilterExamples) and it produced a result, that to my eye, is identical!

I'll try to research this on my own, but if you know an easy way for the script to process multiple files (like all the .jpg files in a given folder) and could share that, I'd be very appreciative. I'm using Ubuntu linux, BTW.

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

Re: High Pass filter for a batch?

Post by fmw42 »

One would have to script that in a loop over the files in a directory. I will see what I can do with that for you. Do you need to process only jpg or any image? Do you have files with spaces in the name? Do you want to overwrite the images in the directory (not a good idea) or change the name and write them to the same directory or write them to another empty directory with the same name or with a modified name?


I also want to convert this into a script for a given image so that one can easily vary the parameters.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: High Pass filter for a batch?

Post by anthony »

fmw42 wrote:I have determined from some testing that the PS high pass is equivalent to the image minus an IM -blur radiusxsigma with radius=0 and sigma=photoshop radius at 50% bias (in non-HDRI)

Here is my IM high pass processing command and result:

Code: Select all

infile="birdofparadise.jpg"
blur=60
convert $infile \
\( -clone 0 -blur 0x60 \) \
\( -clone 0 -clone 1 +swap -compose mathematics \
-set option:compose:args "0,1,-1,0.5" -composite \) \
-delete 0,1 birdofparadise_im_hp${blur}.jpg
Hmmm sounds like a job for the new morphology convolutions :-)

Code: Select all

convert birdofparadise.jpg -set option:convolve:scale -100,100% -bias 50% \
           -morphology convolve Gaussian:0x3  birdofparadise_morph.jpg
And that can be used with mogrify as it does not require the handling multiple images to produce a result.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
philipmorg

Re: High Pass filter for a batch?

Post by philipmorg »

I would like to be able to process a folder of either TIF or JPG images. I would ordinarily not have spaces in the file name. I think I'd want the processed files to end up in a subdirectory of the original directory (something like ./processed) with the original name + "edited."

Thanks,
--Philip.
fmw42 wrote:One would have to script that in a loop over the files in a directory. I will see what I can do with that for you. Do you need to process only jpg or any image? Do you have files with spaces in the name? Do you want to overwrite the images in the directory (not a good idea) or change the name and write them to the same directory or write them to another empty directory with the same name or with a modified name?


I also want to convert this into a script for a given image so that one can easily vary the parameters.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: High Pass filter for a batch?

Post by fmw42 »

I have made the processing into a script called enrich on my web site below. It processes only one image at a time as do all my other scripts.

I will post some code here as soon as I can to loop over a directory as requested above.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: High Pass filter for a batch?

Post by fmw42 »

Anthony wrote:convert birdofparadise.jpg -set option:convolve:scale -100,100% -bias 50% \
-morphology convolve Gaussian:0x3 birdofparadise_morph.jpg
But gaussian is much slower than -blur, especially when he needs sigma=60. So you really need to combine in sequence two morphology convolve blur:0x3 with its 90 degree rotated version. That would be something like

convert birdofparadise.jpg -set option:convolve:scale -100,100% -bias 50% \
-morphology convolve blur:0xsigma,0 -morphology convolve blur:0xsigma,90 birdofparadise_morph.jpg

But it does not duplicate my results or that of Photoshop for sigma=60 and has some severe artifacts. I suspect I am not doing the scaling and bias properly. Anthony, can you correct this?

He also needs two different sizes blurs and two composites, so not really suitable for mogrify. Correct me if I am wrong.
Last edited by fmw42 on 2010-07-06T10:31:18-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: High Pass filter for a batch?

Post by fmw42 »

Philip,

I have made a script to loop over a directory of images. You can get it at http://www.fmwconcepts.com/misc_tests/hpbatch

It may download as hpbatch or hpbatch.sh

You can remove the .sh and run it as --- bash fullpathto/hpbatch

or leave the .sh and run it as --- bash fullpathto/hpbatch.sh

Either way, you need to edit a couple of parameters at the top --- provide your actual full path to your image directory and provide your actual full path to IM convert.

It will create a subdirectory (processed) in your image directory if it does not already exist. It will also replace spaces in filenames with underscores in the output directory. It will process files that have suffixes of jpg, tif or tiff (or with all caps) only.

Let me know if you have any problems. If it does not run, be sure it has executable permissions (chmod u+x hpbatch).

Fred
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: High Pass filter for a batch?

Post by anthony »

Thinking about this problem last night a scaling/unity addition of -100,100% is actually equivelent to a DoG (Difference of Gaussians) where the positive gaussian has a sigma of '0'
That is unity - gaussian

As such...

Code: Select all

convert birdofparadise.jpg -set option:convolve:scale -100,100% -bias 50% \
           -morphology convolve Gaussian:0x3  birdofparadise_morph.jpg
is equivelent to

Code: Select all

convert birdofparadise.jpg  -bias 50% \
           -morphology convolve DoG:0x0,3  birdofparadise_dog.jpg
Note the order of the two sigma arguments. '0' is unity, '3' is the subtracted gaussian.

Of course as Fred points out...
fmw42 wrote:But gaussian is much slower than -blur, especially when he needs sigma=60.
At large sigmas any form of 2-dimensional gaussian convolution is orders of magnitude slower than using 2, 1-dimentional Blurs (whcih the -blur operator provides).
See Gaussian vs Blur Kernels
fmw42 wrote: So you really need to combine in sequence two morphology convolve blur:0x3 with its 90 degree rotated version. That would be something like

Code: Select all

convert birdofparadise.jpg -set option:convolve:scale -100,100% -bias 50% \
           -morphology convolve blur:0xsigma,0 -morphology convolve blur:0xsigma,90  birdofparadise_morph.jpg
But it does not duplicate my results or that of Photoshop for sigma=60 and has some severe artifacts. I suspect I am not doing the scaling and bias properly. Anthony, can you correct this?
First

Code: Select all

-morphology convolve blur:0xsigma,0 -morphology convolve blur:0xsigma,90
using advanced multi-kernel features of morphology is equivalent to

Code: Select all

-morphology convolve blur:0xsigma\>
Second BOTH the bias and the unity (original image) will be added to twice!!!! The result is as you found a usless image!

However I have had some minor success with using a 50% unity addition.

Code: Select all

   convert birdofparadise.jpg -set option:convolve:scale -100,50% -bias 50% \
              -morphology convolve blur:0xsigma\>    birdofparadise_blur_trial.jpg 
The real problem is that a DoG kernel (what you are really doing) is NOT separatable. You can not use a 2-pass 'Difference of Blurs' to generate a DoG (I tried). The one example I generated of doing a DoG using blurs is shown as the last example of the introduction of the DoG kernel as is basically what you were doing initially using multiple images.

The only other solutions I can think of (using multi-kernel minus composition) would require HDRI versions of IM to prevent clipping of values between images, and even then Dog Separability problem remains a problem :-(
He also needs two different sizes blurs and two composites, so not really suitable for mogrify. Correct me if I am wrong.
You are not wrong. As soon as multiple image processing becomes involved, "mogrify" is no longer really suitable.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
philipmorg

Re: High Pass filter for a batch?

Post by philipmorg »

Thanks Fred! It works perfectly.

-P
fmw42 wrote:Philip,

I have made a script to loop over a directory of images. You can get it at http://www.fmwconcepts.com/misc_tests/hpbatch

It may download as hpbatch or hpbatch.sh

You can remove the .sh and run it as --- bash fullpathto/hpbatch

or leave the .sh and run it as --- bash fullpathto/hpbatch.sh

Either way, you need to edit a couple of parameters at the top --- provide your actual full path to your image directory and provide your actual full path to IM convert.

It will create a subdirectory (processed) in your image directory if it does not already exist. It will also replace spaces in filenames with underscores in the output directory. It will process files that have suffixes of jpg, tif or tiff (or with all caps) only.

Let me know if you have any problems. If it does not run, be sure it has executable permissions (chmod u+x hpbatch).

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

Re: High Pass filter for a batch?

Post by fmw42 »

Happy to help and that it worked OK.
Post Reply