MATLAB Answers

color thresholdig convert rgb to binary consider the value of red green and bue

28 views (last 30 days)
izyan hanum
izyan hanum on 16 Mar 2015
Commented: izyan hanum on 17 Mar 2015
how to convert this rgb to grayscale and to binary consider the value of red green and blue. can anyone help me? because my coding have some error :( i just convert the image to grayscale and binary not consider the value of rgb, i dont know how to consider the value of rgb

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 16 Mar 2015
Try thresholding in HSV color space to get purple blobs but not black blobs or yellow background.
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 = 14;
%===============================================================================
% Read in a color demo image.
folder = 'D:\Temporary Stuff';
baseFileName = 'USM16.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(3, 3, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
% Extract the individual red, green, and blue color channels.
hsv = rgb2hsv(rgbImage);
hImage = hsv(:, :, 1);
sImage = hsv(:, :, 2);
vImage = hsv(:, :, 3);
% Display the images
subplot(3, 3, 4);
imshow(hImage, []);
title('Hue Image', 'FontSize', fontSize);
subplot(3, 3, 5);
imshow(sImage, []);
title('Saturation Image', 'FontSize', fontSize);
subplot(3, 3, 6);
imshow(vImage, []);
title('Value Image', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(hImage);
subplot(3, 3, 7);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of Hue image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(sImage);
subplot(3, 3, 8);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of Saturation image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(vImage);
subplot(3, 3, 9);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of Value image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Manually threshold the images usinghttp://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image
% [lowThreshold, highThreshold] = threshold(.1, .9, vImage);
% Get a binary image by thresholding and combining the different channels.
hueBinary = hImage > 0.2 | hImage < 0.1;
saturationBinary = sImage > 0.22;
valueBinary = vImage > 0.28 & vImage < 0.9;
binaryImage = hueBinary & saturationBinary & valueBinary;
% Clear up by getting rid of particles less than 400 pixels in area.
binaryImage = bwareaopen(binaryImage, 400);
% Fill any holes in the blobs.
binaryImage = imfill(binaryImage, 'holes');
subplot(3, 3, 2);
imshow(binaryImage, []);
axis on;
title('Cells Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage, numberOfBlobs] = bwlabel(binaryImage, 8);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
subplot(3, 3, 3);
imshow(coloredLabelsImage);
title('Labeled Image', 'FontSize', fontSize);
% Get all the blob properties.
blobMeasurements = regionprops(labeledImage, 'all')
numberOfBlobs = size(blobMeasurements, 1)

  3 Comments

Image Analyst
Image Analyst on 17 Mar 2015
I've see that "upside down" font once before, and even then it showed up on only 1 of the 3 computers the app was installed on. The solution was to change the renderer to one of the other options. I think it was zbuffer that worked. But now when I open that GUI in R2014b, it doesn't look like zbuffer is an option anymore and my renderer is blank. The only options seem to be painters and opengl. Anyway, try one of those. Before the first call to subplot, put this
figure;
set(gcf, 'Renderer', 'Painters'); % or zbuffer or opengl.
If none of the 3 options work, then call the Mathworks.
izyan hanum
izyan hanum on 17 Mar 2015
i use matlab2013a. yes right. when i call the code in first line its ok like picture below. thank you so much.

Sign in to comment.

More Answers (1)

Sean de Wolski
Sean de Wolski on 17 Mar 2015
Use the colorThresholder App which gives you the ability to adjust the boundaries

  2 Comments

izyan hanum
izyan hanum on 17 Mar 2015
i want adjust the font. the image is not same because i use different picture. but when i run the fon is like "mirror" i dont know where to adjust it. can you help me.?

Sign in to comment.

Sign in to answer this question.