How to threshold and obtain RGB and reflactance values (max, min, mean) of MULTIPLE images?
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
JoanManBar
il 19 Ago 2017
Modificato: per isakson
il 2 Dic 2017
Hi! I have several '.tif' photos of seeds that I would like to threshold to obtain RGB values (max, min, mean) and reflectance values (max, min, mean) of individual images and, if it's possible, individual seeds. I used to do this on ImageJ but I need to learn it on MatLab.
I manually set up the channels thresholds with the Color Thresholder app and generated a function, but honestly I have not had success using this function in a loop. I would eventually like to have a .csv file with columns: "image, seed, red_max, red_min, red_mean, green_max, green_min, green_mean, blue_max, blue_min, blue_mean..." and the same for reflectance.
This is what my images look like.
I'd really appreciate any help you guys could provide me!
Cheers!
Joan.
0 Commenti
Risposta accettata
Image Analyst
il 19 Ago 2017
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 = 25;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'Seeds.jpg';
% Get the full filename, with path prepended.
folder = pwd
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
%===============================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original image.
subplot(1, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% 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;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Threshold to create a mask.
binaryImage = redChannel > 90;
% Get rid of small noise blobs.
binaryImage = bwareafilt(binaryImage, [100, inf]);
% Erode them a bit to separate touching ones and get away from light shadows.
se = strel('disk', 7, 0);
binaryImage = imerode(binaryImage, se);
% Display the mask image.
subplot(1, 2, 2);
imshow(binaryImage, []);
axis on;
caption = sprintf('Seed Mask');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
[labeledImage, numSeeds] = bwlabel(binaryImage);
propsR = regionprops(labeledImage, redChannel, 'MeanIntensity', 'PixelValues');
propsG = regionprops(labeledImage, greenChannel, 'MeanIntensity', 'PixelValues');
propsB = regionprops(labeledImage, blueChannel, 'MeanIntensity', 'PixelValues');
allMeansR = [propsR.MeanIntensity];
allMeansG = [propsG.MeanIntensity];
allMeansB = [propsB.MeanIntensity];
overallSeedMeanR = mean(allMeansR);
overallSeedMeanG = mean(allMeansG);
overallSeedMeanB = mean(allMeansB);
% Get min and max of each seed for each color channel
for k = 1 : numSeeds
allMinsR(k) = min(propsR(k).PixelValues);
allMinsG(k) = min(propsG(k).PixelValues);
allMinsB(k) = min(propsB(k).PixelValues);
allMaxsR(k) = max(propsR(k).PixelValues);
allMaxsG(k) = max(propsG(k).PixelValues);
allMaxsB(k) = max(propsB(k).PixelValues);
end
% Prepare data for csvwrite
% "image, seed, red_max, red_min, red_mean, green_max, green_min, green_mean, blue_max, blue_min, blue_mean..."
data = [(1:numSeeds)', allMaxsR', allMinsR', allMeansR', allMaxsG', allMinsG', allMeansG', allMaxsB', allMinsB', allMeansB']
% csvwrite can't handle strings, only numbers. If you want the filenames on each line, use xlswrite().
% Get the name of the file that the user wants to save.
% Note, if you're saving an image you can use imsave() instead of uiputfile().
startingFolder = userpath; % Or "pwd" or wherever you want.
defaultFileName = fullfile(startingFolder, '*.csv');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
csvwrite(filename, data);
1 Commento
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Blue in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!