Modify subimage with reference 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?".
Locked
vaix
Posts: 5
Joined: 2012-10-08T03:00:43-07:00
Authentication code: 67789
Location: $indonesia =~ /Bali Island is in Indonesia Country :) /gi;
Contact:

Modify subimage with reference image

Post by vaix »

Hi all,

I was confuse how to edit subimage (small image) inside the big image.
i have 3 image.
big.png
Image

start.png
Image

stop.png
Image

i'm using compare -metric rmse -subimage-search big.png start.png
but still dont know how then ..

my objective need change color start to red and stop to green in the big image.

Please help and thanks.

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

Re: Modify subimage with reference image

Post by fmw42 »

You can locate the top left coordinate of a small image in a large image using compare as follows.

compare -metric rmse -subimage-search largeimage smallimage similarityimages

where similarityimages will be either two image -0,-1 or two frames of an image depending upon the format suffix you use.

-subimage-search must now (added at IM v6.6.3-6) be added to allow compare to know the two images are no the same size.

The second of the two output images will be the match score image showing you as the very brightest points where it matches best. You can extract all the matches (not just the best one that is reported to the terminal) using my script maxima at the link below

see
http://www.imagemagick.org/script/compare.php
http://www.imagemagick.org/Usage/compare/
http://www.imagemagick.org/script/comma ... age-search
viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076


You will have to run compare twice, once for the start image and once for the stop image and located with start and stop points you want to use via my script maxima

vaix
Posts: 5
Joined: 2012-10-08T03:00:43-07:00
Authentication code: 67789
Location: $indonesia =~ /Bali Island is in Indonesia Country :) /gi;
Contact:

Re: Modify subimage with reference image

Post by vaix »

Hi Fred/fmw42,

Thanks and apreciate for for answer and help. i have download your maxima script

Let say we work just for search start.png in big.png and replace color to red of start.png ( i dont know it 'll using change color of start.png using imagemagick function or embed/drop another image similart start.png with different color, but i alreay make it start_red.png
Source png fle
-----------------
big.png
Image
start.png
Image
start_red.png
Image

Expected Result ( i modify manual using photoshop )
-------------------
bigstart_result.png
Image

Step
----------------
1. compare -metric rmse -subimage-search big.png start.png bigstart.png
Result :
bigstart-0.png
Image
bigstart-1.png
Image
2. maxima -r 5 -n 10 -t 50 -d 15 bigstart-1.png
Result :
111,3 gray=64372,250,98.2254%
303,46 gray=59840,233,91.31%
143,86 gray=45002,175,68.6687%
3. How then? to process 3 of result of coordinate and make them as my result like :
bigstart_result.png
Image

Thanks for your help, hope it'll usefull for other people also. Thanks Fred, i'm new comer to this forum
Last edited by vaix on 2012-10-08T23:23:59-07:00, edited 3 times in total.

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

Re: Modify subimage with reference image

Post by fmw42 »

It would be very helpful if you edited your post above so that the images show. Do not put HTML tags. Just type the link to your images and then highlight the link and click on the "Img" formatting button above.

My guess is that once you have the coordinates, you need to composite a new small image of the desired color at that location(s) in the large image. You may be able to take the black/white small image and recolor it as you are doing the compositing.

see
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/Usage/color_basics/#replace
http://www.imagemagick.org/Usage/color_ ... vel-colors

vaix
Posts: 5
Joined: 2012-10-08T03:00:43-07:00
Authentication code: 67789
Location: $indonesia =~ /Bali Island is in Indonesia Country :) /gi;
Contact:

Re: Modify subimage with reference image

Post by vaix »

Hi Fred,

Thanks for your help. Finally i got the clue using this step:

:: convert big.png start_red.png -geometry 11x13+111+3 -composite start_red.png -geometry 11x13+303+46 -composite start_red.png -geometry 11x13+143
+86 -composite bigstart_result.png
Result :
bigstart_result.png
Image

is there any simplify convert command above? composite using same/1 reference image and 3 of geometry/coordinate.

and overall process i think i should make script .. and i'm familiar using perl script, i'll share later. or is there any simple command to run step from beginning until compose image?

Thanks ..

vaix
Posts: 5
Joined: 2012-10-08T03:00:43-07:00
Authentication code: 67789
Location: $indonesia =~ /Bali Island is in Indonesia Country :) /gi;
Contact:

Re: Modify subimage with reference image

Post by vaix »

i write script using perl script :

Code: Select all

###################################
# Created by   : vaix             #
# Created date : oct 09, 2012     #
# Usage        : perl cari.pl     #
###################################

$fbig       = 'big';
$fsta       = 'start';
$fsto       = 'stop';
$fsta_red   = 'start_red';
$fsto_green = 'stop_green';
$fext       = '.png';
$small_res  = '12x13';


$cmd1 = "compare -metric rmse -subimage-search $fbig$fext $fsta$fext $fbig$fsta$fext";
$cmd2 = "maxima -r 5 -n 10 -t 40 -d 15 $fbig$fsta"."-1"."$fext";
$cmd3 = "compare -metric rmse -subimage-search $fbig$fext $fsto$fext $fbig$fsto$fext";
$cmd4 = "maxima -r 5 -n 10 -t 40 -d 15 $fbig$fsto"."-1"."$fext";

$ocmd1 = `$cmd1`;
$ocmd2 = `$cmd2`;
$out1   = getgeo($ocmd2,$small_res,"$fsta_red$fext");

$ocmd3 = `$cmd3`;
$ocmd4 = `$cmd4`;
$out2  = getgeo($ocmd4,$small_res,"$fsto_green$fext");

$fcmd  = "convert $fbig$fext $out1 $out2 $fbig"."hasil"."$fext";
#print "$fcmd\n";
$oo   = `$fcmd`;

sub getgeo {

   my($out,$res,$fname) = @_;
   $i=0;
   @has='';
   @data  = split(/\n/,$out);
   $cdata = @data;
   for ($i=0; $i<$cdata; $i++){
        $data[$i] =~ s/\s+//gi;
        ($met,$els) = split('gray',$data[$i]);
        ($x,$y)     = split(',',$met);
         push(@has,"$fname -geometry $res+$x+$y -composite ");
   }
   $has = "@has";
   return $has;
}

Locked