Page 1 of 2
Cropping picture and rejoining them
Posted: 2015-11-24T06:40:35-07:00
by mianthefirsts
Hi guys,
i need your help.
I've a picture with size 324x324
and i wanna cut out all 16 numbers with specific Geometry Specifications and set it back together.
The goal is to do this as quick as possible because after that comes OCR.
Right now i crop every single number individually, so 16 times convert -crop (convert 1.png -crop 46x46+0+18 1%d.png) and once (montage -geometry 324x324) So that takes quiet a long time.
Every single number is 46x46
X = 0,88,172,258
Y = 18,105,192,278
I tried with a bit cutting in lines and rows or Tile Cropping.
I think Tile Cropping would be a good solution but i've no idea how.
Best solution would be a two liner.
Can anyone help?
Re: Cropping picture and rejoining them
Posted: 2015-11-24T10:22:38-07:00
by fmw42
What is your IM version and platform? I am not sure I understand fully. Can you explain and show one of the cropped sections.
Are you cropping at the grid lines or the light blue squares? If you can crop to the bounds of all the tiles you want and if they are equally spaced, then you can crop them all in one command. See
http://www.imagemagick.org/Usage/crop/#crop_equal and the @ key.
Re: Cropping picture and rejoining them
Posted: 2015-11-24T10:32:09-07:00
by fmw42
P.S. Are the grid lines in your image real or there to show how you want to crop? Are you not missing one line at the bottom of the row of
1 1 -1 1.
Re: Cropping picture and rejoining them
Posted: 2015-11-24T10:37:54-07:00
by fmw42
Perhaps you can provide the exact commands you use for the above example and post your final output as well.
Re: Cropping picture and rejoining them
Posted: 2015-11-24T10:46:10-07:00
by mianthefirsts
True, i missed one. But i added them via Photoshop. Just to get easier x and y.
And yes, i thoight of equal one too. But there are not equal. So i need extra code to determine where to cut exactly
Thats one example
And thats my code
Code: Select all
os.system("screencapture -x -t png -R 795,270,324,324 %s" % (filename))
os.system("convert %d.png -crop 46x46+0+18 %s" %(i,filename1))
os.system("convert %d.png -crop 46x46+88+18 %s" %(i,filename2))
os.system("convert %d.png -crop 46x46+172+18 %s" %(i,filename3))
os.system("convert %d.png -crop 46x46+258+18 %s" %(i,filename4))
os.system("convert %d.png -crop 46x46+0+105 %s" %(i,filename5))
os.system("convert %d.png -crop 46x46+88+105 %s" %(i,filename6))
os.system("convert %d.png -crop 46x46+172+105 %s" %(i,filename7))
os.system("convert %d.png -crop 46x46+258+105 %s" %(i,filename8))
os.system("convert %d.png -crop 46x46+0+192 %s" %(i,filename9))
os.system("convert %d.png -crop 46x46+88+192 %s" %(i,filename10))
os.system("convert %d.png -crop 46x46+172+192 %s" %(i,filename11))
os.system("convert %d.png -crop 46x46+258+192 %s" %(i,filename12))
os.system("convert %d.png -crop 46x46+0+278 %s" %(i,filename13))
os.system("convert %d.png -crop 46x46+88+278 %s" %(i,filename14))
os.system("convert %d.png -crop 46x46+172+278 %s" %(i,filename15))
os.system("convert %d.png -crop 46x46+258+278 %s" %(i,filename16))
Re: Cropping picture and rejoining them
Posted: 2015-11-24T11:23:53-07:00
by fmw42
Can you post your input image (%d.png)? Why not just crop each character's bounding blue box first, then shave off the tops?
Re: Cropping picture and rejoining them
Posted: 2015-11-24T11:28:43-07:00
by mianthefirsts
My input image is that first picture i posted, just not that original.
Here is one
http://i.imgur.com/5hQb0To.png
What do you mean with crop each caracter?
Would not that mean i would have 16 individual commands?
Re: Cropping picture and rejoining them
Posted: 2015-11-24T12:33:46-07:00
by fmw42
try these two cropping commands (unix syntax)
Input:
Code: Select all
convert 5hQb0To.png -trim +repage -crop 4x4@ +repage \
-fuzz 30% -trim +repage tmp1_%d.png
Code: Select all
convert 5hQb0To.png -trim +repage -crop 4x4@ +repage \
-fuzz 5% -trim +repage -gravity north -chop 0x15 tmp2_%d.png
On Unix, you can also pipe them to montage. For example
Code: Select all
convert 5hQb0To.png -trim +repage -crop 4x4@ +repage \
-fuzz 30% -trim +repage miff:- |\
montage - -tile 4x4 -geometry +5+5 -background "#00809F" result1.png
Code: Select all
convert 5hQb0To.png -trim +repage -crop 4x4@ +repage \
-fuzz 5% -trim +repage -gravity north -chop 0x15 miff:- |\
montage - -tile 4x4 -geometry +0+0 -background "#00809F" result2.png
Code: Select all
convert 5hQb0To.png -trim +repage -crop 4x4@ +repage \
-fuzz 30% -trim +repage -background "#00809F" -gravity center -extent 46x46 miff:- |\
montage - -tile 4x4 -geometry +0+0 result3.png
Code: Select all
convert 5hQb0To.png -trim +repage -crop 4x4@ +repage \
-fuzz 30% -trim +repage \
-fuzz 40% -fill white -opaque "#B1EEFC" -fill black +opaque white \
-background black -gravity center -extent 46x46 miff:- |\
montage - -tile 4x4 -geometry +0+0 result4.png
Re: Cropping picture and rejoining them
Posted: 2015-11-24T13:17:04-07:00
by mianthefirsts
Wow! Nice Work! Thank you so much!
That's actually the solution. It speeds up my total execution time.
If you wanna have a challenge.
Another problem is with screenshots like that
http://imgur.com/QB6IUau
http://imgur.com/r2G05kj
And your commands will execute like these
http://imgur.com/4UMXeZC
http://imgur.com/93K66Bg
So if i run OCR over i, it results in
Code: Select all
6 -2 4 3
1 5- -2 4
8 2 9 6
-2 3 9 4
an more weird symbols.
I've written an Algorithm that removes them, but it slows it down.
So, if you find a solution for that with ImageMagick you're genius.
Thanks again for that solution above!
Re: Cropping picture and rejoining them
Posted: 2015-11-24T13:32:10-07:00
by fmw42
try this:
Input:
Code: Select all
convert r2G05kj.png -trim +repage \
-fuzz 40% -fill white -opaque "#B1EEFC" -fill black +opaque white \
-crop 4x4@ +repage \
-trim +repage \
-background black -gravity center -extent 46x46 miff:- |\
montage - -tile 4x4 -geometry +0+0 result5.png
Re: Cropping picture and rejoining them
Posted: 2015-11-24T13:39:27-07:00
by fmw42
I am not sure why you have to crop them? You can do just the following:
Code: Select all
convert r2G05kj.png -trim +repage \
-fuzz 35% -fill white -opaque "#B1EEFC" -fill black +opaque white \
result6.png
Re: Cropping picture and rejoining them
Posted: 2015-11-25T04:41:03-07:00
by mianthefirsts
You're right. Didn't know you could do that so easily.
Actually i was able to cut down CPU Time from 10 to und 1 deciseconds.
Problem is still, there's room for error.
Like on these picture's:
OCR will produce something like this:
http://imgur.com/71ycHJM
http://imgur.com/8TXrHLl
2 9 1 7
-1 2 5 7
0
1 7 2 9
7 -3 3 -2
That's why i wanted to crop it. To eliminate those errors.
Any ideas?
Re: Cropping picture and rejoining them
Posted: 2015-11-25T10:53:42-07:00
by fmw42
try using a smaller fuzz value and optionally dilating the characters using -morphology
Input:
Code: Select all
convert 71ycHJM.png -trim +repage \
-fuzz 20% -fill white -opaque "#B1EEFC" -fill black +opaque white \
result7.png
Code: Select all
convert 71ycHJM.png -trim +repage \
-fuzz 20% -fill white -opaque "#B1EEFC" -fill black +opaque white \
-morphology dilate diamond:1 \
result8.png
Re: Cropping picture and rejoining them
Posted: 2015-11-27T03:04:42-07:00
by mianthefirsts
Good idea!
I see what you doing there, but i've a problem when the colors are changing like
http://imgur.com/a/tzCPb
so i end up having a picture like this
That example was made with 20% fuzz and following command:
Code: Select all
convert 50png -trim +repage -fuzz 20% -fill white -opaque '#B1EEFC' -fill black +opaque white -morphology dilate diamond:1 -quiet 50out.png
I know i can get the color of a single pixel in a picture.
Would that help me to change this light blue color to a light pink color?
I could increase 'fuzz', but then the chance the disturbing elements would appear again.
EDIT:
so i figuerd follow fuzz values to the according colors coming up
purple, turquoise, light and dark blue 20% to 25%
orange 40%
red,pink 35%
green, i think 30% something
Re: Cropping picture and rejoining them
Posted: 2015-11-27T10:55:04-07:00
by fmw42
You need to change the -opaque '#B1EEFC' to the color of a pixel in the numbers. You can change the fuzz value to suit.