Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Detecting spots on a butterfly

Subject: Detecting spots on a butterfly

From: Husam Aldahiyat

Date: 3 Apr, 2009 08:14:02

Message: 1 of 11

Hello,
Say I have a picture of a butterfly with black spots on its wings, what is a good way to create an algorithm that takes the picture as input and give the location and/or number of spots as output?
The spots aren't circular.

Subject: Detecting spots on a butterfly

From: Image Analyst

Date: 3 Apr, 2009 11:33:01

Message: 2 of 11

"Husam Aldahiyat" <numandina@gmail.com> wrote in message <gr4gga$d84$1@fred.mathworks.com>...
> Hello,
> Say I have a picture of a butterfly with black spots on its wings, what is a good way to create an algorithm that takes the picture as input and give the location and/or number of spots as output?
> The spots aren't circular.
------------------------------------------------------------------
Say you posted it somewhere. Do you think you might get better answers?
All I can say at this point is to try something like watershed segmentation, or an edge detector (such as Sobel) followed by a morphological closing operation. You'll also need bwlabel and regionprops in the image processing toolbox.
Regards,
ImageAnalyst

Subject: Detecting spots on a butterfly

From: Husam Aldahiyat

Date: 3 Apr, 2009 11:47:01

Message: 3 of 11

"Image Analyst" <imageanalyst@mailinator.com> wrote in message <gr4s5d$3lf$1@fred.mathworks.com>...
> "Husam Aldahiyat" <numandina@gmail.com> wrote in message <gr4gga$d84$1@fred.mathworks.com>...
> > Hello,
> > Say I have a picture of a butterfly with black spots on its wings, what is a good way to create an algorithm that takes the picture as input and give the location and/or number of spots as output?
> > The spots aren't circular.
> ------------------------------------------------------------------
> Say you posted it somewhere. Do you think you might get better answers?
> All I can say at this point is to try something like watershed segmentation, or an edge detector (such as Sobel) followed by a morphological closing operation. You'll also need bwlabel and regionprops in the image processing toolbox.
> Regards,
> ImageAnalyst

The pictures are like this one:
http://img8.imageshack.us/img8/1272/200903153435.jpg

I've read the help and tried some things but still having trouble.

Subject: Detecting spots on a butterfly

From: Husam Aldahiyat

Date: 3 Apr, 2009 12:09:01

Message: 4 of 11

"Husam Aldahiyat" <numandina@gmail.com> wrote in message <gr4svl$paf$1@fred.mathworks.com>...
> "Image Analyst" <imageanalyst@mailinator.com> wrote in message <gr4s5d$3lf$1@fred.mathworks.com>...
> > "Husam Aldahiyat" <numandina@gmail.com> wrote in message <gr4gga$d84$1@fred.mathworks.com>...
> > > Hello,
> > > Say I have a picture of a butterfly with black spots on its wings, what is a good way to create an algorithm that takes the picture as input and give the location and/or number of spots as output?
> > > The spots aren't circular.
> > ------------------------------------------------------------------
> > Say you posted it somewhere. Do you think you might get better answers?
> > All I can say at this point is to try something like watershed segmentation, or an edge detector (such as Sobel) followed by a morphological closing operation. You'll also need bwlabel and regionprops in the image processing toolbox.
> > Regards,
> > ImageAnalyst
>
> The pictures are like this one:
> http://img8.imageshack.us/img8/1272/200903153435.jpg
>
> I've read the help and tried some things but still having trouble.

I'm not well versed in the IPT but the thresholds I impose count the spot as one part with the wing.
Any help is appreciated.

Subject: Detecting spots on a butterfly

From: Husam Aldahiyat

Date: 3 Apr, 2009 12:30:03

Message: 5 of 11

Here is my progress so far:

>> I=imread('2009_03_15_3434.JPG');
>> I=rgb2gray(I);
>> bw = im2bw(I,.3);
>> bw = bwareaopen(bw,30);
>> imshow(bw)

I want to use regionprops() like in one of the demos and decide which objects are more round, which will be the spots. But how can I split my pic into objects???

Subject: Detecting spots on a butterfly

From: Husam Aldahiyat

Date: 3 Apr, 2009 12:47:01

Message: 6 of 11

"Husam Aldahiyat" <numandina@gmail.com> wrote in message <gr4vgb$5sn$1@fred.mathworks.com>...
> Here is my progress so far:
>
> >> I=imread('2009_03_15_3434.JPG');
> >> I=rgb2gray(I);
> >> bw = im2bw(I,.3);
> >> bw = bwareaopen(bw,30);
> >> imshow(bw)
>
> I want to use regionprops() like in one of the demos and decide which objects are more round, which will be the spots. But how can I split my pic into objects???

I solved the problem finally. The problem was I needed to do bw=~bw :)

Subject: Detecting spots on a butterfly

From: Skeptic

Date: 3 Apr, 2009 14:29:57

Message: 7 of 11



