Detecting colors on a RGB image

35 views (last 30 days)
Maida Kasumovic
Maida Kasumovic on 13 Jun 2020
Commented: Image Analyst on 28 Oct 2022
Hello, I have a assignment in my class where I need to load a picture of a rubik's cube, and detect the colors of the cube and write them down into a matrix, here is an example of the picture.
The matrix for this image should be:
orange orange blue
blue blue orange
orange green red
My professor told me I should use functions like. RGB2gray(), imfilter(), graythresh(), bwlabel(), regionprops() etc. but I dont know how to do this.
Any help with a code?
  2 Comments
Ameer Hamza
Ameer Hamza on 13 Jun 2020
Since this is an assignment, so it would not be beneficial if we directly write the code. What have you already tried, and what algorithm you propose to detect those colors?
Maida Kasumovic
Maida Kasumovic on 13 Jun 2020
Its not an important assignment for my class since image processing is not my major, its just a small part of one of my subjects so it would be nice if I could get some help. I have tried something with boundingboxes in the regionprops, and got two codes for the picture above.
The first one:
%% Bounding box
close all
clear all
clc
I = imread('sedam.jpg');
I_size = size(I);
threshold = 150;
bw = zeros(I_size(1),I_size(2));
for i = 1:I_size(1)
for j = 1:I_size(2)
pixel(1) = I(i,j,1);
pixel(2) = I(i,j,2);
pixel(3) = I(i,j,3);
if ( abs(norm(double(pixel) - double([0 0 255]))) < threshold)
bw(i,j) = 1;
end
if ( abs(norm(double(pixel) - double([0 255 0]))) < threshold)
bw(i,j) = 1;
end
if ( abs(norm(double(pixel) - double([255 0 0]))) < threshold)
bw(i,j) = 1;
end
end
end
figure,imshow(bw);
stats=regionprops(bw,'BoundingBox','Centroid');
figure
imshow(I)
hold on
for obj=1:(length(stats))
bbox=stats(obj).BoundingBox;
bcentroid=stats(obj).Centroid;
rectangle('Position',bbox,'EdgeColor','r','LineWidth',2)
end
The second one:
rgbImage=imread('sedam.jpg');
% Convert RGB to HSV color space.
hsvImage = rgb2hsv(rgbImage);
% Extract the Saturation channel and threshold it at, say, 0.25
mask = hsvImage(:,:,2) > 0.675;
% extract the 16 largest blobs
mask = bwareafilt(mask, 16);
% Get bounding boxes
stats = regionprops(mask, 'BoundingBox','Centroid')
figure
imshow(rgbImage)
hold on
for obj=1:(length(stats))
bbox=stats(obj).BoundingBox;
bcentroid=stats(obj).Centroid;
rectangle('Position',bbox,'EdgeColor','r','LineWidth',2)
end

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 13 Jun 2020
Try this:
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = pwd;
baseFileName = 'sedam.jpg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the RGB image full size.
subplot(1, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
% Threshold the pixels that are not gray scale.
hsvImage = rgb2hsv(rgbImage);
threshold = .2;
mask = hsvImage(:, :, 2) > threshold & hsvImage(:, :, 3) > 0.2;
% Get rid of background
mask = imclearborder(mask);
% Fill holes
mask = imfill(mask, 'holes');
% Take the largest 9 blobs only.
mask = bwareafilt(mask, 9);
% Display the binary image.
subplot(1, 2, 2);
imshow(mask, []);
axis('on', 'image');
caption = sprintf('Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Extract the individual red, green, and blue color channels using imsplit() (introduced in R2018b).
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
% Measure the colors
propsR = regionprops('table', mask, redChannel, 'MeanIntensity')
propsG = regionprops('table', mask,greenChannel, 'MeanIntensity')
propsB = regionprops('table', mask, blueChannel, 'MeanIntensity')
Here are the colors:
propsR =
9×1 table
MeanIntensity
________________
224.190361623616
2.59752107003672
222.903755176795
10.3684728359575
225.160324612946
2.80965263253615
3.1237139461745
225.969213059489
156.868206509418
propsG =
9×1 table
MeanIntensity
________________
87.6111586715867
89.561960229492
87.2096606050236
152.169745591831
89.2610873563985
92.877315972538
94.3119784382858
94.6842312368222
24.6052271222715
propsB =
9×1 table
MeanIntensity
________________
53.8946715867159
146.488723588315
55.069970309673
31.8296795229311
56.3948326176616
148.606142584341
147.144448383856
61.6532827852725
20.9011979577586
They are not in left-to-right, top-to-bottom order so you'll have to sort them if you want to know which color is where.
  4 Comments
Sang
Sang on 28 Oct 2022
What's wrong with me?
Image Analyst
Image Analyst on 28 Oct 2022
Try this:
% Demo by Image Analyst
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = pwd;
baseFileName = 'sedam.jpg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the RGB image full size.
subplot(1, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
% Threshold the pixels that are not gray scale.
hsvImage = rgb2hsv(rgbImage);
threshold = .2;
mask = hsvImage(:, :, 2) > threshold & hsvImage(:, :, 3) > 0.2;
% Get rid of background
mask = imclearborder(mask);
% Fill holes
mask = imfill(mask, 'holes');
% Take the largest 9 blobs only.
mask = bwareafilt(mask, 9);
% Display the binary image.
subplot(1, 2, 2);
imshow(mask, []);
axis('on', 'image');
caption = sprintf('Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Extract the individual red, green, and blue color channels using imsplit() (introduced in R2018b).
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
% Measure the colors
propsR = regionprops('table', mask, redChannel, 'MeanIntensity', 'Centroid')
propsG = regionprops('table', mask,greenChannel, 'MeanIntensity', 'Centroid')
propsB = regionprops('table', mask, blueChannel, 'MeanIntensity', 'Centroid')
% Label the blobs with the id
hold on;
for k = 1 : size(propsR, 1)
thisCentroid = propsR{k,"Centroid"};
x = thisCentroid(1);
y = thisCentroid(2);
plot(x, y, 'r.', 'MarkerSize', 30);
labelString = sprintf(' %d', k);
text(x, y, labelString, 'FontSize', 15, 'Color', 'r');
end

Sign in to comment.

Categories

Find more on MATLAB Report Generator in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!