Color correct an altered image with the unaltered version.

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
coolperez8
Posts: 29
Joined: 2016-03-11T07:27:11-07:00
Authentication code: 1151

Color correct an altered image with the unaltered version.

Post by coolperez8 »

So, I had an image I wanted my friend to edit. He edited the image and sent it back to me. When I opened it however, I noticed that the colors in the altered image were slightly different than the original even in areas where no edits were made. So, what I want to do is alter the edited image's color curves to make them closer to the original image's color curves by defining one point for each color that is supposed to be the same in both images, and having ImageMagick color correct the edited image to make the color curves look more like the original image. How can I do this?

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

Re: Color correct an altered image with an unaltered image.

Post by snibgo »

What version of IM? On what platform?

If you show your images, advice can be more specific.

The method will depend on how we assume the colours have changed. My Gain and bias page shows a simple method that assumes we can colour-correct by six numbers: a multiplier and addition for each channel. That page mentions some other methods (eg matching histograms) that are shown on other pages.

Gain-and-bias can cause clipping. I've devised another method that never clips, not yet published.
snibgo's IM pages: im.snibgo.com

coolperez8
Posts: 29
Joined: 2016-03-11T07:27:11-07:00
Authentication code: 1151

Re: Color correct an altered image with the unaltered version.

Post by coolperez8 »

Looks like I found an image with the same color differences, it wasn't a result of my friend editing it.
Here is my ImageMagick and System Info:

Distributor ID: LinuxMint
Description: Linux Mint 18.2 Sonya
Release: 18.2
Codename: sonya
Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-05-26 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib

So, here are the images:
Correct Color Info: http://i.imgur.com/0tiEJvQ.png
Altered Color Info: http://i.imgur.com/CpODdS5.png
(Note the wall in the background, such as it being more yellow in the correct version and having more green in the altered version.)

So, what I want to do is find the color curves to get the correct color info image (or as close as possible) from the altered color info so I can apply this to a different image. I also want to test how accurate the curves are by comparing ImageMagick's output when applying the curves to the altered image with the original image, and have it return a value. I am guessing the colors changed as both frames were extracted from a video using FFMPEG two different times.

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

Re: Color correct an altered image with the unaltered version.

Post by fmw42 »

If your friend can edit it again the exact same way, then you can use 3D color lookup tables to map any image to have the same effects applied to it (assuming the operations were not spatially variant, such as blur or sharpen, but only point operations, such as curves, etc). You would create a new HALD image. Then have your friend edit it exactly the same as he did you image and save as PNG or TIF (NOT JPG). Then bring that modified HALD image back to imagemagick. Then in ImageMagick, you can use the modified HALD image to process each of your other images. see http://www.imagemagick.org/Usage/color_mods/#hald-clut

Visible modifications in your images might be associated with one image having a color profile and the other not.

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

Re: Color correct an altered image with the unaltered version.

Post by snibgo »

You have an altered and a correct image. Those have an RMSE difference of 1.29%, so they are quite close.

I used three methods to get a transformation from your altered image to the adjusted image. The methods are aimed at ordinary photographs, rather than flat-colour images like this.

Ideally, we want a result that is RMSE 0% from correct.

Matching histograms creates a result that is 1.15% from correct.

The gain-bias method creates a result that is 1.14% from correct.

A new method (unpublished) creates a result that is 1.09% from correct. The transformation is:

%IM%convert img_altered.png -channel R -sigmoidal-contrast 1.534882305949552217,37.46802552930004282% -evaluate pow 1.032916723495265643 -channel G -sigmoidal-contrast 0.3765163147968605961,38.37666773737035442% -evaluate pow 1.017967888500743046 -channel B -sigmoidal-contrast 0.976562499999999889,32.49165025696888165% -evaluate pow 1.030850761726158415 +channel out.png
snibgo's IM pages: im.snibgo.com

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

Re: Color correct an altered image with the unaltered version.

Post by snibgo »

As Fred suggests, hald-cluts (3D cluts) are more powerful for adjusting images than ordinary cluts. The trick is to put the hald-clut through the same editing process as the original image.

But what if we don't know the editing process? Given before-and-after images, can we derive the equivalent hald-clut? I could never see how.

But I've cracked it. From the "correct" and "altered" versions of the OP cartoon, I've created a hald-clut that transforms the "altered" into "correct", with an RMSE of 0.703%. I suspect this can be improved.

The hald-clut is:
Image

The method is a bit complex. I'm writing it up. In a nutshell: find the most common colours in "altered", and the corresponding colours in "correct". This gives a few entries for a sparse hald clut. We can fill in the other entries with a 3-D Shepard's interpolation (power factor 3), but applying it to the relative, not absolute, hald clut.

Another critical insight: the OP cartoon appears to be flat colours, but the colours are not flat. If we assume they are flat, we can't get close to the correct result.
snibgo's IM pages: im.snibgo.com

Post Reply