Husam Aldahiyat wrote:
> "Husam Aldahiyat" <numandina@gmail.com> wrote in message <gr4vgb$5sn$1@fred.mathworks.com>...
> > Here is my progress so far:
> >
> > >> I=imread('2009_03_15_3434.JPG');
> > >> I=rgb2gray(I);
> > >> bw = im2bw(I,.3);
> > >> bw = bwareaopen(bw,30);
> > >> imshow(bw)
> >
> > I want to use regionprops() like in one of the demos and decide which objects are more round, which will be the spots. But how can I split my pic into objects???
>
> I solved the problem finally. The problem was I needed to do bw=~bw :)
--------------------------------------------------------------
Good. To decide on roundness (circularity) just use the compactness
measure from regionprops or just look at the perimeter^2 to area
ratio. Thresholding the red or green channel will get you the brown
spots on yellow background pretty easily (better than using im2bw
which will include the blue channel which you probably don't want).
To get the orange spots on orange background - well that's pretty
tough. Might try hough transform or edge filters or ask Dave
Robinson's opinion or ask over in sci.image.processing. If you get
something, you can post the code and see if I can improve on it's
performance.
Good luck,
ImageAnalyst

Subject: Detecting spots on a butterfly

From: Walter Roberson

Date: 3 Apr, 2009 19:31:53

Message: 8 of 11

Husam Aldahiyat wrote:

> Say I have a picture of a butterfly with black spots on its wings, what is a good way
> to create an algorithm that takes the picture as input and give the location and/or
> number of spots as output?
> The spots aren't circular.

I had a look at the image you supplied in a later posting, and it appeared to me that
the number of spots was beyond any easy counting, and it appeared to me that there were
spots that were smaller than the resolution of the image, especially near the body of
the butterfly. This will make the problem quite difficult -- unless, that is, you place
some kind of minimum size as to what is to be considered a "spot".

Subject: Detecting spots on a butterfly

From: Husam Aldahiyat

Date: 3 Apr, 2009 19:44:01

Message: 9 of 11

Walter Roberson <roberson@hushmail.com> wrote in message <futBl.41$TD1.22@newsfe18.iad>...
> Husam Aldahiyat wrote:
>
> > Say I have a picture of a butterfly with black spots on its wings, what is a good way
> > to create an algorithm that takes the picture as input and give the location and/or
> > number of spots as output?
> > The spots aren't circular.
>
> I had a look at the image you supplied in a later posting, and it appeared to me that
> the number of spots was beyond any easy counting, and it appeared to me that there were
> spots that were smaller than the resolution of the image, especially near the body of
> the butterfly. This will make the problem quite difficult -- unless, that is, you place
> some kind of minimum size as to what is to be considered a "spot".

No, no. The spots in interest are the big black thresholdy ones. Each butterfly has either two or four spots only. Some problems occur when I try the dark coloured butterfliies (black spots on grey wings) but things are managable for the most part.

I have a question: how can I threshold channels? I only know the bw function. Or is it done manually by playing around with the matrix resulting from imread()?

Subject: Detecting spots on a butterfly

From: Image Analyst

Date: 4 Apr, 2009 21:36:01

Message: 10 of 11

To Husam Aldahiyat, the butterfly scientist:
It looks like the red channel gives the most contrast. So I thresholded on that. This code will find the spots for you in the one image that you posted. It may require some tweaks to make it more robust for lots of other images. Plus, of course, you may have to fix broken line wraps if you copy and paste this code.
Good luck,
ImageAnalyst

% Demo macro to find spots on a butterfly wing.
% by ImageAnalyst
clc;
close all;
thresholdValue = 8; % Pick something
% Get a standard MATLAB demo image.
originalImage = imread('C:\Documents and Settings\My Documents\Temporary stuff\200903153435.jpg');
subplot(2, 3, 1);
imshow(originalImage, []);
title('Original image');

redband = originalImage(:,:,1);
subplot(2, 3, 2);
imshow(redband, []);
title('Red Band');

thresholdValue = 80;
binaryImage = redband < thresholdValue;
binaryImage = imfill(binaryImage, 'holes');
subplot(2, 3, 3);
imshow(binaryImage, []);
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(2,3,4); imagesc(coloredLabels); title('Pseudo colored labels');

blobMeasurements = regionprops(labeledImage, 'all'); % Get all the blob properties.
% Get a list of the areas.
area_values = [blobMeasurements.Area];
% Filter the blobs by area. Just keep blobs in a certain area range.
% Find out which blobs have an area between 1000 and 5000 pixels.
% Note: you could also do similar for any of the measurements, such as solidity, etc.
% You can even combine filters over different measurements.
% Use whatever the know valid range for spots area is.
indexes = find((1000 <= area_values) & (area_values <= 5000));
spotsOnlyImage = ismember(labeledImage, indexes);
subplot(2,3,5);
imshow(spotsOnlyImage, []);
title('Spots-only image');

% Get the subset of blobs that describes only the wing-spot blobs.
blobMeasurements = blobMeasurements(indexes); % Get the spot properties only.

% 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(2,3,6); imagesc(originalImage); title('Wing spots outlined over original image');
hold on;
boundaries = bwboundaries(spotsOnlyImage);
numberOfWingSpots = size(blobMeasurements, 1);
for k = 1 : numberOfWingSpots
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
hold off;

% List the various parameters to the command window.
fprintf(1,'Wing Spot # Mean Intensity Area Perimeter Centroid\n');
for k = 1 : numberOfWingSpots % 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
message = sprintf('Done processing this image.\nThere are %d wing spots.\nMaximize and check out the figure window.\nThen check out the command window for the results.', numberOfWingSpots);
msgbox(message);

Subject: Detecting spots on a butterfly

From: Husam Aldahiyat

Date: 9 Apr, 2009 16:10:03

Message: 11 of 11

Wow! That code acted like a full lecture on image processing. I only need to edit it and make it more robust.

Thanks a lot, you're a lifesaver :)

Tags for this Thread

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.

Contact us