Thread Subject: retain certain pixels in image

Subject: retain certain pixels in image

From: Philip Villaseran

Date: 7 May, 2008 16:57:03

Message: 1 of 4

I'm new to image processing in MATLAB, so bear with me. =)

Is there a way to retain parts of an image, say of size 5
pixels by 1 pixel? I've tried a number of different
functions (mostly through trial and error) but can't seem to
find a way.

It's a colored image. Do I have to convert it to black and
white or grayscale?

Thanks.

Subject: retain certain pixels in image

From: William Thielicke

Date: 7 May, 2008 17:13:04

Message: 2 of 4

Hey!
I am also new, but can't you use A=imread('...') and then
B=A(1:5 1)

Oder something like that (in a correct syntax.... ;-D)
Cheers,
William

Subject: retain certain pixels in image

From: Image Analyst

Date: 7 May, 2008 18:27:04

Message: 3 of 4

"William Thielicke" <w.thielicke.doesntLikeSpam.@rug.nl>
wrote in message <fvsnv0$k78$1@fred.mathworks.com>...
> Hey!
> I am also new, but can't you use A=imread('...') and then
> B=A(1:5 1)
>
> Oder something like that (in a correct syntax.... ;-D)
> Cheers,
> William
-----------------------------------------------------------
-----------------------------------------------
No, William - that won't work. What you need to do Philip
is to first find the objects in your image. There are
many ways to do this but basically you want to get your
color image down to a point where you can threshold it to
form a binary image. Then you do bwlabel() on it,
followed by regionprops(). The structure returned by
regionprops will have information (such as size, shape,
coordinates of pixels, etc.) in it for every object in
your image. You can then do whatever you want to do with
the information (e.g. make distributions or calculate
means). If you want to erase them from the image you can
find the indexes of the objects you want to erase and then
zero out the pixels from the labelled image. Then you
could threshold the labelled image at a value of 1 to form
a binary mask image, which you can then multiply by your
original image to zero out those parts of your image not
meeting your criteria (such as their area is more than 5
pixels or its shape is not rectangular, etc.) It might
sound more complicated than it really is. Once you have
the image ready for thresholding, the rest of it is only
like 8 or 9 lines of code.
Regards,
ImageAnalyst

Here's some expanded demo code for you that I wrote:

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

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 perimeter.
    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.');

Subject: retain certain pixels in image

From: Philip Villaseran

Date: 8 May, 2008 01:47:04

Message: 4 of 4

Thank you Image Analyst. Will try and post my results later.

"Image Analyst" <imageanalyst@mailinator.com> wrote in
message <fvss9o$bfu$1@fred.mathworks.com>...
> -----------------------------------------------
> No, William - that won't work. What you need to do Philip
> is to first find the objects in your image. There are
> many ways to do this but basically you want to get your
> color image down to a point where you can threshold it to
> form a binary image. Then you do bwlabel() on it,
> followed by regionprops(). The structure returned by
> regionprops will have information (such as size, shape,
> coordinates of pixels, etc.) in it for every object in
> your image. You can then do whatever you want to do with
> the information (e.g. make distributions or calculate
> means). If you want to erase them from the image you can
> find the indexes of the objects you want to erase and then
> zero out the pixels from the labelled image. Then you
> could threshold the labelled image at a value of 1 to form
> a binary mask image, which you can then multiply by your
> original image to zero out those parts of your image not
> meeting your criteria (such as their area is more than 5
> pixels or its shape is not rectangular, etc.) It might
> sound more complicated than it really is. Once you have
> the image ready for thresholding, the rest of it is only
> like 8 or 9 lines of code.
> Regards,
> ImageAnalyst
>
> Here's some expanded demo code for you that I wrote:
>
> 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
>
> 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 perimeter.
> 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.');
>

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
image processing Image Analyst 7 May, 2008 14:30:50
image analysis Image Analyst 7 May, 2008 14:30:50
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com