필터 지우기
필터 지우기

gray to BW

조회 수: 3 (최근 30일)
Adam
Adam 2012년 4월 1일
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
Image Analyst
Image Analyst 2012년 4월 1일
I do. I know. I know a few ways. You used one way. Do you have another question?

댓글을 달려면 로그인하십시오.

채택된 답변

Adam
Adam 2012년 4월 3일
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.
  댓글 수: 3
Adam
Adam 2012년 4월 6일
Thank you very much
Image Analyst
Image Analyst 2012년 4월 6일
You're welcome. Usually though, you accept an answer by the person who helped you, not your own.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Image Analyst
Image Analyst 2012년 4월 1일
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);
uiwait(warndlg(errorMessage));
return;
end
end
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);
bar(pixelCount);
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
Adam 2012년 4월 2일
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)
  댓글 수: 2
Image Analyst
Image Analyst 2012년 4월 2일
I don't know what that code has to do with image thresholding. Where did you upload your image?
Walter Roberson
Walter Roberson 2012년 4월 3일
http://www.mathworks.com/matlabcentral/answers/7924-where-can-i-upload-images-and-files-for-use-on-matlab-answers

댓글을 달려면 로그인하십시오.


Adam
Adam 2012년 4월 3일
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 2012년 4월 3일
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.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
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');
uiwait(msgbox(message));
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);
imshow(binaryImage);
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);
imshow(burnedImage);
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);
imshow(blackMaskedImage);
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);
imshow(insideMasked);
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);
imshow(croppedImage);
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));
msgbox(message);

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by