I need help please to obtain histogram clonocolour values of TIF type photos. I can do this separately but i am struggling to do this with multiple images at a time.
1 view (last 30 days)
Show older comments
I am using cancer images to observe the pixel values and obtain clonocolour values. I am able to run the code seperatley but do not know how to upload multiple images or files for these TIF type images so that i can obtain multiple histogram values at once rather than having to load these images seperatley.
Can anyone help with this URGENTLY PLZ?!?
4 Comments
Image Analyst
on 2 Sep 2024
I still don't know what clonocolour is. It looks like it's the integrated gray value of the entire image, except ignoring graylevels 0 and 1. How did you come up with that term for it?
I also can't tell what you're doing with the flask region in the image, and all that morphology stuff, because you keep forgetting to upload an image. That might, or might not, be the optimal algorithm to segment the flask and get the integrated gray value but I won't know until you supply an image. Or better, 3 images: a best possible image, a typical image, and a worst case image.
By the way when I read an image from a file into MATLAB, I call it "reading", not uploading. I always think of uploading and downloading as being a transfer between some other computer on the network (for example a web server), and your local computer.
Answers (2)
dpb
on 2 Sep 2024
Moved: dpb
on 2 Sep 2024
" This was the skeleton used in order to gather the pixel intensity values from the histogram from one image at a time."
Encapsulate the code in a function and then call that function in a loop as outlined in the FAQ link. I particularly favor the
d=dir(fullfile('DirectoryLocation','WildCardMatching*FileName.ext')); % return list of files matching wildcard
Clonocolour=size(d); % create array for results
for i=1:numel(d) % iterate over the files
Clonocolour(i)=clonocolour(fullfile(d(i).folder,d(i).name)); % call your function, save the values
end
that would put whatever you choose to return from the function you created ...
function Clonocolour=clonocolour(image)
RGB = imread(image)
%imread(IMAGE)
%subtraction of inside flask background
se = strel('sphere',2);
redchannel = RGB(:,:,1);
mask = imdilate(redchannel>150,se);
PO = bsxfun(@times, RGB, cast(~mask, 'like', RGB));
figure(2)
imshow(PO)
G_PO = im2gray(PO);
%converts image to Greyscale and shows
G_RGB = im2gray(RGB);
figure(3)
imshow(G_RGB)
%converts greyscale image to binary
BW = imbinarize(G_RGB);
figure(4)
a1 = im2bw(G_RGB)
%fills in holes in the binary image (area of dark pixels surrounded by light pixels)
a2 = imfill(a1, 'holes');
imshow(a2)
%expands boundaries using a disk of radius 500 pixels
se2 = strel('disk',700,4);
b3b = imerode(a2,se2);
%Flask without border and background and in greyscale
a3 = G_PO .* uint8(b3b);
figure(5)
imshow(a3)
%Creates histogram of greyscaled image (from selected area) and shows
[Data,BinEdges] = imhist(a3);
BinEdgesf = BinEdges(2:256,1);
Dataf = Data(2:256,1);
bar(BinEdgesf,Dataf)
%Creation of arbitary value
hist = BinEdgesf.*Dataf;
Clonocolour = sum(hist);
end
The above will reuse the same figures for each pass because you hardcoded figure numbers in the script; if you need to save these as well, you'll need to add code to do so as well as whether you also need to save the actual histogram data or not, but those are just details.
0 Comments
Image Analyst
on 3 Sep 2024
Rather than try to figure out how many cells are there, I'd just simply compute the area fraction of purple. It will correlate with whatever you're trying to correlate with probably just as well as if you were able to count individual cells. You can use the color thresholder app on the Apps tab of the tool ribbon to generate segmentation code. Here's a full demo:
% Demo by Image Analyst
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 = 16;
markerSize = 20;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
folder = [];
baseFileName = 'flask purple.png';
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, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis('on', 'image');
title('Original RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Update 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)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
g.Name = 'Demo by Image Analyst';
g.NumberTitle = 'off';
drawnow;
%--------------------------------------------------------------------------------------------
% MASK THE IMAGE.
[mask,maskedRGBImage] = createMask(rgbImage);
subplot(2, 2, 2);
imshow(mask)
impixelinfo;
axis('on', 'image');
title('Purple Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Mask the image with the new mask by multiplying each channel by the mask.
% Invert the mask with ~ so that the red will turn to black.
maskedRgbImage = rgbImage .* cast(mask, 'like', rgbImage);
% Get the are fraction.
areaFraction = sum(mask, 'all') / numel(mask)
% Show the masked image with white now turned into black.
subplot(2, 2, 3);
imshow(maskedRgbImage)
impixelinfo;
axis('on', 'image');
caption = sprintf('Masked RGB Image. Area fraction of purple = %.4f', areaFraction);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%===============================================================================================
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 03-Sep-2024
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.604;
channel1Max = 0.873;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.127;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 0.849;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
0 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!