MATLAB Answers

0

Setting pixel to maximum color values in grayscale image.

awezmm 님이 질문을 제출함. 28 Jun 2019
최근 활동 awezmm 님이 댓글을 추가함. 1 Jul 2019
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?

  댓글 수: 0

로그인 to comment.

답변 수: 2

Image Analyst 님의 답변 29 Jun 2019
 채택된 답변

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

  댓글 수: 3

I already said that I do NOT want to change the pixel values to a 0-255 range. I need to keep the values I have. I just want to know how to change the pixel of the blob to a pure green color. A pure green color is a color that has the highest possible maximum value in the green channel and 0 in the red and blue channels. How do I find that highest possible maximum value in the current range I am?
Then try
Make an RGB image
redImage = grayImage;
greenImage = redImage;
blueImage = redImage;
% Make the mask area 0 in green and blue and max integer value in red.
redImage(binaryImage) = intmax(class(grayImage));
greenImage(binaryImage) = 0;
blueImage(binaryImage) = 0;
One problem could be visualizing your cells if they are way less than 65535, but anyway it should work. But if they brightest thing below the threshold is like 2000 and you set it to 65535, then you could see the red but might not see the stuff below the threshold.
You might want to do
redImage(binaryImage) = max(grayImage(:));
so that the red is about the same brightness as the brightest thing in your image to avoid that problem, but then the red is not all the same intensity.
Another option is to not change the image at all but just display it with the proper colormap to change just the way it's displayed.
Yep, intmax(class(grayImage)) seems fine. Thanks!

로그인 to comment.


Answer by gonzalo Mier on 28 Jun 2019

You can use uint8 to saturate all the values between 0 and 255.
You can test it as:
gray_as_RGB = uint8(repmat(grayimg, [1 1 3]));
You can leave the other part of the code without more changes

  댓글 수: 1

No, I don’t want to change the values. How do I find the highest possible value in the range I am in.

로그인 to comment.



Translated by