help with image threshold using bioformats
2 views (last 30 days)
Show older comments
Hi folks,
I am reading non-standard image files (not jpeg, png etc) into matlab, then using matlab to threshold these images based on RGB values.
However, all I've managed to do is to display the same image but in false colour...
Can you please help identify my error?
Thanks
data1 = bfopen(fullfile(sourcePathName, file1));
series1 = data1{1, 1};
series1_plane1 = series1{1, 1};
series1_plane2 = series1{2, 1};
series1_plane3 = series1{3, 1};
img1(:,:,1) = uint8(series1_plane1);
img1(:,:,2) = uint8(series1_plane2);
img1(:,:,3) = uint8(series1_plane3);
R = img1(:,:,1);
G = img1(:,:,2);
B = img1(:,:,3);
allR_Low = R>=148;
allR_High = R<=208;
allG_Low = G>=157;
allG_High = G<=255;
allB_Low = B>=193;
allB_High = B<=255;
allMask1 = img1;
allMask = allR_Low & allR_High & allG_Low & allG_High & allB_Low & allB_High;
allMask1(repmat(~allMask,[1 1 1])) = 0;
imshow(img1);
imshow(allMask1);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/644710/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/644715/image.png)
0 Comments
Accepted Answer
LO
on 7 Jun 2021
your threshold values are different among channels so it is not strange for the image to have "false colors".
if you think there is an issue with colormapping, see if this code by ImageAnalyst works for you. Then use the flipud function on the colormaps and see what happens inverting one channel at a time (perhaps the threshold has to be set differently?).
% Loads an RGB image and shows each color channel in monochrome, indexed, and RGB in its respective color.
clc; % Clear the command window.
clearvars;
close all
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
subplot(3, 4, 1);
rgbImage = imread('peppers.png');
imshow(rgbImage);
impixelinfo; % Let user mouse around and see pixel values and location.
fontSize = 12;
title('Original Image', 'FontSize', fontSize);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
%==================================================================================================
% RED CHANNEL : Show as gray scale, indexed, and RGB.
% Display red channel as monochrome.
cmap = [(0:255)', zeros(256, 1), zeros(256, 1)] / 256;
subplot(3, 4, 2);
imshow(redChannel);
impixelinfo; % Let user mouse around and see pixel values and location.
title('Red Channel as Gray Scale', 'FontSize', fontSize);
% Display red channel as indexed red.
cmap = [(0:255)', zeros(256, 1), zeros(256, 1)] / 256;
subplot(3, 4, 6);
imshow(redChannel, 'Colormap', cmap);
impixelinfo; % Let user mouse around and see pixel values and location.
title('Red Channel in Indexed Red', 'FontSize', fontSize);
colorbar;
% Display red channel as RGB by making the red channel the redChannel and the other channels all zero.
blackColorChannel = zeros(size(redChannel));
redRGB = cat(3, redChannel, blackColorChannel, blackColorChannel);
subplot(3, 4, 10);
imshow(redRGB);
impixelinfo; % Let user mouse around and see pixel values and location.
title('Red Channel as RGB', 'FontSize', fontSize);
%==================================================================================================
% GREEN CHANNEL : Show as gray scale, indexed, and RGB.
% Display green channel as monochrome.
cmap = [(0:255)', zeros(256, 1), zeros(256, 1)] / 256;
subplot(3, 4, 3);
imshow(greenChannel);
title('Green Channel Image as Gray Scale', 'FontSize', fontSize);
% Display green channel as green.
cmap = [zeros(256, 1), (0:255)', zeros(256, 1)] / 256;
subplot(3, 4, 7);
impixelinfo; % Let user mouse around and see pixel values and location.
imshow(greenChannel, 'Colormap', cmap);
title('Green Channel Image in Indexed Green', 'FontSize', fontSize);
colorbar;
% Display green channel as RGB by making the green channel the greenChannel and the other channels all zero.
greenRGB = cat(3, blackColorChannel, greenChannel, blackColorChannel);
subplot(3, 4, 11);
imshow(greenRGB);
impixelinfo; % Let user mouse around and see pixel values and location.
title('Green Channel as RGB', 'FontSize', fontSize);
%==================================================================================================
% BLUE CHANNEL : Show as gray scale, indexed, and RGB.
% Display blue channel as monochrome.
subplot(3, 4, 4);
imshow(blueChannel);
impixelinfo; % Let user mouse around and see pixel values and location.
title('Blue Channel Image as Gray Scale', 'FontSize', fontSize);
% Display blue channel as blue.
cmap = [zeros(256, 1), zeros(256, 1), (0:255)'] / 256;
subplot(3, 4, 8);
imshow(blueChannel, 'Colormap', cmap);
title('Blue Channel Image in Indexed Blue', 'FontSize', fontSize);
colorbar;
% Display blue channel as RGB by making the blue channel the blueChannel and the other channels all zero.
blueRGB = cat(3, blackColorChannel, blackColorChannel, blueChannel);
subplot(3, 4, 12);
imshow(blueRGB);
impixelinfo; % Let user mouse around and see pixel values and location.
title('Blue Channel as RGB', 'FontSize', fontSize);
%------------------------------------------------------------------------------
% 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')
0 Comments
More Answers (0)
See Also
Categories
Find more on Red 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!