How to convert the image into a binary image?

1 Ansicht (letzte 30 Tage)
Manoj Kumar
Manoj Kumar am 21 Jul. 2014
Kommentiert: Image Analyst am 11 Nov. 2016
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...

Akzeptierte Antwort

Image Analyst
Image Analyst am 21 Jul. 2014
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 Kommentare
Image Analyst
Image Analyst am 21 Jul. 2014
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 am 22 Jul. 2014
I need a binary image with all the regions outlined...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Peyman Obeidy
Peyman Obeidy am 11 Nov. 2016
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 Kommentare
Walter Roberson
Walter Roberson am 11 Nov. 2016
Peyman Obeidy comments to Image Analyst:
were is the start point, I am not use to GUI at all
Image Analyst
Image Analyst am 11 Nov. 2016
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.

Melden Sie sich an, um zu kommentieren.


Peyman Obeidy
Peyman Obeidy am 11 Nov. 2016
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.

Kategorien

Mehr zu Convert Image Type finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by