Crop the rectangle region from the image using imagemagick - imagemagick

I have an image. I need to crop the rectangle region from the image. This rectangle region is identified by black color border. Inside the rectangle is what I need. Is it possible to crop the black color bordered rectangle region in imagemagick? I know it could be possible using crop command by providing offset(-crop WxH+X+Y) of the rectangle region. But I want to crop the rectangle region without manually measuring the upper left and lower right corners of the rectangle. Is it possible to crop the rectangle region using black color border alone...???

How about something like this?
convert source.jpg -fuzz 10% -bordercolor black -border 1x1 -trim +repage dest.jpg
You may have to play around with the 'fuzz' percentage. The reason you need the fuzz option is that without it trim will only trim pixels that are exactly black - with JPEGs this is unlikely to be the case.
All this is explained on this page: http://www.imagemagick.org/Usage/crop/#trim
This solution will only work if the black border goes right up to the edges of the image. If this is not the case then I don't think you'll be able to do what you need to with IM without examining the image (e.g. pixel by pixel) programmatically.

Related

ImageMagick/etc.: automated removal of unclean edges from scanning?

With a few thousand images like the following one, with complex edges from scanning (first a perfectly regular white one, then a very fuzzy gray/black'ish one), is there a good algorithm to figure out the coordinates of the area-to-keep? In the below example, going clockwise from the north-western corner, the optimal coordinates would be:
121,18
1934,18
1934,1312
121,1312
So that from this input image...
... can be cropped to remove anything tinted red, as shown here:
To clarify, I do know how to use ImageMagick or other tools to crop to that area. What I'm not sure about is how to figure out how to get to those above numbers? Do you just binarize the image and then go left to right until you hit "white" for the first time, then do the same from top to bottom, right to left and bottom to top? Or is there some library, script, etc. which already does that for you?
I would use vertical and horizontal projections of the image. By a simple detection of the highest transition, you easily find the limit between the background and the paper.
Your image has a border of white surrounding a border of black. So you can do two fuzzy -trim processes after padding with the appropriate color to ensure it goes all around the image. So in ImageMagick 6, it would be
Input:
convert book.jpg -bordercolor white -border 1 -fuzz 55% -trim +repage -bordercolor black -border 1 -trim +repage book_trimmed.jpg
Result:

Trim image with transparency around and know it's location

I know it is possible to trim all transparency around image to make image smaller and contain only the image. But is it also possible to somehow know the location of "box", that contains the image?
For example I have 100x100 transparent image, which contains something at 10x10 box having topleft corner at x=15,y=15. Rest is all transparent.
I'd like to end up with 10x10 image, all transparency around trimmed, but also having that 15,15 information. Those are probably 2 separate actions. How do I do this in a script?
Just fyi - I am having bunch of images like this and they are layers, that I need to trim and stack onto eachother to make them clickable.
There are lots and lots of words but no image in your question so I am trying to guess what you want. I made this input image:
magick -size 100x100 xc:black -fill white -draw "rectangle 10,20 50,80" image.png
And I think you want to know the trim box, which is where it would trim to if you ran -trim:
magick image.png -format "%#" info:
41x61+10+20
So that's a 41x61 box with the top-left at (10,20).

regarding the ways of removing surrounding background areas during processing the image

The attached image, showed as follows
It includes some surrounding areas that represents the noise or background introduce while getting the image.
How to remove this part while processing the image. For instance, when I try to segment the original image, I got the following result, where the background areas are also included.
You could use a flood fill starting at the bottom-right corner to fill all pixels less than some "fuzziness" distance (in shades of grey terms rather than in geometric distance terms) with black so they all come out to the same class.
Here I do it with ImageMagick just in Terminal, and colour using red and blue, rather than black, to show the concept:
convert input.jpg -fuzz 15% -fill red -floodfill +1140+760 black result15.jpg
Or, allowing slightly fewer colours (darker) to match via fuzziness:
convert input.jpg -fuzz 10% -fill blue -floodfill +1140+760 black result10.jpg
You can do this with OpenCV in Python, and Wand and other tools. Here is an example showing how to do a floodfill withPIL/Pillow.

How to add a white space around an image using online tools like Pixlr Editor or imagemagick ?

I want to put some white space around an image in equal length. I want to maintain the image pixel ratio to 12:7 too. Any help would do a great help. Thank you.
Another method using Imagemagick V7:
magick ShF4m.jpg -background white -gravity center -extent "%[fx:w+20]"x"%[fx:h+20]" result.jpg
Open image, set the background to white, set the gravity to the centre so the -extent extends the canvas in all directions, increase the canvas size by 20px on the width and 20px on the height.
It would be simple in ImageMagick if you wrote over the inside of the border with white. That way you do not change the image dimensions nor aspect ratio. Here I shave 10 pixels all around and then put a 10 pixel white border all around. Here I will add a red border just to make it visible. But you can change red to white later.
convert barn.jpg -shave 10x10 -bordercolor red -border 10x10 barn_border10.jpg
It would be very hard to add white outside the image of equal amount and keep the aspect ratio. I do not think you can have equal white border amounts and keep the aspect ratio.

Finding the Largest Connected Blob Starting from Selected Point

I am trying to extract a region from an image that is already marked with a certain color. In the picture below
I would like to extract only the pixels which belong to the sidewalk, that is, all pixels that belong to the black blob that is connected to the mid-lower part of the image. There are black dots outside that blob which I am not interested in. So if I could get roughly the region shown below
it would be perfect. Does anyone know of some common algorithms that can do this? Morphology? Region growing using a kind of flooding algorithm?
Thanks,
You can do that quite easily with a flood fill. If I use ImageMagick to demonstrate at the command line because it is installed on most Linux distros and is available for macOS and Windows.
So, bearing in mind that the pixel you identified as your seed is at around 440,520 in the image you supplied that includes the axes, we can floodfill all pixels that match that colour and touch the seed with cyan using:
convert scene.png -fill cyan -draw 'color 440,520 floodfill' result.png
Or, we can make a mask by changing the non-cyan pixels to white and the cyan pixels to black:
convert scene.png -fill cyan -draw 'color 440,520 floodfill' -fill white +opaque cyan -fill black -opaque cyan z.png
There are a thousand other things you can simply do from the command line to take this further... fill small holes in the mask, make a transparency layer from the mask - just ask more questions if you need a hand.
If you want to close the holes in your image, you probably want to use morphological functions. I am away from any computers with ImageMagick for a week so I can only tell you in general terms. Start with the pure black and white (no grey) picture above and try:
convert image.png -morphology open disk:3 result.jpg
Try replacing the word open above with close, erode or dilate. Experiment with disk, disk:3 disk:7 and so on.

Resources