Detecting Red Green and Blue by cancelling background - matlab

I have continuous image acquisition and have a white paper at the background of the camera (facing the camera)
The image with just the white background is producing a greenish blue tinge due to light bulbs used in the room which is resulting in output as GREEN for the following code
green = im(:,:,2);
red = im(:,:,1);
if sum( green(:)) > sum(red(:))
disp('green');
else
disp('red');
end
Is there any way to CANCEL the white paper background for processing only the color objects that will be placed in front of the camera ??
The Color objects will be RED, GREEN and BLUE squares

The way you are testing for green or red is fundamentally flawed - you need to look for the dominant colour using a suitable threshold, otherwise you will get false positives. E.g.
threshold = 0.7; % determine a suitable threshold empirically
blue = sum(sum(im(:,:,3)));
green = sum(sum(im(:,:,2)));
red = sum(sum(im(:,:,1)));
total = red + green + blue;
if (red / total > threshold)
disp("red");
elseif (green / total > threshold)
disp("green");
elseif (blue / total > threshold)
disp("blue");
else
disp("indeterminate colour");
end

Related

How to change a gray scale image to red colored image?

I have an image of a thick wave which changes from black to white smoothly like the black slowly changes from dark to white. How can I change this image to red instead of having different shades of black I need different shades of red going from dark red to white.
Create an array of size (rows,cols,3) where (rows,cols) is the size of gray image. Make its red channel equal to 255 and other channels equal to gray image
img = imread('moon.tif');
[r,c,z] = size(img);
red_img = uint8(zeros(r,c,3));
red_img(:,:,1) = 255;
red_img(:,:,2) = img;
red_img(:,:,3) = img;
subplot(1,2,1);
imshow(img,[]);
subplot(1,2,2);
imshow(red_img,[]);

How can i detect silver pixels in an image?

I'm trying to detect all the pixels with silver colors in my image.
I tried to convert the image from RGB to HSV but I saw that every silver pixel is very different from another and therefore we couldn't identify all the silver pixels.
I'm trying to understand if it's possible to use HSV to solve this problem or maybe there is a better way to detect all the silver pixels?
I'm trying to use this code that will mark all the silver pixels in blue:
I chose the Hue,Saturation and Value (HSV) according to wikipedia to identify the silver color, therefore Hue doesn't matter, Saturation is 0-0.1 and Value is 0.65-0.85.
clear all; close all; clc; imtool close all;
im = imread('image.jpg');
size_x = size(im,1);
size_y = size(im,2);
size_z = size(im,3);
hsv = rgb2hsv(im);
for i = 1:size_x
for j = 1:size_y
if (hsv(i,j,2) < 0.1) && (hsv(i,j,3) > 0.65) && (hsv(i,j,3) < 0.85)
im(i,j,1) = 0;
im(i,j,2) = 0;
im(i,j,3) = 255;
end
end
end
imtool(im);
with this Image:
Thanks in advance.
The difference between a white pixel and a grey pixel is the amount of light that falls on it. You can see this in your picture, the whitish background changes brightness from left to right, going from middle grey to light grey. We perceive it all as white.
The same is true for silver. Silver is a white/grey color, but we perceive it differently because it has specular reflections. This is what characterizes metal surfaces. But the color of the pixels themselves are just grey and white.
That is to say, you cannot use color segmentation to distinguish silver contacts from white paper.
I would suggest you look for the shapes of interest, rather than the colors of interest.

How to set up a colormap from white to green with n shades

I want to set up a Matlab colormap from white (min values) to green (max values) with any number of shades.
I think about a colormap similiar to the summer preset but reversed and with a white edge instead of the yellow one.
c = [zeros(64,1) linspace(0,1,64)' zeros(64,1); % black to green
linspace(0,1,64)' ones(64,1) linspace(0,1,64)']; % green to white
surf(peaks);
colormap(c);

How to change green pixels to gold color in peppers image with matlab?

