Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Can nobody help me???
Date: Sat, 15 Nov 2008 03:45:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 114
Message-ID: <gflgjv$916$1@fred.mathworks.com>
References: <gfc8tj$4mb$1@aioe.org> <gfc9ou$33r$1@fred.mathworks.com> <2002cdb8-6508-4455-a065-825124e142c0@q26g2000prq.googlegroups.com> <gfh642$6gh$1@aioe.org>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1226720703 9254 172.30.248.35 (15 Nov 2008 03:45:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 15 Nov 2008 03:45:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1343420
Xref: news.mathworks.com comp.soft-sys.matlab:500946


Vicky <bonsai19@gmx.de> wrote in message <gfh642$6gh$1@aioe.org>...
> Hi ImageAnalyst,
> 
> Thank you for your answer.
> I saved it in tiff format but I still get rgb-values.
> 
> This is my code:
> 
> image1 = imread('org_001.bmp');
> image1 = im2bw(image1);
> L1 = bwlabel(image1, 8);
> imwrite(L1/255, 'image.tiff')
> 
> In each pixel is an rgb value like (8, 8, 8) or (12, 12, 12). But I want 
> that in each pixel is the label like 1, 2,...8,...12,...
> That means that in each pixel should be a single scalar value and not 
> the color.
> 
> What I'm doing wrong when saving?
> 
> Then I have a question. In my original rgb images are more than 256 
> colors. Is it still possible to create a monochrome image, so that each 
> color get a seperate label?
> 
> Can you please help me again?
> 
> Best regards,
> Vicky
> 
> 
> 
> 
> ImageAnalyst schrieb:
> > On Nov 11, 10:53 am, "David" <d...@bigcompany.com> wrote:
> >> Vicky <bonsa...@gmx.de> wrote in message <gfc8tj$4m...@aioe.org>...
> >>> Hallo there,
> >>> this is my code:
> >>> image1 = imread('image1.bmp');
> >>> image1 = im2bw(image1);
> >>> M = bwlabel(image1, 8);
> >>> imwrite(M, 'myImage.bmp');
> >>> How can I save an image (bmp), so that in each pixel is the element of
> >>> the matrice M?
> >>> If I save the matrice M with the command "imwrite" in each pixel of the
> >>> myImage.bmp is an rgb-value. But I don't want a rgb-value, I want the
> >>> label (element) of the matrice M.
> >>> How can I realize it?
> >>> Please I need help!!!
> >>> Best regards,
> >>> Vicky
> >> well, a bmp file is normally a simple rgb format.  read the help for imwrite and 
> >> see if one of the other formats may be better for what you want.  or you may just 
> >> want to save M to something else, like a mat file instead of as an image.- Hide 
> >>quoted text -
> >>
> >> - Show quoted text -
> > 
> > ---------------------------------------------
> > Correct.  Save in Tiff format instead.  It can take monochrome.
> > Regards,
> > ImageAnalyst
-------------------------------------------------------
OK, Vicky.  Run this code that I've posted before that will find blobs and label and measure them.  For you, I've adapted it to write out a tif image like you want, then i read it back in and display it with imtool so you can mouse around and inspect the values to verify that they are in fact the label values.
Regards,
ImageAnalyst

disp(' ');
disp('Running BlobsDemo.m...');
originalImage = imread('coins.png'); % Read in image
binaryImage = im2bw(originalImage, 0.4);       % Threshold to binary

subplot(3,2,1); imagesc(originalImage); colormap(gray(256)); title('Original Image');
subplot(3,2,2); imagesc(binaryImage); colormap(gray(256)); title('Binary Image');

labeledImage = bwlabel(binaryImage, 8);     % Label each blob so can do calc on it
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels

% Save the labeled image as a tif image.
imwrite(uint16(labeledImage), 'tif_labels.tif');
tifimage = imread('tif_labels.tif');
imtool(tifimage, []);  % Use imtool so she can inspect the values.

subplot(3,2,3); imagesc(labeledImage); title('Labeled Image');
subplot(3,2,4); imagesc(coloredLabels); title('Pseudo colored labels');

blobMeasurements = regionprops(labeledImage, 'all');   % Get all the blob properties.
numberOfBlobs = size(blobMeasurements, 1);

% bwboundaries returns a cell array, where each cell
% contains the row/column coordinates for an object in the image.
% Plot the borders of all the coins on the original
% grayscale image using the coordinates returned by bwboundaries.
subplot(3,2,5); imagesc(originalImage); title('Outlines');
hold on;
boundaries = bwboundaries(binaryImage);	
for k = 1 : numberOfBlobs
	thisBoundary = boundaries{k};
	plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
hold off;

fprintf(1,'Blob #      Mean Intensity  Area     Perimeter  Centroid\n');
for k = 1 : numberOfBlobs           % Loop through all blobs.
	% Find the mean of each blob.  (R2008a has a better way where you can pass the original image
	% directly into regionprops.  The way below works for all versions including earlier versions.)
    thisBlobsPixels = blobMeasurements(k).PixelIdxList;  % Get list of pixels in current blob.
    meanGL = mean(originalImage(thisBlobsPixels));             % Find mean intensity (in original image!)
	blobArea = blobMeasurements(k).Area;		% Get area.
	blobPerimeter = blobMeasurements(k).Perimeter;		% Get perimeter.
	blobCentroid = blobMeasurements(k).Centroid;		% Get centroid.
    fprintf(1,'#%d %18.1f %11.1f %8.1f %8.1f %8.1f\n', k, meanGL, blobArea, blobPerimeter, blobCentroid);
end
msgbox('Finished running BlobsDemo.m.  Check out the figure window and the command window for the results.');