MATLAB Answers

gray to BW

13 views (last 30 days)
Adam on 1 Apr 2012
Hi all. I should question if anybody know how to convert a gray picture were stored up in us so that only the black pixels and white pixels.
D = im2bw(A);
  1 Comment
Image Analyst
Image Analyst on 1 Apr 2012
I do. I know. I know a few ways. You used one way. Do you have another question?

Sign in to comment.

Accepted Answer

Adam on 3 Apr 2012
This is an excellent demo. But I use a selected area of active contour segmentation. I do not know how I took advantage of it.
Output of the segmentation are xy coordinates.
Image Analyst
Image Analyst on 6 Apr 2012
You're welcome. Usually though, you accept an answer by the person who helped you, not your own.

Sign in to comment.

More Answers (4)

Image Analyst
Image Analyst on 1 Apr 2012
Basically is always comes down to thresholding. You do some processing so that you get an image that can be thresholded. That image may or may not be the original image. For example, to find edges in the image, you might run a standard deviation image on the original grayscale image and then threshold that standard deviation image:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% 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);
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Find edges
stdImage = stdfilt(grayImage);
% Display the image.
subplot(2, 2, 2);
imshow(stdImage, []);
title('Standard Deviation of Image', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCount grayLevels] = hist(stdImage, 256);
subplot(2, 2, 3);
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
grid on;
% Threshold to get binary image of where the edges are strong.
binaryImage = stdImage > 30;
% Display the image.
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Binary Image (where edges are)', 'FontSize', fontSize);

Adam on 2 Apr 2012
yes I chose an area of the picture and that area I would like to convert into white clusters of the same time, I lose the image information I need on the calculated area of assistance:
B = sum(sum(seg));
str = ['plocha ktera je označena je ', num2str(B), ' pixelu.'];
disp (str)
Walter Roberson
Walter Roberson on 3 Apr 2012

Sign in to comment.

Adam on 3 Apr 2012
ook. In gray images I choose the area. I want to convert that area into white.
1) I have a choose area. I do not know how to convert the area was all white.
2) white area need to calculate size I use:
B = sum(sum(seg));
str = ['plocha ktera je označena je ', num2str(B), ' pixelu.'];
disp (str)
Sorry, I use google.translate

Image Analyst
Image Analyst on 3 Apr 2012
To mask an area, follow my masking tutorial. You'll see when you run it how I turn the inside both white and black:
% Demo to have the user freehand draw an irregular shape over
% a gray scale image, have it extract only that part to a new image,
% and to calculate the mean intensity value of the image within that shape.
% By ImageAnalyst
% Change the current folder to the folder of this m-file.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
% Read in standard MATLAB gray scale demo image.
grayImage = imread('cameraman.tif');
% subplot(2, 3, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
hFH = imfreehand();
% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();
% Now make it smaller so we can show more images.
subplot(2, 3, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Display the freehand mask.
subplot(2, 3, 2);
axis on;
title('Binary mask of the region', 'FontSize', fontSize);
% Label the binary image and computer the centroid and center of mass.
labeledImage = bwlabel(binaryImage);
measurements = regionprops(binaryImage, grayImage, ...
'area', 'Centroid', 'WeightedCentroid');
area = measurements.Area
centroid = measurements.Centroid
centerOfMass = measurements.WeightedCentroid
% Calculate the area, in pixels, that they drew.
numberOfPixels1 = sum(binaryImage(:))
% Another way to calculate it that takes fractional pixels into account.
numberOfPixels2 = bwarea(binaryImage)
% Get coordinates of the boundary of the freehand drawn region.
structBoundaries = bwboundaries(binaryImage);
xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
x = xy(:, 2); % Columns.
y = xy(:, 1); % Rows.
subplot(2, 3, 1); % Plot over original image.
hold on; % Don't blow away the image.
plot(x, y, 'LineWidth', 2);
% Burn line into image by setting it to 255 wherever the mask is true.
burnedImage = grayImage;
burnedImage(binaryImage) = 255;
% Display the image with the mask "burned in."
subplot(2, 3, 3);
axis on;
caption = sprintf('New image with\nmask burned into image');
title(caption, 'FontSize', fontSize);
% Mask the image and display it.
% Will keep only the part of the image that's inside the mask, zero outside mask.
blackMaskedImage = grayImage;
blackMaskedImage(~binaryImage) = 0;
subplot(2, 3, 4);
axis on;
title('Masked Outside Region', 'FontSize', fontSize);
% Calculate the mean
meanGL = mean(blackMaskedImage(binaryImage));
% Put up crosses at the centriod and center of mass
hold on;
plot(centroid(1), centroid(2), 'r+', 'MarkerSize', 30, 'LineWidth', 2);
plot(centerOfMass(1), centerOfMass(2), 'g+', 'MarkerSize', 20, 'LineWidth', 2);
% Now do the same but blacken inside the region.
insideMasked = grayImage;
insideMasked(binaryImage) = 0;
subplot(2, 3, 5);
axis on;
title('Masked Inside Region', 'FontSize', fontSize);
% Now crop the image.
leftColumn = min(x);
rightColumn = max(x);
topLine = min(y);
bottomLine = max(y);
width = rightColumn - leftColumn + 1;
height = bottomLine - topLine + 1;
croppedImage = imcrop(blackMaskedImage, [leftColumn, topLine, width, height]);
% Display cropped image.
subplot(2, 3, 6);
axis on;
title('Cropped Image', 'FontSize', fontSize);
% Put up crosses at the centriod and center of mass
hold on;
plot(centroid(1)-leftColumn, centroid(2)-topLine, 'r+', 'MarkerSize', 30, 'LineWidth', 2);
plot(centerOfMass(1)-leftColumn, centerOfMass(2)-topLine, 'g+', 'MarkerSize', 20, 'LineWidth', 2);
% Report results.
message = sprintf('Mean value within drawn area = %.3f\nNumber of pixels = %d\nArea in pixels = %.2f\nCentroid at (x,y) = (%.1f, %.1f)\nCenter of Mass at (x,y) = (%.1f, %.1f)\nRed crosshairs at centroid.\nGreen crosshairs at center of mass.', ...
meanGL, numberOfPixels1, numberOfPixels2, ...
centroid(1), centroid(2), centerOfMass(1), centerOfMass(2));


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!