MATLAB: Setting pixel to maximum color values in grayscale image.

colorimage analysisimage processingImage Processing Toolboximage segmentation

I have a grayscale image attached below. I want to make all the circular objects a true red color (255, 0 0). However, after I convert the the grayscale to rgb using:
gray_as_RGB = repmat(grayimg, [1 1 3]); the range of values is not between 0-255. I do NOT want to normalize the range to 0-255 and then change the values and then recombine.
This is my code so far with grayimg being the image I have attached
grayimg;
% thresholding to find ciruclar objects
level = graythresh(grayimg)
BW = imbinarize(grayimg,level);
% convert grayscale to rgb
gray_as_RGB = repmat(grayimg, [1 1 3]);
% get color channels
redc = gray_as_RGB(:,;,1);
greenc = gray_as_RGB(:,;,2);
bluec = gray_as_RGB(:,;,3);
% This is the part I am confused about. I want the circles to have a true red but I do not know what value to set since it is not in the range of 0-255.
% I do not want to normalize since I will lose data. Right now I am thinking to just set the red channcel values, in the appropriate area to some random high number hoping that it is going to be the maximum of the range
redc(BW) = 999999999999999999;
greenc(BW) = 0;
bluec(BW) = 0;
final_img == cat(3, redc, greenc, bluec);
%The red in the final image does not look like true red. What should I set redc(BW) to accomplish this.
I don’t want to convert any values to the 0-255 scale. How do I find the highest possible value in the range I am in?

Best Answer

Try this:
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 = 20;
%===============================================================================
% Read in gray scale image.
folder = pwd;
baseFileName = 'grayscale copy.png';
% Get the full filename, with path prepended.
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);
% Display the image.


subplot(2, 3, 1);
imshow(rgbImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 2); % Take green channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Display the histogram of the image.
subplot(2, 3, 2);
imhist(grayImage);
grid on;
title('Histogram of Image', 'FontSize', fontSize, 'Interpreter', 'None');
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Binarize the image
binaryImage = grayImage > 1200;
% Get rid of particles less than 1000 pixels.
binaryImage = bwareaopen(binaryImage, 1000);
% Label the image.
labeledImage = bwlabel(binaryImage);
% Display the image.
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
% Let's assign each blob a different color to visually show the user the distinct blobs.
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
% coloredLabels is an RGB image. We could have applied a colormap instead (but only with R2014b and later)
subplot(2, 3, 4);
imshow(coloredLabels);
title('Unique Blobs', 'FontSize', fontSize, 'Interpreter', 'None');
% Make an RGB image
redImage = im2uint8(imadjust(grayImage));
greenImage = redImage;
blueImage = redImage;
% Make the mask area 0 in green and blue and 255 in red.
redImage(binaryImage) = 255;
greenImage(binaryImage) = 0;
blueImage(binaryImage) = 0;
% Concatenate indeividual color channels into an RGB image:
rgbImage = cat(3, redImage, greenImage, blueImage);
% Display the image.
subplot(2, 3, 5);
imshow(im2double(rgbImage), []);
title('RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
impixelinfo; % Show (x,y) RGB as you mouse over the image.
0000 Screenshot.png