I Can not convert image to binary image !

2 次查看(过去 30 天)
Hi
I'm trying to convert this image to binary image
This is the code:
OriginalImage = imread('t1.jpg');
subplot(2,2,1);
imshow(OriginalImage);
g = rgb2gray(OriginalImage);
subplot(2,2,2);
imshow(g);
BW = im2bw(g);
subplot(2,2,3);
imshow(BW);
The background should be black but here is the result !!
Could you please help me !

回答(1 个)

Image Analyst
Image Analyst 2018-2-15
Use imbinarize() instead.
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 = 16;
%===============================================================================
% Read in a demo image.
folder = pwd;
baseFileName = 't9.jpg';
% Get the full filename, with path prepended.
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
originalImage = imread(fullFileName);
% Display the image.
subplot(2, 2, 1);
imshow(originalImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(originalImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(originalImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
else
grayImage = rgbImage; % Not really an RGB image.
end
% Display the image.
subplot(2, 2, 2);
imshow(grayImage);
title('Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
%------------------------------------------------------------------------------
% 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')
% Binarize the image.
binaryImage = imbinarize(grayImage);
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
  2 个评论
Han
Han 2018-2-15
Thank you ..
I used imbinarize() but when I try this image is not working
Image Analyst
Image Analyst 2018-2-15
It does work in that it doesn't throw an error. Exactly what would you like for the output in this case of 3 different intensity ranges? imbinarize() divides the image into two ranges, not three. Perhaps you'd like multithresh() instead of imbinarize, or simply do it yourself:
binaryImage = grayImage ~= backgroundGrayLevel;

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by