block image detection - image

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);

Related

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.

Matlab: How can I find the number of black pixels in a color image?

I wish to find the number of black pixels in a color image using MATLAB, how can I do it?
Thanks.
Use nnz like this -
nnz(all(im==0,3))
where im is your image data.
Alternatives - sum(reshape(all(im==0,3),[],1)) and sum(sum(all(im==0,3))).
The assumption here is that black pixels are triplets (0,0,0).
Instead if you define black pixels as pixels that have values in the interval [0 th] for the same pixel location across all channels, use this -
nnz(all(im<=th,3))

Why does imagesc change colormap (MATLAB)

I want to use imagesc to crop and display a black and white (grayscale?) image. However, it keeps displaying the image in rgb, making it look like it came from an infra-red camera! Any tips for preserving the original colors?
You need to change the color map. After you run imagesc use colormap('Gray'). You can set default colormap to current colormap using colormap('default')

How to convert gray scale image which assigned threshhold value in some pixel of original RGB image to RGB?

I have original RGB image.
then, I convert it to gray scale.
then, I assign new threhold value into some pixel.
then, I need to convert the gray scale image from step#3 to RGB image.
I am trying on the step#4. Could you help to advise?
Check out rgb2hsv and hsv2rgb in matlab. Then threshold on the value matrix. This makes more sense to me.
Another possibility is to convert to grey scale but keep the original colour image. Then create a mask by thresholding the grey scale image and use that mask to discard the pixels above/below your threshold on the colour matrix.

Max color used in a RGB 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);

Resources