Hi dear;
I have a binary image that contains an object (some times a lot of objects), I want to reshape the object as showing in the image 1 to the one as showing in the image 2. My goal is to have an object tha has a straight boundaries.
Any help and suggestion will be appreciated.

댓글 수: 4

Walter Roberson
Walter Roberson 2016년 1월 29일
Do you mean like calculating the amount of white pixels and creating a new image with the same size and width but with all of the white pixels in the rectangular center, as long as there are the same number? If so then what should be done if the number of white pixels happens to be a prime and so cannot be factored into a rectangle?
Image Analyst
Image Analyst 2016년 1월 29일
And, if so, why do you think you need to do this, and would you want/need any black padding around the rectangle?
Walter Roberson
Walter Roberson 2016년 1월 29일
Maybe you would like to skeletonize, remove spurs, and then dilate out again??
Maa Kari
Maa Kari 2016년 1월 31일
편집: Maa Kari 2016년 1월 31일
Hi Mr Image analyst
sorry I didnt pay attention to those coments
no i dont want any black padding around the rectangle, the resut is like what you get bellow for the simple binary image, but for a complex binary image my goal is to have the result as in the http://www.mathworks.com/matlabcentral/answers/265581#comment_339054
for clarification the binary image is after a segmentation of initial image that contains a lot of object and our goal is to have a straight boundaries for the connected component in the binary image i hope that my question is clear and I'm available for any clarification
Thank you very much

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

 채택된 답변

Image Analyst
Image Analyst 2016년 1월 28일
편집: Image Analyst 2016년 1월 28일

1 개 추천

Use
[labeledImage, numBlobs] = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'BoundingBox');
then for each blob in a for loop over all blobs, get the bounding box and get the bounding rows and columns,
for k = 1 : numBlobs
xLeft = ceil(measurements(k).BoundingBox(1));
% etc.
binaryImage(yTop:yBottom, xLeft:xRight) = true;
end
Let me know if you can't figure out the rest.

댓글 수: 7

Maa Kari
Maa Kari 2016년 1월 29일
편집: Maa Kari 2016년 1월 29일
hi;
thanks for the ansewer but i cant figure out the rest of the code
here is the binary image
can you please help me
You simply had to compute the top and bottom row and right column - pretty trivial. Anyway, here's the whole thing - a full blown demo.
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 = 20;
%===============================================================================
% Read in a demo image.
folder = pwd
baseFileName = 'image1.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
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
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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 = grayImage > 128;
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Label the image.
[labeledImage, numBlobs] = bwlabel(binaryImage);
% Measure bounding box(es).
measurements = regionprops(labeledImage, 'BoundingBox');
% Then for each blob in a for loop over all blobs,
% get the bounding box and get the bounding rows and columns,
for k = 1 : numBlobs
xLeft = ceil(measurements(k).BoundingBox(1));
yTop = ceil(measurements(k).BoundingBox(2));
width = measurements(k).BoundingBox(3);
height = measurements(k).BoundingBox(4);
xRight = ceil(xLeft + width);
yBottom = ceil(yTop + height);
% etc.
binaryImage(yTop:yBottom, xLeft:xRight) = true;
end
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
title('Reshaped Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
Walter Roberson
Walter Roberson 2016년 1월 30일
Certainly not an interpretation of the Question that I had considered, but it does make some sense.
Image Analyst
Image Analyst 2016년 1월 30일
I hadn't thought of your interpretation either - I just went by the image she posted, which looks like it has more white pixels and is the bounding box. However the tick marks are not shown so we can't be sure of how many pixels are actually there in the box. When you brought up another, equally valid, interpretation based on the ambiguous wording, she never responded to that but instead asked for expanded code on the bounding box method so I figured that is what was wanted.
Actually though, I don't think either of these is really needed. I never do this kind of thing when I'm processing image so I doubt it is needed here either. I would guess she thinks it's a needed intermediate step towards the actual goal, but in reality it's probably not needed. For example, if you just wanted to see the bounding boxes, you could plot them in the overlay with plot(). Or if you wanted to crop you'd just use imcrop. I don't see any need to construct a binary image of them. Hopefully she'll clarify with a compelling reason for why it's needed, or else give the actual objective so we can decide on a better approach.
Maa Kari
Maa Kari 2016년 1월 30일
thanks you very much Image Analyst for the ansewer
now its work and gives the good result but when I change the image to an other one like the atteched one i dont get the result nedeed so i think that in this case i must skelitize the image and remove spurs to get thin and straith or a curve line then dilate out again as Walter Roberson says, if there is an method please let know it
thanks a lot
inpute image
image result
Image Analyst
Image Analyst 2016년 1월 30일
Aha, we knew it! You didn't actually want what you asked for (and so all my prior effort went to waste, perhaps). Now, with this image it looks like you actually want some kind of "envelope" of the blob. So it seems like you want activecontour(). This time I'll simply attach my canned demo and you can adapt it yourself.
Maa Kari
Maa Kari 2016년 1월 30일
thank you very mutch your first prosition is very helpful for me, and thank you for the answer and for your efforts it is really appreciated i will try the activecontour().

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

추가 답변 (0개)

질문:

2016년 1월 28일

편집:

2016년 1월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by