Creating a more effective matt for chromakey? - imagemagick

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

Related

Ignore the colour white in the imagemagick convert colors histogram output

I would like to ignore/filter white and those that are close to white when using this type of command
convert $file -colors 10 -format "%c" histogram:info:
I have some images that are predominantly white in their content and i'd like to focus on the other colours.
I thought i could use something like -fuzz 20% -fill "#0000ff" -opaque white to change all white and white'ish into a simple blue which i could then grep out in the histogram output but no luck.
Can anyone point me in the correct direction or offer an example please ?
Let's say for example i have a photo that contains a very overexposed sky i.e lot's of white. I'd like the histogram output to ignore that part of the photo and instead output the other top 10 colours that are in the photo.
Thanks
fLo

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.

ImageMagick Unable to change colour in shade explicitly for image

I want to use ImageMagick to change colour in shade.
I am able to manage the shade change using :
convert input.png -colorspace HCL -channel R -evaluate set 5% +channel -colorspace sRGB output.png
Using set XX% i am able to get different colours like, red, green, yellow, blue, pink, sky-blue, gray, etc.
The below command works for targeting blue colour :
convert input.png -colorspace HCL -channel R -separate +channel -level 48,52% output.png
But I am unable to target other colour explicitly.
For example, if I want to change green colour with some other colour, resulted image will effect green, yellow,red and sky-blue as well.
Is there a way to explicitly change a single colour in shade for :
yellow
sky-blue
pink
green
white
black
red
I tried changing all -channel : R,G,B,C,M,Y,K,A,O.
Using -separate option I can target RBG, but the problem with RGB is R effect red, yellow and pink, G effect green, sky-blue and yellow and B effect blue, pink and Sky-blue.
sample for output :
RGB image colour change
expected output : In the above output for "output-0" it effect red,yellow and pink. i want the command which will effect only red. similarly for other colours as well.
links I used : https://www.imagemagick.org/discourse-server/viewtopic.php?t=33361
I am using python to run this command. I am also open to use other libraries which will work with all the colours explicitly.
If your image is representative like I requested, it is as simple as this:
magick rgb.png -fill white -opaque red result.png
If you also want to affect hues "close to red", you can apply some fuzz:
magick rgb.png -fuzz 40% -fill white -opaque red result.png
Notice that also affects the edges of the red circle where it is a "feathered red".
If not, your ImageMagick code is essentially doing a "Hue rotation" and, as you have noticed, it affects the entire image. Read the Wikipedia page on HSV before continuing. Here is an HSI Hue wheel for reference:
The solution is to do your Hue rotation, but apply its effects via a mask that only selects the colours/areas you want affected. Remember that OpenCV halves the Hue from the range 0..360 to 0..180 so that it can store a Hue in a np.uint8.
So, if we load the same image as above and select only the greens (where Hue is near 120) we can rotate just those into blues by adding 60 (Hue=240):
#!/usr/local/bin/python3
import cv2 as cv
import numpy as np
# Load the image and convert to HSV colourspace
image = cv.imread("rgb.png")
# Convert to HSV and split channels
hsv=cv.cvtColor(image,cv.COLOR_BGR2HSV)
H,S,V = cv.split(hsv)
# Shift only greens (Hue near 120) around hue circle by 120 degrees to blues - remembering OpenCV halves all these values - see comment
H[(H>55)&(H<65)] += 60
# Recombine into single 3-channel image and convert back to RGB
result = cv.merge((H,S,V))
result = cv.cvtColor(result,cv.COLOR_HSV2BGR)
cv.imwrite("result.png",result)
If you want to change the blues (Hue=240) into yellows (Hue=60), just change this:
H[(H>55)&(H<65)] += 60
into this:
H[(H>115)&(H<125)] -= 90
If you want to broaden the range of greens affected, decrease the 55 in my code and/or increase the 65. If you want to move greens to a different hue, either increase or decrease the 60.
You can do all the stuff above with PIL/Pillow if you want to - you don't need to install the (massive) OpenCV.
Keywords: Image, image processing, Python, OpenCV, ImageMagick, Hue, HSL, HSV, hue rotation, colour replacement, selective colour, mask.

Imagemagick how to delete all colors expect black

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

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