Conversion of grayscale to binary Image
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
채택된 답변
Image Analyst
2022년 1월 17일
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
ANUSHA H P
2022년 1월 18일
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.

ANUSHA H P
2022년 1월 20일
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에서 Contrast Adjustment에 대해 자세히 알아보기
참고 항목
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)
