equalizeImage or -equalize

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

equalizeImage or -equalize

Post by mjamal »

Hello Team,

Can anyone please suggest on Imagick equalizeImage function or image magick -equalize. I have used both the methods but it did not giving me 100% same result as photoshop "image/adjustments/equalize" effect.

Please let me know what I need to do for making same result.

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

Re: equalizeImage or -equalize

Post by fmw42 »

We do not know exactly what Photoshop does. ImageMagick allows you to do an -equalize on each channel separately.

Code: Select all

convert image -channel rgb -equalize result
or all channels together

Code: Select all

convert image -equalize result
You will have to check those against Photoshop.

Imagick does not seem to have the equivalent of the first method, only the second. See http://us3.php.net/manual/en/imagick.equalizeimage.php.

However, you can separate channels, then equalizeImage on each channel separately, then recombine the separated channels back into an RGB image.
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: equalizeImage or -equalize

Post by mjamal »

Hello Fred,

We have checked both the mentioned processes but didn't got the exact result as photoshop. May be we are not following you correctly.
Can you please advice about separating the channels and provide us few code examples for this.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: equalizeImage or -equalize

Post by snibgo »

Equalizing is complex arithmetically, and rounding may produce slightly different results.

@mjamal: I suggest you post a link to your input image, and the output from Photoshop, and state any settings you have used. Then we can try to understand differences between PS and IM.
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: equalizeImage or -equalize

Post by fmw42 »

Here is a test example using Photoshop and ImageMagick.

Input:
Image

Photoshop equalize:
Image


ImageMagick equalize channels together:

Code: Select all

convert boats3.png -equalize boats3_IM_equalize.png
Image


ImageMagick equalize channels separately:

Code: Select all

convert boats3.png -channel rgb -equalize boats3_IM_equalize_rgb.png
Image




As you can see the IM equalize channels together if very close to that of Photoshop. There is slight difference in brightness.

As user snigbo has said, there may be difference in the approach and the histogram number of gray levels as well as precision. ImageMagick use floating point precision. I believe Photoshop is only 8-bit precise, but I could be wrong.

I believe there are two approaches to histogram equalization. The first is to shift colors to different bins to make the bins nearly equal in count. The other uses a look up table generated from the cumulative histogram. The number of bins in the latter makes a difference.
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: equalizeImage or -equalize

Post by mjamal »

In addition to Fred's input, I am sending my test inputs/results

1) Source image
source.jpg
source.jpg (136.25 KiB) Viewed 82398 times
2) Photoshop Equalize result on above image
photoshop.jpg
photoshop.jpg (298.85 KiB) Viewed 82398 times
3) IM equalize result on source image
IM-equalize.jpg
IM-equalize.jpg (287.01 KiB) Viewed 82398 times
Few additional observations/results
1) When we applied Brightness 16 (From Image >> Adjustment >> Brightness/Contrast in photoshop) to the IM result image in #3 above, we got exactly same result as photoshop result in #2.
2) In IM (#3), we added additional step for brightness 16 using HALD CLUT image (after applying brighness 16 to HALD16 image), we got exactly same result as photoshop in #2
3) In IM (#3), we tried to add brightness 16 using IM function, we didn't get the same result as photoshop #2

So adding brightness 16 using HALD16 image (haldClutImage function), worked and gives us same result as photoshop but with other source images, this trick didn't work. So adding brightness 16 is not a working solution for all the images but may give direction for finding working solution.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: equalizeImage or -equalize

Post by fmw42 »

Have you tried just creating a HALD image from ImageMagick, taking it to Photoshop, applying the PS equalize and save to PNG, then bringing that back to ImageMagick and just applying the processed HALD image with your input image using -hald-clut. Do not do any IM equalize, just use the Photoshop equalized HALD image with your input in IM
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: equalizeImage or -equalize

Post by snibgo »

I don't think equalizing a hald:16 will change much.

@mrjamal: What version of IM did you use?

Please show the exact command you used to make IM-equalize.jpg. It wasn't simply "magick source.jpg -equalize IM-equalize.jpg".
snibgo's IM pages: im.snibgo.com
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: equalizeImage or -equalize

Post by mjamal »

@Fred, I already checked the equalize effect with the HALD 16 and it was not giving any changes for the source file.

@Snibgo - My server's Image Magick version is "6.7.8-9", you can see below screen for this.
imagickPHPINFO.png
imagickPHPINFO.png (49.48 KiB) Viewed 82332 times
Also I have checked with the below couple of codes and both are giving the same output result as we already mentioned above (IM-equalize.jpg).

Code 1:
exec("convert source.jpg -equalize IM-equalize.jpg");

Code 2:
$imagick = new \Imagick(realpath('source.jpg'));
$imagick->equalizeImage();
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();

Please advice us how we can close the result with result from Photoshop.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: equalizeImage or -equalize

Post by snibgo »

v6.7.8-9 is very old. I suggest you upgrade (but I doubt that will make any difference here).
mjamal wrote:exec("convert source.jpg -equalize IM-equalize.jpg");
Your IM-equalize.jpg has an ICC colour profile. Your source.jpg does not. So that command did not make that output from that input.

I can't replicate the Photoshop result in IM. I've varied the number of buckets (eg 32) and the source for the histogram statistics (eg "-grayscale Brightnes"). These make small differences, but something more major is happening.

When you made the Photoshop equalization, I assume you didn't base this on a selected area within the image?
snibgo's IM pages: im.snibgo.com
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: equalizeImage or -equalize

Post by mjamal »

snibgo, did get it about ICC colour profile. We have used the attached source files only to produce the attached results.
Please note, we are PHP developers having basic knowledge about photoshop. We don't have much knowledge about the terms like ICC colour profile, number of buckets, histogram statistics etc.

Also while applying the equalize effect on image, we did it on complete image and not on the selected portion of the image.

One more thing
When we tested the equalize effect on source image in GIMP image processing software, we get the exact result as IM.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: equalizeImage or -equalize

Post by snibgo »

mjamal wrote:Also while applying the equalize effect on image, we did it on complete image and not on the selected portion of the image.
That doesn't quite answer my query. If you have selected a portion, the statistics can be gathered from just that portion then the equalisation applied to just that portion, or applied to the entire image. Is that what you did?
snibgo's IM pages: im.snibgo.com
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: equalizeImage or -equalize

Post by mjamal »

Applied to the entire image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: equalizeImage or -equalize

Post by snibgo »

So, you selected a portion, gathered statistics from that portion, and applied the result to the entire image. Yes?

Then that's the problem. IM gathers statistics from the entire image.

We can make IM gather stats from just a portion, and apply those to the entire image, if we want.
snibgo's IM pages: im.snibgo.com
mjamal
Posts: 62
Joined: 2017-12-27T06:13:59-07:00
Authentication code: 1152

Re: equalizeImage or -equalize

Post by mjamal »

We are using PHP's imagick library equalizeimage function from below link
http://us3.php.net/manual/en/imagick.equalizeimage.php.
Post Reply