How to Set a color to a value in image processing

1 次查看(过去 30 天)
This is the photo that I am using. Its a front view CAD drawing of a newsstand. In the image, the dark grey squares are representative of products that would be on their designated shelves. The green and red colors are tags that would be behind products. If green is shown, 2 products are missing. If red is shown, 3 products are missing. I want to identify this in the photo and put them in a vector: product 1 would be the first position in the vector and so on. Can anyone please help me with the code that I currently have.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Read in a standard MATLAB color demo image.
folder = 'C:\Users\FIQ\Documents\Temporary';
baseFileName = 'FrontNewsStand.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
I = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(I);
% Display the original color image.
subplot(4, 3, 1);
imshow(I);
axis on;
hold on;
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = I(:, :, 1);
greenChannel = I(:, :, 2);
BinaryRed = redChannel > 100;
BinaryGreen = greenChannel > 100;
classifiedImageG = 1 * int32(BinaryGreen);
classifiedImageR = 1 * int32(BinaryRed) ;
Demand_Vector = [0 0 0 0 0 0 0 0 0];
%Cropping of Row 1, Column 1
I1=imcrop(I,[20 7 170 165]);
subplot(4, 3, 4);
imshow(I1);
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(1) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(1) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(1) = 0;
end
%Cropping of Row 1 Column 2
I2=imcrop(I,[185 7 185 165]);
subplot(4, 3, 5);
imshow(I2);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(2) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(2) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(2) = 0;
end
%Cropping of Row 1 Column 3
I3=imcrop(I,[365 7 170 165]);
subplot(4, 3, 6);
imshow(I3);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(3) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(3) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(3) = 0;
end
%Cropping of Row 2 Column 1
I4=imcrop(I,[20 170 170 165]);
subplot(4, 3, 7);
imshow(I4);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(4) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(4) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(4) = 0;
end
%Cropping of Row 2 Column 2
I5=imcrop(I,[185 170 185 165]);
subplot(4, 3, 8);
imshow(I5);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(5) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(5) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(5) = 0;
end
%Cropping of Row 2 Column 3
I6=imcrop(I,[365 170 170 165]);
subplot(4, 3, 9);
imshow(I6);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(6) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(6) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(6) = 0;
end
%Cropping of Row 3 Column 1
I7=imcrop(I,[20 335 170 165]);
subplot(4, 3, 10);
imshow(I7);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(7) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(7) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(7) = 0;
end
%Cropping of Row 3 Column 2
I8=imcrop(I,[185 335 185 165]);
subplot(4, 3, 11);
imshow(I8);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(8) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(8) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(8) = 0;
end
%Cropping of Row 3 Column 3
I9=imcrop(I,[365 335 170 165]);
subplot(4, 3, 12);
imshow(I9);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(9) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(9) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(9) = 0;
end
disp(Demand_Vector);

采纳的回答

Image Analyst
Image Analyst 2013-10-28
Try the attached file (below).
  1 个评论
Cady
Cady 2013-10-28
Works great, thank you very much. I now have to add these up and put them in a vector such as this [0 3 0 0 0 1 1 0 0]. I greatly appreciate your help with everything.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by