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.
Use the "save" command, and save the variable M in a .mat file
Hi Vicky
bwlabel creates a matrix of doubles (which imwrite assumes are between
0 and 1). If you convert it to an unsigned integer format, e.g
M = uint8(M) % (or uint16, uint32, etc)
then saving to a bmp will probably give you what you want
cheers,
Richard
---------------------------------------------
Correct. Save in Tiff format instead. It can take monochrome.
Regards,
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:
I'm not clear on what you are asking. With your repetitions of the fact that you
want each pixel to be the "label" and not the colour, then it sounds like what you
want to produce is an image in which everywhere that the original image would
have been labeled with 1, the new image would show the -character- 1, and so
on -- an image composed of text drawn in to the appropriate places.
If your goal was just to store binary data corresponding to the label, then
it is not clear why you would bother to store it in an image file, but there is
some hidden Very Good Reason For This, then create a new matrix which is
NewImageG = uint16(LabelMatrix);
and write that out using 'png' or 'ppm' or 'jpg' format. You may need
to use the jpg options
'Bitdepth', 16, 'Mode', 'lossless'
If you -must- use BMP for your purposes, then if L is your label matrix:
L16 = uint16(L);
Lrgb = cat(3,zeros(size(L16),'uint8'),uint8(bitshift(L16,-8)),uint8(bitand(L16,uint16(255))));
imwrite(Lrgb, 'FileName.bmp');
and to read the values back in:
Lrgb = imread('FileName.bmp');
L = bitor(bitshift(uint16(Lrgb(:,:,2)),8),uint16(Lrgb(:,:,3)));
This code would need some minor adjustment if you have more than 65535 labels.
--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?
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.');
thank you very much for your help and the code.
Best regards,
Vicky
ImageAnalyst schrieb: