[HELP] Autocrop batch on photo ID

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?".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Autocrop batch on photo ID

Post by fmw42 »

Sorry I do not use Windows and so cannot help further. You would need to hear back from one of the Windows users.
newbie1
Posts: 31
Joined: 2012-05-29T07:31:08-07:00
Authentication code: 13

Re: [HELP] Autocrop batch on photo ID

Post by newbie1 »

Any windows user that can help ?? :D
newbie1
Posts: 31
Joined: 2012-05-29T07:31:08-07:00
Authentication code: 13

Re: [HELP] Autocrop batch on photo ID

Post by newbie1 »

I have managed to let my program code looping by using function to call it back again but the problem is that it keeps on running. Is there any solution to let the program run again if there is an additional photo being added to the folder.

Code: Select all

@echo off

cd C:\Users\user\Desktop\New\New folder 


call:menu


:menu
convert *.jpg -fuzz 10%% -trim -write %%02d_trim.png -scale "x1!" txt:
convert *_trim.png -crop 209x329+0+0 -fuzz 10%% -trim +repage %%02d_left.png
convert *_trim.png -crop 173x336+227+0 -fuzz 10%% -trim +repage %%02d_right.png

goto:menu

Help Needed !! :)
newbie1
Posts: 31
Joined: 2012-05-29T07:31:08-07:00
Authentication code: 13

Re: [HELP] Autocrop batch on photo ID

Post by newbie1 »

Hi, im back with new problems regarding auto crop. haha

First of all, let me thanks those who have help me in the past.

Now i am using Ubuntu(Linux).

From the previous problem, i had a picture with the person face and ID. Then i had to crop out the person face and ID out into 2 different picture. Example:
Image
Image
Image

Now, i have to crop out the name, admin no. and barcode out of the ID. Cropping them out is easy as i just need to find the size and coordinates and just convert it. But, my problem is that i have 2 different ID, a NEW ID and a OLD ID.
Example:
OLD ID:
Image
NEW ID:
Image

As, you can see the placing of the name, admin no. and barcode are different.

So, is there a way to differentiate this 2 ID? and then do a if-else statement?

eg: check if there white on the top right corner of the ID

Code: Select all

if(no white color on top right corner of ID)
crop name, admin no. and barcode of OLD ID
else
crop name, admin no. and barcode of NEW ID
Is this idea possible?

Thanks in Advance!! :D
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Autocrop batch on photo ID

Post by fmw42 »

Make an image of the word student in the first example and use compare to search for it in both image.

Or just threshold the two images and look at the difference. The color is in different places in your two examples

Or scale the images down to one column. The first will have a more distinct light pattern in the middle. The second will not.
newbie1
Posts: 31
Joined: 2012-05-29T07:31:08-07:00
Authentication code: 13

Re: [HELP] Autocrop batch on photo ID

Post by newbie1 »

How do i do all that ?
Sorry for bothering but im still new to these kind of programming.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Autocrop batch on photo ID

Post by fmw42 »

newbie1 wrote:How do i do all that ?
Sorry for bothering but im still new to these kind of programming.

To use compare, see this example but add -subimage-search

viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076

The small image should be your create image of the word "Student" or cut it out of the existing image. This works only if the scale of all scanned images is the same.

For resizing to one column

convert image -scale 1x\! result

the \ is an escape for unix. If on windows, put "1x!" in quotes or 1x^!, though I am not sure of the windows syntax. So see http://www.imagemagick.org/Usage/windows/
newbie1
Posts: 31
Joined: 2012-05-29T07:31:08-07:00
Authentication code: 13

Re: [HELP] Autocrop batch on photo ID

Post by newbie1 »

I did what you say, and i got the result.

I crop out the word "STUDENT", student.jpg:
Image

My oldID.jpg:
Image

My newID.jpg:
Image

So, i tired out the command:

Code: Select all

compare -subimage-search  -metric rmse oldID.jpg student.jpg result.jpg
and i got this result:

Code: Select all

284.409 (0.00433981) @ 132,130
Image
Image

When i tried it out with my newID, i also got this result:

Code: Select all

compare -subimage-search  -metric rmse newID.jpg student.jpg result.jpg

Code: Select all

2596.47 (0.0396196) @ 25,254
Image
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Autocrop batch on photo ID

Post by fmw42 »

That is correct. But look at the rmse error values. In the first case it is (0.00433981) which is the error in the range of 0 to 1. It is very small and so a good match. In the second case, it is (0.0396196). That is 10 times larger. So you need to do some testing for a good threshold to select whether it is a match or not. The second match is only 10x larger because the letters in student are too close to the same colors as in the second images bottom portion.
newbie1
Posts: 31
Joined: 2012-05-29T07:31:08-07:00
Authentication code: 13

