how to connect coordinate points?
Mostrar comentarios más antiguos
I have used bwlabel to label the connected components of a picture.Then I have used find() to have the coordinates for the pixels in object 1 and saved it in [r c].
Now is there any way to use those co-ordinate and draw them again to reconstruct the object?
My code is something like that::
img = imread('global1.jpg');
b_img = im2bw(img);
[m,n] = bwlabel(b_img,4);
[r c] = find(m==1);
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 4 de Oct. de 2012
Editada: Image Analyst
el 5 de Oct. de 2012
The usual Mathworks-recommended way is to use ismember():
keeperBlobsImage = ismember(labeledImage, blobNumberToKeep);
where blobNumberToKeep can be either a single number of a vector of numbers that you want to keep. See my BlobsDemo for a demo http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 - I extract out two types of coins from the standard MATLAB demo image.
22 comentarios
Sayak
el 5 de Oct. de 2012
Image Analyst
el 5 de Oct. de 2012
Sorry - that was the answers profile. I've corrected it to my File Exchange. Look for "Image Segmentation Tutorial - BlobsDemo". http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
Sayak
el 6 de Oct. de 2012
Image Analyst
el 6 de Oct. de 2012
Editada: Image Analyst
el 6 de Oct. de 2012
No, that's the inefficient, complicated, non-MATLAB way of doing it. You can do all that with just one line:
blob1 = ismember(L, 1);
instead of using mask, zeros(), find(), im2bw(), the "for" loop, the "while" loop, the "if" statement, etc. All that is totally unnecessary and just makes it way more complicated than it needs to be.
Here, here's a full-blown demo, using your image of 3 blobs, where I extract each of the 3 blobs one at a time. Note that if you label with 8 connectivity instead of 4 connectivity, you'd have just 2 blobs since it would consider blobs touching at the pixel corners to be part of the same blob, while 4 connectivity considers them as separate blobs.
BW = [...
1 1 1 0 0 0 0 0
1 1 1 0 1 1 0 0
1 1 1 0 1 1 0 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 1 1 0
1 1 1 0 0 0 0 0];
subplot(2, 3,1);
imshow(BW, 'InitialMagnification', 800);
title('Original Binary Image', 'FontSize', 20);
% Label the image with 4 connectivity
[L n]= bwlabel(BW, 4);
% Note: 8 connectivity would get you two blobs.
% Display the three blobs.
subplot(2, 3, 2);
imshow(L, [], 'InitialMagnification', 800);
title('Labeled Image', 'FontSize', 20);
coloredLabels = label2rgb (L, 'hsv', 'k', 'shuffle'); % pseudo random color labels
subplot(2, 3, 3);
imshow(coloredLabels, []);
title('Labeled Image, with colored labels.', 'FontSize', 16);
%======== KEY PART BELOW =======================
% Extract the two blobs.
blob1 = ismember(L, 1);
blob2 = ismember(L, 2);
blob3 = ismember(L, 3);
%======== KEY PART ABOVE =======================
% Display the blobs.
subplot(2, 3, 4);
imshow(blob1, 'InitialMagnification', 800);
title('Blob 1', 'FontSize', 20);
subplot(2, 3, 5);
imshow(blob2, 'InitialMagnification', 800);
title('Blob 2', 'FontSize', 20);
subplot(2, 3, 6);
imshow(blob3, 'InitialMagnification', 800);
title('Blob 3', 'FontSize', 20);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
Thanks for the comment about BlobsDemo. I thought it was easy for beginners since I had more comments than lines of code. Since it's designed for beginners, I'll have to take a look at it to see how it can be made even more instructive.
Sayak
el 7 de Oct. de 2012
Image Analyst
el 7 de Oct. de 2012
No, you can't do that. ismember returns an entire image, where as blob(i) can take only a single number, not a whole array of numbers. I'm not sure why you say that you can't get the two shapes. You can. Just do this:
shape1 = ismember(L, 1); % Left hand side shape = man.
shape2 = ismember(L, 2); % Right hand side shape = woman.
Those are integer/grayscale/labeled images. If you want binary images, you can just compare to zero.
binaryShape1 = ismember(L, 1) > 0;
binaryShape2 = ismember(L, 2) > 0;
Image Analyst
el 7 de Oct. de 2012
OK, in case my latest comment is still not enough, I downloaded your image and applied my code to it. Here I extract each shape:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 14;
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Users\Mark\Documents\Temporary';
% Read in a standard MATLAB color demo image.
baseFileName = 'man_woman.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
grayImage = rgbImage(:,:,2); % Get green channel as a gray scale image.
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Gray Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Binarize it.
binaryImage = grayImage < 128;
subplot(2, 3, 2);
imshow(binaryImage);
title('Original Binary Image', 'FontSize', 20);
% Label the image with 4 connectivity
[L n]= bwlabel(binaryImage, 4);
% Note: 8 connectivity would get you two blobs.
% Display the three blobs.
subplot(2, 3, 3);
imshow(L, [], 'InitialMagnification', 800);
title('Labeled Image', 'FontSize', 20);
coloredLabels = label2rgb (L, 'hsv', 'k', 'shuffle'); % pseudo random color labels
subplot(2, 3, 4);
imshow(coloredLabels, []);
title('Labeled Image, with colored labels.', 'FontSize', 16);
%======== KEY PART BELOW =======================
% Extract the two blobs.
blob1 = ismember(L, 1);
blob2 = ismember(L, 2);
%======== KEY PART ABOVE =======================
% Display the blobs.
subplot(2, 3, 5);
imshow(blob1, 'InitialMagnification', 800);
title('Blob 1', 'FontSize', 20);
subplot(2, 3, 6);
imshow(blob2, 'InitialMagnification', 800);
title('Blob 2', 'FontSize', 20);
Sayak
el 8 de Oct. de 2012
Sayak
el 8 de Oct. de 2012
Sayak
el 10 de Oct. de 2012
Image Analyst
el 10 de Oct. de 2012
No background subtraction is needed, just thresholding.
Sayak
el 11 de Oct. de 2012
Image Analyst
el 11 de Oct. de 2012
You need to call regionprops. See my image segmentation demo in my File Exchange.
Sayak
el 12 de Oct. de 2012
Image Analyst
el 12 de Oct. de 2012
This is completely different than a 2 gray level silhouette - this is a full color photograph. Please give the code you used to take that photo and arrive at the binary image of the three people.
Sayak
el 13 de Oct. de 2012
Editada: Image Analyst
el 14 de Oct. de 2012
Sayak
el 14 de Oct. de 2012
Image Analyst
el 14 de Oct. de 2012
No, sorry but that algorithm is way way too primitive to extract out the silhouettes of the people. You can't just find all the reddish pixels in the image and expect that you'll get each person, including their closing, which is not red and overlaps. YUV color space will be no better than hsv colorspace.
Sayak
el 15 de Oct. de 2012
Image Analyst
el 15 de Oct. de 2012
In general, watershed segmentation. See Steve Eddins's demo: http://blogs.mathworks.com/steve/2006/06/02/cell-segmentation/
Sayak
el 17 de Oct. de 2012
Image Analyst
el 17 de Oct. de 2012
Categorías
Más información sobre Image Arithmetic en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!