How to convert the image into a binary image?

조회 수: 1 (최근 30일)
Manoj Kumar
Manoj Kumar 2014년 7월 21일
댓글: Image Analyst 2016년 11월 11일
HI, I have a GUI in which I can select multiple regions of interest for an image. I have selected 3 regions in the image. I tried to get the binary image consisting of 3 regions. But unfortunately, I could get only the last(third) region that i selected.
These are the selected regions of interest in the image one by one using the following code.
global segmentedImage
if(size(segmentedImage,3)==3)
im6=rgb2gray(segmentedImage);
else
im6=imadjust(segmentedImage);
end
hFig=figure;
finalImage = segmentedImage;
finalImage(:,:) = 0;
choice = 1; % =2 represents it is done
while(choice == 1)
% Display the image in Grayscale to draw the contour
figure;imshow(im6);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% axis on;
message = sprintf('Draw CONTOUR line.\nLeft click mouse and hold to begin drawing.\nRelease the mouse button to finish');
uiwait(msgbox(message));
hFH = imfreehand();
% Create a binary image ("mask") from the ROI object.
binaryImage2 = hFH.createMask();
subplot(1,2,1);
imshow(im6);
title('Original Image','FontSize',fontSize);
% Get coordinates of the boundary of the freehand drawn region.
structBoundaries = bwboundaries(binaryImage2);
xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
x = xy(:, 2); % Columns.
y = xy(:, 1); % Rows.
subplot(1, 2, 1); % Plot over original image.
hold on; % Don't blow away the image.
plot(x, y, 'LineWidth', 2);
drawnow;
% Mask the image using bsxfun() function
maskedImage = bsxfun(@times, im6, cast(binaryImage2, class(im6)));
imgselected = im2uint8(maskedImage);
finalImage = imadd(finalImage,imgselected);
subplot(1,2,2);
imshow(maskedImage);
% axis on;
title('Regions to be included', 'FontSize', fontSize);
promptMessage = sprintf('Do you want to accept this,\nor draw more regions it?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Accept', 'More', 'Accept');
if strcmpi(button, 'More')
im6=im6-maskedImage;
else
break;
end
close(gcf);
end
close(hFig);
% End addition
%figure;imshow(finalBinaryImage);
axes(handles.axes2);
imshow(finalImage);
This is the binary Image that i got using the following code:
segmentedImageMask=binaryImage-binaryImage2;
Where "binaryImage in the above code" is the binary image of the original image . Here is the attachment of the binary image.
I tried to remove the binary image of the multiple regions(binaryImage2) with the binary image of the original segmented Image(binaryImage). But I couldn't get the appropriate result.
Can you please help me out in getting all the three regions in the binary image.
Thanks...

채택된 답변

Image Analyst
Image Analyst 2014년 7월 21일
You need to initialize a binary image and then OR in the ones from the loop. Before the loop
finalBinaryImage = false(size(im6));
Then in the loop
finalBinaryImage = finalBinaryImage | binaryImage2;
When the loop is done, finalBinaryImage will have all the regions you drew.
  댓글 수: 3
Image Analyst
Image Analyst 2014년 7월 21일
What image do you want as the final output? A gray image with black holes burned into it? A binary image with all the regions you outlined? Three separate binary images?
Manoj Kumar
Manoj Kumar 2014년 7월 22일
I need a binary image with all the regions outlined...

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

추가 답변 (2개)

Peyman Obeidy
Peyman Obeidy 2016년 11월 11일
Greeting I am also interested in doing the same things, did you manage to get all your 3 ROIs on the image. If so, may I get the complete code. This will save me a lot of time.
Cheers Peyman
  댓글 수: 3
Walter Roberson
Walter Roberson 2016년 11월 11일
Peyman Obeidy comments to Image Analyst:
were is the start point, I am not use to GUI at all
Image Analyst
Image Analyst 2016년 11월 11일
Peyman, the code is in the original poster's question at the very top. If, some some unreasonable reason, you're unable to use a GUI, then you can't do this at all, because imfreehand() requires a GUI so you can draw on an image.

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


Peyman Obeidy
Peyman Obeidy 2016년 11월 11일
Dear Image analyser,
what I would like to do is to a- open an image b- select multiple regions c-display selected region on the original image d- calculate the area of each region
for a-b, I found "Multi ROI/Mask Editor Class (https://au.mathworks.com/matlabcentral/fileexchange/31388-multi-roi-mask-editor-class)" GUI, but I can't get the binary and original image as and output into the workspace.
Maybe I get some help with this, I can manage to do the 'c-d' part by myself.

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by