Page 1 of 1
Replace chroma key by image
Posted: 2013-07-09T04:59:27-07:00
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.
Thanks for any advice.
Re: Replace chroma key by image
Posted: 2013-07-09T10:31:26-07:00
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
Re: Replace chroma key by image
Posted: 2013-07-17T01:19:19-07:00
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 :
My color path (cant be same size - different angles)
Lets use compare command
Code: Select all
compare -metric rmse -subimage-search 1b.jpg blueSample.jpg 1b_similarity.jpg
Result:
And please whats next step? How to merge function compare and convert together ?
Many many thanks
Re: Replace chroma key by image
Posted: 2013-07-17T09:30:33-07:00
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:
Create text image:
convert -size 20x -background white -fill black label:"A" 1A.png
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
Re: Replace chroma key by image
Posted: 2013-07-17T11:39:48-07:00
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.
Re: Replace chroma key by image
Posted: 2013-07-17T12:01:25-07:00
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.
Re: Replace chroma key by image
Posted: 2013-07-17T12:13:48-07:00
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.
Re: Replace chroma key by image
Posted: 2013-07-17T14:23:42-07:00
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.
Re: Replace chroma key by image
Posted: 2013-07-21T23:00:25-07:00
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.
Re: Replace chroma key by image
Posted: 2013-08-06T23:03:51-07:00
by bbtrash
bump
I still researching how use IM to solve problem, but no results
Re: Replace chroma key by image
Posted: 2013-08-07T09:03:33-07:00
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