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.
Related
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:
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 have question. Is it possible to delete all colors from image, but save the black color? I have a picture with several unknown colors. (Therefore I can't just replace e.g red color with white color). I have an image like this:
And I am trying to delete this "Text2" and "Text3". Is that possible? Which option in Imagemagick should I use?
Mark's answer is probably the best if you easily can separate the black region from the rest of the image, since it preserves the antialiased text better. However, if not then you can do something similar to Bonzo's command. Here is another variation of that.
convert EWwSX.jpg -fuzz 40% -fill white +opaque black result.png
Not sure I understand your question - you can't really delete a colour in an image. What would be left? I understand you can't replace the red with white because you have reds elsewhere in your image.
I guess the easiest thing to do is draw a white rectangle over the unwanted text:
convert text.jpg -fill white -draw "rectangle 20,72 100,150" result.jpg
Not a great result but with some tweeking or work you could improve it:
convert EWwSX.jpg -threshold 20% black.png
I just encountered unexpected behavior in ImageMagick, which I'm hoping someone can explain to me.
Version numbers
$ convert --version
Version: ImageMagick 6.7.7-10 2013-02-25 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP
Running on Linux Mint 15 Olivia (based on Ubuntu 13.04 "Raring Ringtail").
Executive summary
Running the same operation with two images that should be quite similar, except that one is a GIF while the other is a JPEG, the resulting output is entirely different. The GIF comes out black, the JPEG comes out white.
To reproduce:
Go to http://karenswhimsy.com/public-domain-images/animal-silhouettes/animal-silhouettes-1.shtm and download the elephant silhouette as elephant.jpg
Download http://www.arthursclipart.org/silhouettes/animals/DUCK1.gif as DUCK1.gif
convert elephant.jpg -negate -alpha shape output-elephant.png
convert DUCK1.gif -negate -alpha shape output-duck.png
Compare the output-elephant.png and output-duck.png images
Can anyone explain why these two output images are different? Why the GIF duck ends up being black after conversion, while the JPEG elephant becomes white after conversion?
Long-winded explanation
The situation is this: I want to take some black-and-white images, turn the background transparent, and turn the foreground different colors. For example, starting with the elephant silhouette at http://karenswhimsy.com/public-domain-images/animal-silhouettes/images/animal-silhouettes-1.jpg (I saved the image as elephant.jpg), I want to produce a .png with a transparent background and an elephant that's green, red, yellow, or whatever color I want.
The command I'm using to do this is:
convert elephant.jpg -negate -alpha shape +level-colors ,green green-elephant.png
This does exactly what I want. First it inverts the image so that the background is black and the elephant is white, because -alpha shape expects an alpha mask where black = fully transparent and white = fully opaque. Then -alpha shape does its magic and produces a white elephant against a transparent background. Then +level-colors ,green kicks in, transforming black-and-white gradients into the two colors specified as parameters to +level-colors; here, the first color is omitted so it would remain black, and the second color is what white turns into. This produces a green elephant with smooth borders -- exactly the result I want -- and I'm quite happy with it.
Next, I tried running the same command against one of the images from http://www.arthursclipart.org/silhouettes/animals.htm (I used DUCK1.gif). The input source is the same -- a black silhouette against a white background -- so I expected the same result, a green duck against a transparent background. But it didn't work. A little research showed me that -alpha shape was behaving differently. Where with the JPEG elephant it was producing a white elephant against a transparent background, with the GIF duck the same command was producing a black duck against a transparent background. In other words, -alpha shape was inverting the result with a GIF image source, but not with a JPEG image source. So to color the animal properly, I needed to rewrite the +level-colors parameters to put "green" before the comma instead of after.
Tweaking my script is no problem at all, but I'd love to understand why this is happening, and so far I'm clueless. Can anyone explain to me why ImageMagick is treating GIFs and JPEGs so differently in the -alpha shape operation?
OK, I think here is the story. With GIF, a background color might be explicitly defined, and I think that is the case with DUCK1.GIF. Not so with JPEG; here the background is, I think, assumed to be "white".
Whatever the case is, converting the DUCK1.GIF to DUCK1.JPG results in a similar image to the elephant. Moreover, you can make sure that the same background color is used with the apropriate Imagemagick option. The two commands below produce similar output:
convert DUCK1.jpg -background Black -negate -alpha shape output-duck.png
convert elephant.jpg -background Black -negate -alpha shape output-elephant.png
I'm trying to chromakey some pictures. Here is an example of one:
Here is another one,
Now using image magic, I can generate a mat like this..
But I can never get the mat to be "full". My plan is to create a static mat for the turntable and the lightbank -- those won't have to be removed. But, I'd like to fix the problems I'm seeing with the grill, licenseplate, and window. I'd like the car to show up pitch-black. I'm using ImageMagick's convert to get this working,
convert 1.bmp -channel g -separate +channel -fuzz 45% -fill black -opaque black -fill white +opaque black greenscreensample_mask_1.gif
How can I improve this to fill in the bumper of the vehicle?
I would guess the shinny parts are slightly green and you could try reducing the fuzz value.
You can use the -fx operator and then work with specific channels. The following is by no means optimal, and also, it is very inefficient to execute:
convert ./in.jpg -background none -transparent white -channel Alpha -fx '1-((g-r)+(g-b)+(g-(r+b)/2))^2' -channel Green -fx '(r+b)/2' ./out.png;eog ./out.png
in order to obtain a key for the green channel you can subtract the
red from the green
blue from green
average of blue and red channels from the green channels
the very basic colour correction involves replacing fringed areas with the average of the blue and red channels, here however the entire image had its green channel replaced with the average of the blue and red channels. you should actually write an algorithm that seperates the fringe into a seperate channel, then you colour correct the entire image and mix it in with the original based on this "fringe" matt.
thankyou, best of luck