Conversion of grayscale to binary Image
显示 更早的评论
when a dataset containing gray scale images are converted to binary images , few images are getting complemented i.e, few images with black as background and few with white as background.
I want all images to be in a uniform way, black background and white foreground.
how to use imcomplement() for selected images in a datasset? or is there anyother way?
i have attached few sample input and their corresponding binary images from the dataset.
Thankyou in advance
采纳的回答
There is an option for that in imbinarize to make sure you always get either the brighter thing or the darker thing:
BW = imbinarize(grayImage,'adaptive','ForegroundPolarity','dark','Sensitivity',0.4);
8 个评论
Thankyou sir for the response
but when i apply this command, segmentation of lung doesnt happen properly.
i have used the same code given by you for the segmentation.
i have attached the images.
Attach the original gray scale images, not screenshots of binary images, and your program
yes,sir,may be use
clc; clear all; close all;
grayImage = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/864875/CHNCXR_0639_1-input.png');
if ndims(grayImage) == 3
grayImage = rgb2gray(grayImage);
end
BW = ~imbinarize(grayImage,'adaptive','ForegroundPolarity','dark','Sensitivity',0.55);
figure; imshow(BW, []);
I have attached the code and grayImage sir.
folder = pwd; % 'E:\project\dataset'
fullFileName = fullfile(folder, 'CHNCXR_0639_1-input.jpg');
rgbImage=imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
subplot(2, 2, 1);
imshow(rgbImage);
axis('on', 'image')
title('Original RGB image')
% Convert to gray scale.
if numberOfColorChannels == 3
grayImage = rgb2gray(rgbImage);
else
% It's already gray scale.
grayImage = rgbImage;
end
% Resize.
grayImage = imresize(grayImage,[256,256]);
subplot(2, 2, 2);
imshow(grayImage);
axis('on', 'image')
title('Resized gray scale image')
% Binarize
binaryImage = imbinarize(grayImage,'adaptive','ForegroundPolarity','dark','Sensitivity',0.4);
subplot(2, 2, 3);
imshow(binaryImage);
binaryImage = imclearborder(binaryImage);
binaryImage = bwareafilt(binaryImage,2);
subplot(2, 2, 3);
imshow(binaryImage);
axis('on', 'image')
title('Lungs Only')
% Initialization Steps.
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;
folder = pwd; % 'E:\project\dataset'
fullFileName = fullfile(folder, 'CHNCXR_0639_1-input.png');
rgbImage=imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
subplot(2, 3, 1);
imshow(rgbImage);
axis('on', 'image')
title('Original RGB image', 'FontSize', fontSize)
impixelinfo;
% Convert to gray scale.
if numberOfColorChannels == 3
grayImage = rgb2gray(rgbImage);
else
% It's already gray scale.
grayImage = rgbImage;
end
subplot(2, 3, 2);
imhist(grayImage);
grid on;
title('Histogram of gray scale image', 'FontSize', fontSize)
% Resize.
grayImage = imresize(grayImage,[256,256]);
subplot(2, 3, 3);
imshow(grayImage);
axis('on', 'image')
title('Resized gray scale image', 'FontSize', fontSize)
% Binarize
% Determine threshold
lowThreshold = 0;
highThreshold = 189;
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
% [lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, grayImage);
binaryImage = grayImage >= lowThreshold & grayImage <= highThreshold;
% binaryImage = imbinarize(grayImage,'adaptive','ForegroundPolarity','dark','Sensitivity',0.4);
% binaryImage = imbinarize(grayImage);
subplot(2, 3, 4);
imshow(binaryImage);
title('Binary Image', 'FontSize', fontSize)
binaryImage = imclearborder(binaryImage);
binaryImage = bwareafilt(binaryImage,2);
binaryImage = bwconvhull(binaryImage, 'objects');
subplot(2, 3, 5);
imshow(binaryImage);
axis('on', 'image')
title('Lungs Only', 'FontSize', fontSize)
% Plot the borders of all the blobs in the overlay above the original grayscale image
% using the coordinates returned by bwboundaries().
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
subplot(2, 3, 6);
imshow(grayImage); % Optional : show the original image again. Or you can leave the binary image showing if you want.
% Here is where we actually get the boundaries for each blob.
boundaries = bwboundaries(binaryImage);
% boundaries is a cell array - one cell for each blob.
% In each cell is an N-by-2 list of coordinates in a (row, column) format. Note: NOT (x,y).
% Column 1 is rows, or y. Column 2 is columns, or x.
numberOfBoundaries = size(boundaries, 1); % Count the boundaries so we can use it in our for loop
% Here is where we actually plot the boundaries of each blob in the overlay.
hold on; % Don't let boundaries blow away the displayed image.
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k}; % Get boundary for this specific blob.
x = thisBoundary(:,2); % Column 2 is the columns, which is x.
y = thisBoundary(:,1); % Column 1 is the rows, which is y.
plot(x, y, 'r-', 'LineWidth', 2); % Plot boundary in red.
end
hold off;
caption = sprintf('%d Outlines, from bwboundaries()', numberOfBoundaries);
fontSize = 15;
title(caption, 'FontSize', fontSize);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.

thankyou sir
If you want the shape to better hug/follow the binary image, instead of using the convex hull, you could fill the blobs and use activecontour. See attached demo.
ANUSHA H P
2022-1-21
编辑:ANUSHA H P
2022-1-21
yes sir, it was of grt help ,thankyou.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
