Max color used in a RGB image - image

I have some RGB images.
What's the best way to know most color used in an image is Red or Yellow or White?
The input images must have more than 50% red, yellow or white pixels and it's impossible to an image have two colors in same percentage.
Other colors in image may be black or blue.
Is there any function in MATLAB for this?
Note that i need a method with good performance for this!
Thanks in advance...

Convert your image to the HSV colorspace (rgb2hsv) and find appropriate thresholds for Red, Yellow and White on the Hue values. E.g.
[H S V] = rgb2hsv(I);
num_red_pixels = nnz(H>=red_min & H<=red_max);

Related

Detecting anti-aliased or undersampled text image

I have an image that is essentially a text document (black and white) but due to anti-aliasing/undersampling applied during scanning, the image contains a lot of color, light tone pixels and is thus saved as a full-color image i.e: takes a lot of space.
My goal is to be able to detect Black and White image candidates in order to convert them from full color to B&W which dramatically reduces their size.
Is there a way to detect such anti-aliased/undersampled images? Doing color pixel analysis doesn't help because the colored pixels end up being close in amount to the black pixels... Essentially I want to be able to detect that the colored pixels come from anti-aliasing/undersampling a black & white image and not from a picture type image.
Here is an example image:
As you can see there are many more colors than just black. However this image is a good candidate for Black & White / Greyscale conversion instead of full color. How can I detect such images? Please note that in this example the colors tend to be on the grey side but there are many cases where they are cyan or brown etc.
I think it is a valid question. I don't have 50 reputation to post a comment so I will post this as an answer.
Basically, in a black and white anti-aliased image the various grey colors are opacity differences of the black color. If we observe those pixels they will be like these listed below. So, if the operation is a color manipulation then apply the same opacity picked up from those grey pixels to the new color.
rgba(0,0,0,0.6)
rgba(0,0,0,0.9)
rgba(0,0,0,0.5)
rgba(0,0,0,0.9)
rgba(0,0,0,0.6)
rgba(0,0,0,0.1)
rgba(0,0,0,0.5)
In my opinion, the pixels other than grey, in this example image, cyan and brown as it appears can be safely ignored because they seemed like not part of the original text. If there were a few more example images of non grey pixels would have been good. But if we cannot ignore them just need to get the pixel opacity and apply the same color manipulation. In other words we treat them as black pixels.

Algorithm for smart floodfill in raster image

I'm trying to implement flood fill method for raster image.
For center pixels it's easy and works correct, but the problem is to fill pixels near border, which have different color.
For example, if draw Black figure on White background, some border pixels will have kind of gray color instead of black (for smoothing).
Image editors (like paint.net) during floodfill fixes it changing these pixels to some middle color between old and new one. Here I filled figure in red color, and gray pixels became in red gradient
I need to know method or algorithm how gray pixels became in kind of color to fill (here it's red, but can be any) using RGB pixel manipulation.
Thanks for any help.
So, for similar effect like in example we just need to use & operation between old and new color.
For RGB color:
resultColor.R = (byte)(oldColor.R & newColor.R);
resultColor.G = (byte)(oldColor.G & newColor.G);
resultColor.B = (byte)(oldColor.B & newColor.B);
If RGB color is Int number:
resultColor = oldColor & newColor;
It will not be exactly same color as in example below but pretty similar.

Finding the Most Bold Color in a Picture

I have an array of Colors that represents a picture. I want to find the most bold / most standout color from that frame (aka a bright pink if there is no pink in the frame or a bright yellow if there is no yellow in the frame, etc). Currently I don't know how to do this efficiently. Is there any known or efficient way of doing this efficiently for a 1280 by 720 pixel image (921,600 pixels) or any method you can think of?
As a starting point, I would say you are looking for the most saturated colour, so I would suggest converting to HSL colorspace, discarding the Hue and Lightness and seeing which one has the highest saturation.
So, for a quick example using ImageMagick using these two images:
This is the command yo would run in Terminal:
convert lighthouse.png -colorspace hsl -separate -delete 0,2 saturation.png
And here are the results - the most saturated colours show up as white.

Color approximation

Let's suppose we have a regular RGB image. Now we would want to approximate the color of each individual pixel of our source image with a color out of a small set of colors.
For example, all tones of red should be converted to that specific red out of my set of colors, same goes for green, blue, etc.
Is there any elegant way/algorithm to achieve this?

block image detection

i want to detect color from a pixelated image, and then, turn the specific pixel into different color?
for example:
pixel with B/G/R < 150 re-color it to white. and pixel with B/G/R > 150 to black or stay.
is it possible? if yes, how? i need guidance.
The answer is to convert your image into HSV Color Space!
You can Mask out Blue color from the Image using inRange function as follows:
Mat mInput_Bgr,mInput_HSV,mBlueMask,mDestination;
mInput_Bgr= imread(FileName_S,1);
imshow("mInput_Bgr",mInput_Bgr);
cvtColor(mInput_Bgr,mInput_HSV,COLOR_BGR2HSV);
inRange(mInput_HSV,Scalar(104,200,0),Scalar(132,255,255),mBlueMask);// You can play with all the values to choose your desired color
imshow("mBlueMask",mBlueMask);
mInput_Bgr.setTo(Scalar(255,255,255),mBlueMask);// You can set what ever Color Value
imshow("Corrected_mInput_Bgr",mInput_Bgr);

Resources