Hi,
I'm relatively new to ImageMagick and trying to integrate it into a script. I've got a black grid that will have many boxes filled in, and then will be scanned by an end-user. The grid has a black border all the way around it, and will be surrounded by whitespace on all sides. I need to take each grid cell and crop it into its own file. Obviously scanners will differ in resolution, and some deskewing may be necessary. I've been trying to use Fred's multi-crop script but I can't get it to work properly. A first iteration would be to pull out the grid squares (minus the black border of each cell) but the ideal implementation would just pull out the content of each cell cropped down as much as possible. Any help or pointers would be truly appreciated.
I have no issues attacking problems with PHP or python if that's the direction to head.
Thanks!
Dave
Auto-crop a grid
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Auto-crop a grid
Post a link to an example image and I will see what I can do with my multicrop script, if appropriate to your image.
You will have to find some free image hosting service and upload your image there. Then put a link to that page here on this forum.
You will have to find some free image hosting service and upload your image there. Then put a link to that page here on this forum.
Re: Auto-crop a grid
Thanks Fred! I really appreciate the help. Here's the image: http://stepaheadtech.com/grid.png
You can see there are two basic box sizes (except for the large one in the bottom right-hand corner) - there are short boxes and tall boxes, each associated with one another (I hope I'm being clear). I don't care about the short boxes - if the contents are pulled out that's fine and I can discard it. If the content is only pulled out of the large boxes, that'd be great.
Thanks,
Dave
You can see there are two basic box sizes (except for the large one in the bottom right-hand corner) - there are short boxes and tall boxes, each associated with one another (I hope I'm being clear). I don't care about the short boxes - if the contents are pulled out that's fine and I can discard it. If the content is only pulled out of the large boxes, that'd be great.
Thanks,
Dave
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Auto-crop a grid
Dave,
Basically, multicrop needs to have a "good" border around every section, that can be distinguished in color from everything inside what you need to extract. By good, I mean thick enough, though perhaps?? one-pixel will do, but generally thicker, provided that there are no similar colors in the image and such that the borders are continuous around each area after using the fuzz value. If the borders have/get gaps in them, then the script cannot isolate the cell. It also needs one to specify a grid that is finer spaced than the size of your smallest cell, so that it can locate each isolated region (insides of the cells).
The other issue is that the black lines are the separators. So you have to trim the image so that there is no white outside the grid lines.
Well after all that "backgound information", a test shows that it can work on this image. It does catch the borders and tick marks, but separates each cell. However, as there is nothing in each cell, I cannot say for sure how it will behave (esp if there is any black in the things you put in the cells). You may have to adjust the background color and/or fuzz value. Note your image is large and has many cells, so it won't be quick.
Let me know how it goes.
So try this on your test image that you provided, if you want all the cells (even the small thin ones)
convert grid.png -trim +repage grid_trim.png
multicrop1 -b black -f 0 -g 1 grid_trim.png grid_trim_crop.png
If you only want the squarish and large cell, try this
convert grid.png -trim +repage grid_trim.png by increasing the grid parameter.
multicrop1 -b black -f 0 -g 3 grid_trim.png grid_trim_crop.png
Note I have uploaded a new version to my web site as I caught a bug. So download it first.
Another improvement that I will try to work on some time is that -g needs really two arguments for x and y spacing. That way it will be more efficient. But I need to do that later. I might also convert -g from percent to actual pixels.
It might work better if you convert all shades of gray to black so that it is black border with white cells. But again, this may be hard in your real case.
Basically, multicrop needs to have a "good" border around every section, that can be distinguished in color from everything inside what you need to extract. By good, I mean thick enough, though perhaps?? one-pixel will do, but generally thicker, provided that there are no similar colors in the image and such that the borders are continuous around each area after using the fuzz value. If the borders have/get gaps in them, then the script cannot isolate the cell. It also needs one to specify a grid that is finer spaced than the size of your smallest cell, so that it can locate each isolated region (insides of the cells).
The other issue is that the black lines are the separators. So you have to trim the image so that there is no white outside the grid lines.
Well after all that "backgound information", a test shows that it can work on this image. It does catch the borders and tick marks, but separates each cell. However, as there is nothing in each cell, I cannot say for sure how it will behave (esp if there is any black in the things you put in the cells). You may have to adjust the background color and/or fuzz value. Note your image is large and has many cells, so it won't be quick.
Let me know how it goes.
So try this on your test image that you provided, if you want all the cells (even the small thin ones)
convert grid.png -trim +repage grid_trim.png
multicrop1 -b black -f 0 -g 1 grid_trim.png grid_trim_crop.png
If you only want the squarish and large cell, try this
convert grid.png -trim +repage grid_trim.png by increasing the grid parameter.
multicrop1 -b black -f 0 -g 3 grid_trim.png grid_trim_crop.png
Note I have uploaded a new version to my web site as I caught a bug. So download it first.
Another improvement that I will try to work on some time is that -g needs really two arguments for x and y spacing. That way it will be more efficient. But I need to do that later. I might also convert -g from percent to actual pixels.
It might work better if you convert all shades of gray to black so that it is black border with white cells. But again, this may be hard in your real case.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Auto-crop a grid
Take a look at the Crop Tiles. Whcih has features for extracting images from a fixed and known grid!
Though if this is scans that grid may not be fixed and may even have skew.
Separating Spaced-out Tiling Images
http://www.imagemagick.org/Usage/crop/#crop_spaced
Though if this is scans that grid may not be fixed and may even have skew.
Separating Spaced-out Tiling Images
http://www.imagemagick.org/Usage/crop/#crop_spaced
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/