I want to change green color pixels to gold color in peppers.png image in Matlab.
How can I do this task? Thanks very much for your help
Introduction
Using the HSV colorspace gives a better intuition of detecting a certain color hue and manipulating it. For further information, read the following answer.
Solution
Given an image in hsv format, each color has a certain range which it can reside in. In the peppers image, the hue channel of green peppers is in the range [40/360,180/360] (more or less). Also, the color gold can be identified by a hue value of 0.125 and 'V' value of 0.8. Therefore, a good way to change green to gold in a certain picture will be as follows:
transform the image to hsv.
locate green colors by identifying hue value between the range [40/360,180/360].
changing their first channel to 0.125, and their second channel to 0.8.
transform back to rgb.
*comment: instead of fully changing the third channel of the green pixels to 0.8, it will be better to perform an averaging of 0.8 with the originally stored value, to get a more natural effect (see code below).
Code
%reads the image. converts it to hsv.
I = imread('peppers.png');
hsv = rgb2hsv(I);
%locate pixels with green color
GREEN_RANGE = [40,180]/360;
greenAreasMask = hsv(:,:,1)>GREEN_RANGE(1) & hsv(:,:,1) < GREEN_RANGE(2);
%change their hue value to 0.125
HUE_FOR_GOLD = 0.12;
V_FOR_GOLD = 0.8;
goldHsv1 = hsv(:,:,1);
goldHsv1(greenAreasMask)=HUE_FOR_GOLD;
goldHsv3 = hsv(:,:,3);
goldHsv3(greenAreasMask)=V_FOR_GOLD;
newHsv = hsv;
newHsv(:,:,1) = goldHsv1;
newHsv(:,:,3) = newHsv(:,:,3) + goldHsv3 / 2;
%transform back to RGB
res = hsv2rgb(newHsv);
Result
As you can see, the green pixels became more goldish.
There is a room for improvement, but I think that this would be a good start for you. To improve the result you can modify the green and gold HSV values, and use morphological operations on greenAreasMask.

Matlab - How to detect green color on image?

I'm working in project that basically I have to detect the threes on image and delete the other information. I used HSV as segmentation and the function regionprops to detect each element. It works fine, but in same cases that has house roofs, they aren't deleted because the value of Hue is similar to the threes. So far, this is the result:
To remove the roofs, I thought that maybe is possible detecting the color green in each region detected. If the region dont have 70% of green (for example) that region is deleted. How can I do that? How Can I detect only the green color of the image?
Solution Explanation
Evaluating the level of green in a patch is an interesting idea. I suggest the following approach:
convert your patches from RGB to HSV color system. In the HSV color system it is easier to evaluate the hue (or - the color) of each pixel, by examining the first channel.
Find the range for green color in the hue system. In our case it is about [65/360,170/360], as can be seen here:
for each patch, calculate how many pixels have the hue value which is in the green range, and divide by the size of the connected component.
Code Expamle
The following function evaluate the "level of greenness" in a patch:
function [ res ] = evaluateLevelOfGreen( rgbPatch )
%EVALUATELEVELOFGREEN Summary of this function goes here
% Detailed explanation goes here
%determines the green threshold in the hue channel
GREEN_RANGE = [65,170]/360;
INTENSITY_T = 0.1;
%converts to HSV color space
hsv = rgb2hsv(rgbPatch);
%generate a region of intereset (only areas which aren't black)
relevanceMask = rgb2gray(rgbPatch)>0;
%finds pixels within the specified range in the H and V channels
greenAreasMask = hsv(:,:,1)>GREEN_RANGE(1) & hsv(:,:,1) < GREEN_RANGE(2) & hsv(:,:,3) > INTENSITY_T;
%returns the mean in thie relevance mask
res = sum(greenAreasMask(:)) / sum(relevanceMask(:));
end
Results
When using on green patches:
greenPatch1 = imread('g1.PNG');
evaluateLevelOfGreen(greenPatch1)
greenPatch2 = imread('g2.PNG');
evaluateLevelOfGreen(greenPatch2)
greenPatch3 = imread('g3.PNG');
evaluateLevelOfGreen(greenPatch3)
Results:
ans = 0.8230
ans = 0.8340
ans = 0.6030
when using on non green patches:
nonGreenPatch1 = imread('ng1.PNG');
evaluateLevelOfGreen(nonGreenPatch1)
result:
ans = 0.0197