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:
Related
[]
I use imagemagick -6 for conversion.
I have an image like above. Actual image resides in the rectangular area. I need to remove the L shaped corner edges as you see. I tried with lots of options available in : https://imagemagick.org/Usage/crop/#splice . As crop/chop and shave options applicable symmetrical to both sides. I was thinking If I can just splice the un-wanted L shaped areas.
I tried with convert -background blue -gravity East -splice 10x10 +repage but of no luck. It basically adds a cross in horizontal and vertical in the middle of the image. Please advise if it possible just to erase/chop a particular area. e.g. some portion of an edge and not the complete edge. The reason here is I don't want to chop-off any part of rectangular shape but the L shapes above and below. Basically looking for removing/splicing with white color just L shaped one corner at a time if possible using Imagemagick-6. As I don't know about the pixel size in advance. I don't know if one liner solution will work.
Many thanks!
I think you mean this:
magick image.png -crop 305x365+232+58 result.png
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.
I'm using an example for ImageMagick's gradient found here:
http://www.imagemagick.org/Usage/photos/#tilt_shift
The arguments are as follows:
magick convert beijing_contrast.jpg -sparse-color Barycentric "0,0 black 0,%h white"-function polynomial 4,-4,1 beijing_blurmap.jpg
It creates a perfect gradient image with white at the top and bottom:
But I can't figure out how to do the same thing for portrait image (make white at the left and right edges).
Please help.
In Imagmagick, just change the arguments to sparse-color. Use %w,0 rather than 0,%h. Note also that in Imagemagick 7, you should use just magick and not magick convert and not convert. Otherwise, you may get IM 6 behavior. For other tools such as identify and mogrify and montage, etc, you do need to preface those with magick. Note also you are missing a space before -function. Try
magick beijing_contrast.jpg -sparse-color Barycentric "0,0 black %w,0 white" -function polynomial 4,-4,1 beijing_blurmap.jpg
If needed, swap the black and white depending upon whether you want it white in the middle or black in the middle.
For IM 6.9.2.5 or higher, there are new convenience defines for creating various directional gradient. But you need to specify the image size. See https://www.imagemagick.org/script/gradient.php
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.
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.