Re: [HELP] Autocrop batch on photo ID

Post by newbie1 »

Well, i tried making the letters student in black and this is what i came up with:
Image

The result with the old ID is :

Code: Select all

1334.25 (0.0203593) @ 132,130
so the error is only 0.02

But the result with the new ID is:

Code: Select all

compare.exe: images too dissimilar `NEWID.jpg' @ error/compare.c/CompareImageCommand/953.
Is this a good thing or a bad thing to happen ?

and what do you mean by testing for a good threshold to select whether it is a match or not.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Autocrop batch on photo ID

Post by fmw42 »

Making the letters black will only make it worse. You want the small image (template) to be as close as possible to what you have in the one image. That would occur when it is the same color. Unfortunately, the second image has red in it, so it finds a match, but it is not as good as the match with the first image.

When the letters are black, it won't match the other image very well and that is why you get the message. To force it to finish add -dissimilarity-threshold 1 or 100%. But you will then find that the error is going to be much larger and thus worse.

As I said before, the fact that the letters are red and the second image has a similar red in it makes the difference in the error values closer than one would like. But there is still a factor of 10 difference which should be enough.

You have to test on a number of each types of photos and get some average value for both. Then pick a threshold in between. Then when you have to first image, the error value will be less than the threshold. And when you have the second type of image, it will have an error value greater than your threshold value. You will have to extract the error value from the returned text and then compare to your threshold value to determine which image it finds.
newbie1
Posts: 31
Joined: 2012-05-29T07:31:08-07:00
Authentication code: 13

Re: [HELP] Autocrop batch on photo ID

Post by newbie1 »

When the photo is taken, the position where the ID card is taken is fixed.
Therefore, im sure that the error value stay the same.

As you was saying, i need to pick a threshold value that is between the value of the both error.
Since the 1st error = 0.00433981, 2nd error = 0.0396196
Lets say, i pick the threshold value = 0.009

So, how do i extract the error value from the returned text and then compare to my threshold value to determine which image it finds.

I know what you meant by this :

Code: Select all

extract the error
if(error>0.009)
card==NewID
else
card==OldID
But what is the actual code that im suppose to write?
Sorry for all these question as im really new into this kind of programming
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Autocrop batch on photo ID

Post by fmw42 »

OK. You can choose whatever value you think works. But half way between the two is .022. I am not sure that is best, but again you can decide from further tests.

In unix, you compute a variable by filtering the output of compare. You don't even have to save the image results, which I why I used null:. But you have to send the result from stderr to stdout (via 2>&1) and pipe it to sed to filter the number in parenthesis as the only value to keep.

value=`compare -metric rmse -subimage-search largeimage smallimage null: 2>&1 | sed -n 's/^.*(\(.*\)).*$/\1/p'`
test=`convert xc: -format "%[fx:$value<$threshold?1:0]" info:`
if [ $test -eq 1 ]; then
#found first image
else
#found second image
fi


Sorry I do not know how to do that in windows. So see http://www.imagemagick.org/Usage/windows/ for hints.
Last edited by fmw42 on 2012-08-12T19:08:57-07:00, edited 2 times in total.
newbie1
Posts: 31
Joined: 2012-05-29T07:31:08-07:00
Authentication code: 13

Re: [HELP] Autocrop batch on photo ID

Post by newbie1 »

I am using Ubuntu now.

When i try the script it not working:

Code: Select all

#!/bin/sh
value=`compare -metric rmse -subimage-search 2.jpg 1.jpg null: 2>&1 | sed -n 's/^.*(\(.*\)).*$/\1/p'`
test=`convert xc: -format "%[fx:$value<$0.022?1:0]" info:1
if [ $test -eq 1]; then
convert 4.jpg oldid.jpg
else
convert 4.jpg newid.jpg
fi
When i run it in terminal, it just stop there:
Image

Am i writing it the wrong way?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [HELP] Autocrop batch on photo ID

Post by fmw42 »

#!/bin/sh
value=`compare -metric rmse -subimage-search 2.jpg 1.jpg null: 2>&1 | sed -n 's/^.*(\(.*\)).*$/\1/p'`
test=`convert xc: -format "%[fx:$value<$0.022?1:0]" info:1
if [ $test -eq 1]; then
convert 4.jpg oldid.jpg
else
convert 4.jpg newid.jpg
fi
You mistyped 1 -- it should be a backquote to match what is left of convert
Post Reply