Replace chroma key by image

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
bbtrash
Posts: 4
Joined: 2013-07-09T04:08:27-07:00
Authentication code: 6789

Replace chroma key by image

Post by bbtrash »

Hi everyone !
I'm solving following task: How to add (post production) some information on image. We have 5 images of product from different angles (see example below). We need add manufacturer logo to every image dynamically.
My solution is: add rectangle with chroma key color on the wall. Use ImageMagic to locate chroma color rectangle on image and replace it by choosen image. Its possible to do that ?
OR : make two points determining rectangle on wall, locate points positions, fill virtual rectangle by choosen image.

I studied ImageMagic wiki, but i'll be really gratefull for some practical/working example

Here is an image with chroma key rectangle which i want replace.
Image

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

Re: Replace chroma key by image

Post by fmw42 »

You can create the same cromakey color patch (same size and color) as an image and use the compare function to find the location of the patch in your image. Once you have to coordinates, you can composite your small image there.

see

http://www.imagemagick.org/Usage/compare/
http://www.imagemagick.org/Usage/compare/#statistics
http://www.imagemagick.org/script/compare.php

An example is at viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076, but you now need to add -subimage-search to the command.

For compositing, see
http://www.imagemagick.org/Usage/compose/#compose
http://www.imagemagick.org/Usage/layers/#convert

convert backgroundimage smallimage -compose over -composite result
bbtrash
Posts: 4
Joined: 2013-07-09T04:08:27-07:00
Authentication code: 6789

Re: Replace chroma key by image

Post by bbtrash »

Hi.
First I would like say thank you to user fmw42 for his reply to my problem. I tried suggested method but with weird result.
My images :
Image
Image
Image
My color path (cant be same size - different angles)
Image

Lets use compare command

Code: Select all

compare -metric rmse -subimage-search 1b.jpg blueSample.jpg 1b_similarity.jpg
Result:
Image
Image

And please whats next step? How to merge function compare and convert together ?


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

Re: Replace chroma key by image

Post by fmw42 »

And please whats next step? How to merge function compare and convert together ?
They really cannot be merged. You can pipe the results from convert to compare, but this is really not much different that using two separate commands. It just allow you to avoid saving an image.


If your patch is not the same size for each image, then you need to use different size patches for each image, or use the smallest patch size from all the images (assuming it is reasonably large enough for all images). If you can be assured that the color of the patch is unique for all images -- there is no near color in any image -- then you can even use a 1 pixel patch. Then optionally in compare add -similarity-threshold 0 for -metric rmse and it will stop at the first pixel that is a perfect match (for versions 6.8.3-10 or higher). Get the coordinates from the compare and use those to composite your text image at that coordinate.

Here is a simple example:

Input image has red pixel near the center:
Image

Create text image:
convert -size 20x -background white -fill black label:"A" 1A.png
Image

Do compare, get coordinates as variable in form x+y
coords=`compare -metric rmse -subimage-search -similarity-threshold 0 cyclops1.png -size 1x1 xc:red null: 2>&1 |\
cut -d\ -f4 | tr "," "+"`

echo $coords
49+49

Composite the text image over the background image
convert cyclops1.png 1A.png -geometry "+$coords" -compose over -composite cyclops1_label.png
Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Replace chroma key by image

Post by snibgo »

I'll add another thought here.

Perhaps the OP wants the logo to appear on the photo, not only in the correct location, but also with the correct perspective. This would involve (a) finding the corners of the blue patch on each image, then (b) applying "-distort perspective" to the logo.

Step (b) is easy.

Step (a) is harder. The patch is an arbitrary quadrilateral, with sides roughly parallel to the image sides. I can't see an easy way to do this. I can see a hard way: rotate the image by 45 degrees, find the top-most blue pixel, and transform that coordinate back by -45 degrees. Repeat for 135, 225 and 315 degrees.
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: Replace chroma key by image

Post by fmw42 »

snibgo wrote: (a) finding the corners of the blue patch on each image
One can analyze the second output image from the compare if -similarity-threshold is removed. Since one will get a perfect match, the result will be the rectangle or quadrilateral with pure white in the image.

You can get the idea from viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076

where I was searching for a match to the eye of the mandril image. The bright white spot is where the eyes are located. A similar process would show a pure white quadrilateral for a match to a single pixel of the color patch. That region can then be located and its corners found by exporting the image as text and grep for "white". That will give a list of coordinates that can searched for the bounds.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Replace chroma key by image

Post by snibgo »

Export as text, and search for the corners ("most north-westerly point", etc) in a script. Yes, that looks like a sensible method. Thanks.
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: Replace chroma key by image

Post by fmw42 »

You can find those corners or the quadrilateral also by cropping out the white region from the second compare result image, thresholding to binary, then doing a -distort depolar to convert from cartesian to polar coordinates. The corners should stand out as major peaks.
bbtrash
Posts: 4
Joined: 2013-07-09T04:08:27-07:00
Authentication code: 6789

Re: Replace chroma key by image

Post by bbtrash »

snibgo wrote:I'll add another thought here.
Perhaps the OP wants the logo to appear on the photo, not only in the correct location, but also with the correct perspective. This would involve (a) finding the corners of the blue patch on each image, then (b) applying "-distort perspective" to the logo.
Hi, yes this is exactly what i need.
You can find those corners or the quadrilateral also by cropping out the white region from the second compare result image, thresholding to binary, then doing a -distort depolar to convert from cartesian to polar coordinates. The corners should stand out as major peaks.
Its possible add some examples or commands how i can do that ?

Thanks a lot for your help.
bbtrash
Posts: 4
Joined: 2013-07-09T04:08:27-07:00
Authentication code: 6789

Re: Replace chroma key by image

Post by bbtrash »

bump

I still researching how use IM to solve problem, but no results :(
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace chroma key by image

Post by fmw42 »

You can trim the image to the bounding box of the blue rectangle and get its size and offset. From there you can compute the 4 corners. This will do it in unix. But note your blue box is not a solid color so I had to fine tune the -fuzz to get just the box. It would be better if the blue box was a solid constant color.

This will display the result of the trim and print its widthxheight and offset from the top left corner of the original image.

convert mrxn.jpg -fuzz 9.5% +transparent "#8385fe" -trim -write show: -format "dimension=%wx%h offsets=%O" info:

dimension=36x27 offsets=+667+157
Post Reply