This project has moved. For the latest updates, please go here.

Stuck Porting Script

May 16, 2014 at 5:25 PM
I'm porting Fred's maxima script (http://www.fmwconcepts.com/imagemagick/maxima/index.php) because I can't figure out this part:
match=`compare -metric $metric $subimagesearch $dir/tmpI.mpc \( -size 1x1 xc:"gray($valpct%)" \) null: 2>&1`
I think I understand that I can do img.SubImageSearch() and that works fine but it only returns the best match. I really need all matches and their confidence values in a collection that I can iterate through. I have found nothing to do this so this is why I resort to maxima.

I've done this to create the gray color with the value percent. The problem is that img.SubImageSearch() doesn't seem to return a percent. There is a SimilarityMetric but that doesn't look right.
MagickColor graylevel = new MagickColor("xc:gray(" + percent + ")");
MagickImage gray1x1 = new MagickImage(graylevel,1,1);
Fred's routine outputs data like this
DSCHROEDER /d/apps/ImageMagick-6.8.8-Q16
$ ./maxima -r 3 -n 5 -u p -m rmse -g -t 90 hat.jpg hat_out5.png
787,72 gray=60395,235,92.1569%
787,42 gray=59624,232,90.9804%
818,42 gray=59110,230,90.1961%
where the first value is X,Y and gray is listed in 3 types.

Perhaps the real question is how to use these xc: -format "%[]" info: commands in Magick.NET.
val=`convert $dir/tmpI.mpc -format "%[$maximum]" info:`
valfrac=`convert xc: -format "%[fx:$val/$qrange]" info:`
valpct=`convert xc: -format "%[fx:100*$val/$qrange]" info:`
val8=`convert xc: -format "%[fx:round(255*$valfrac)]" info:`
Once I can get the value output from the format info commands the perpaps i can pass in the gray percent to make my color to do a subimage search.

Darren
Coordinator
May 17, 2014 at 6:05 PM
You can use the FormatExpression method of MagickImage for this.
using(MagickImage image  = new MagickImage(@"c:\yourdir\tmpI.mpc"))
{
  string val = image.FormatExpression("%[" +maximum + "]");
}
Coordinator
May 19, 2014 at 7:41 AM
And if you want to use that script commercially please contact Fred. Even if you port the script you still have to pay a license fee if you want to use his script commercially.
May 19, 2014 at 1:58 PM
Thanks for the tip. His script is too slow to use commercially. There needs to be a better way to find this stuff with one pass. Clearly IM knows how to do it because the SimilarityImage highlights the areas that are similar, all at one time. I just wish it would return a collection so I could iterate through each item and check the confidence score and look at the rectangle. It's just silly that once SubImageSearch returns an image you have to read the resulting image in again and analyze it. Wasteful.
May 19, 2014 at 2:23 PM
Edited May 19, 2014 at 2:55 PM
FormatExpression is working great. Thanks for pointing that out.

I noticed that defining gray colors with a percent doesn't seem to work, or at least I'm doing it wrong. I read here http://www.imagemagick.org/script/color.php that I could do something like this:
gray(50%)        mid gray
graya(50%, 0.5)  semi-transparent mid gray
When I try this:
MagickColor graylevel = new MagickColor("xc:gray(" +valpct + "%)");
I get
An unhandled exception of type 'System.ArgumentException' occurred in Magick.NET-AnyCPU.dll
Additional information: Value 'xc:gray(94.902%)' should start with '#'.
I'm sure I'm messing the syntax up somewhere since none of the constructors of MagickColor take the syntax I provided so I assume there's another way to define the color. Any ideas?

This is all I can come up with
string qrange = simImg.FormatExpression("%[fx:quantumrange]");
string val = simImg.FormatExpression("%[max]");
string valfrac = simImg.FormatExpression("%[fx:" + val + "/" + qrange + "]");
string valpct = simImg.FormatExpression("%[fx:100*" +val + "/" + qrange + "]");
string val8 = simImg.FormatExpression("%[fx:round(255*" +valfrac + ")]");
byte val8b = Byte.Parse(val8);
MagickColor graylevel = new MagickColor(val8b, val8b, val8b);
Coordinator
May 19, 2014 at 3:12 PM
That is a check that was added by me. I will see if I can add support for the other color syntax.
Coordinator
May 19, 2014 at 9:57 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.