You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to labeling the tip of the head and the end of the tail of this fish image? Like how i did with the center of the fish using centroid.
1 view (last 30 days)
Show older comments
i attach the image for reference if you do not understand. have problem putting a label on the head and tail.
Accepted Answer
Image Analyst
on 16 Jul 2014
You didn't attach an image. You can use text() or plot() to label the ends.
47 Comments
Image Analyst
on 18 Jul 2014
So pick your marker and plot it. Say you want a * at (x,y):
plot(x, y, 'r*', 'MarkerSize', 10, 'LineWidth', 2);
Get the orientation, and majoraxislength to figure out your x,y using just basic trig or algebra like you learned in 10th grade. Remember the point slope formula of a line? Trivial. You'll have to figure out which end is thicker if you want to use different markers for head and tail. You could also use annotation() if you want to draw an arrow from tail to head.
Tommy
on 18 Jul 2014
yes, i used major and minor to get the x value but i don't get how to obtain the y value. In the end of all the plotting, i need to take the points and do a RMS deviation.
Image Analyst
on 18 Jul 2014
Like I said you need the orientation, which is the angle. Then take the arctangent to get the slope, then use the point slope formula. 10th grade stuff.
Image Analyst
on 21 Jul 2014
measurements = regionprops(labeledImage, 'MajorAxisLength', 'Orientation');
allAngles = [measurements.Orientation];
The point slope formula is
(y-yc) = slope * (x-xc)
where xc,yc is your centroid. You need to figure out x and y, which will be at the head and tail. So (xTail-xc)/2 = (MajorAxisLength/2) * cosd(angle). Does that give you enough to complete it?
Tommy
on 23 Jul 2014
Zebrafish_Majoraxislength = [Zebrafishdataa.MajorAxisLength]; Zebrafish_Minoraxislength = [Zebrafishdataa.MinorAxisLength]; Zebrafishtail = Zebrafish_centroid+[-Zebrafish_Majoraxislength/2,-148]; Zebrafishhead = Zebrafish_centroid+[+Zebrafish_Minoraxislength*1.5,73];
i did this which leaves a mark at the place. but i need the code to auto detect it like let say if the fish change position, will it auto detect its head and tail again?
Image Analyst
on 23 Jul 2014
Whatever you did to find the fish and produce a binary image of it, you will just do that same thing again. The process of putting markers at the head and tail is not dependent on what you do to get the binary image in the first place.
Tommy
on 23 Jul 2014
oh i see. i thought i could use the same code as to determine its position automatically. is there a way to do so? to find the head and tail automatically?
Image Analyst
on 23 Jul 2014
I thought you already had a way to identify the head and tail. One way to do it would be to put a circle around each endpoint of the line that we just discussed drawing. See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F Then use the circle mask to sum up the number of binary white pixels in the mask. The end with the greater number will be the head and the end with the smaller number will be the tail.
Tommy
on 24 Jul 2014
i would like to ask if you know how to find the maximum and minimum area of the fish. Do you think using this method is better? Which will be able to auto detect the head(which is maximum) and tail(which is minimum) for different images of the same fish.
Image Analyst
on 24 Jul 2014
It sounds like you need a basic tutorial in image analysis, which is in my File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 The tutorial will show you how to get the binary image of your fish (which you already know), and then how to call regionprops to get the areas of all the fish. In short, it's something like
measurements = regionprops(labeledImage, 'Area');
allAreas = [measurements.Area];
[biggestArea, indexOfBiggest] = max(allAreas);
[smallesArea, indexOfSmallest] = min(allAreas);
That will give you the area of the biggest and smallest fish.
To find the areas at the head and tail, use the first two code sections in the FAQ to make circle masks at each endpoint of the line. Then count the pixels in the circle
% Get the count at one end of the fish.
circle1 = binaryImage & circleMask1; % Mask binary image by circle mask.
pixelsAtEndpoint1 = sum(circle1(:))
circle2 = binaryImage & circleMask2; % Mask binary image by circle mask.
pixelsAtEndpoint2 = sum(circle2(:))
if pixelsAtEndpoint1 > pixelsAtEndpoint2 then endpoint 1 is the head. Otherwise it's the tail.
Tommy
on 25 Jul 2014
i did a try on it but i realise i need to do it for every picture of my fish. IS there like a code to auto detect head & tail when i change picture like centroid?
Image Analyst
on 25 Jul 2014
Edited: Image Analyst
on 25 Jul 2014
Yes you will need to do it on every fish object in your image one at a time, but that should not be a problem - why do you hint that it is?
The Mathworks is still working on their automated Fish Head and Tail toolbox. Just kidding of course.
If this answers your questions, could you finally mark the Answer as Accepted?
Tommy
on 29 Jul 2014
it will be a problem as there will be thousand of pictures and i cant be doing it one by one. But anyway thanks for your help and patience.
Image Analyst
on 29 Jul 2014
Why not? It should take just a second or so per picture if there are, say, a dozen fish. So a thousand pictures takes 1000 seconds or 15-20 minutes. What's the problem with that? Some people have programs that run for days. Explain why you can't wait 20 minutes.
Tommy
on 29 Jul 2014
true but people now want automatic. Is actually the same fish but moving around in a cylinder. So 2-3 seconds will take picture of its movement. Then i got to calculate the movement of the fish. That is why i need 3 points of each picture, the head, the body and the tail. Centroid is for body. Head and tail i am still figuring out.
Image Analyst
on 29 Jul 2014
You imply that it's not automatic. Tell me what is not automatic about the process I described? There is no user-assisted steps in it where the user has to manually draw or indicate something on the image. Why do you imply that it is not "automatic"? Please define not automatic - your definition. Because as far as I see it, you just highlight all the images in a listbox and click the "Go!" button and it batch processes the whole batch of images with no further user interaction (unless you want to ask them for an Excel filename for any results).
Tommy
on 29 Jul 2014
Edited: Image Analyst
on 29 Jul 2014
as in automatic like every time change picture will detect the coordinates of head and tail. Now i using the pointers and to put at the end of the tip of the head and tail. Then using the position of the pointers for the coordinates for my equation.
%5th and 1st Head Difference
HeadDiff5 = (Zebrafishhead4 - Zebrafishhead).^2;
%5th and 1st Body Difference
BodyDiff5 = (Zebrafish_centroid4 - Zebrafish_centroid).^2;
%5th and 1st Tail Difference
TailDiff5 = (Zebrafishtail4 - Zebrafishtail).^2;
%RMSD result of 5th and 1st
E = ((HeadDiff5 + BodyDiff5 + TailDiff5)./3);
Result5 = sqrt(E);
For example like that. Where Zebrafishhead and Zebrafishtail are the pointers.
Zebrafishtail = Zebrafish_centroid+[-Zebrafish_Majoraxislength/1.83,-172];
Zebrafishhead = Zebrafish_centroid+[+Zebrafish_Minoraxislength*1.93,90];
So i am like trying to like get the position of the head and tail without writing those number(slowly putting the position of the marker). Like Major and Minor only can get the X value for the fish but not the Y value. I dont know how to use orientation for this. So if i can get the Y value of the Major and Minor it would be much easier for me.
Image Analyst
on 29 Jul 2014
I don't know what "using pointers" means, but you simply get a binary image like you already got, then use regionprops() to ask for centroid and orientation, and majorAxisLength of every fish. Then follow the steps I outlined to identify the two ends of the fish using those 3 measurements. Then place a circle mask over each end and determine which end is larger to identify the head. It's totally automatic.
Image Analyst
on 30 Jul 2014
Sorry, it's a bit more work that I would be willing to do for free. It's not like a 5 minutes job. I outlined the steps for you and I think you'll learn a lot by doing it yourself. See the Image Segmentation tutorial in my File Exchange if you need an example.
Tommy
on 30 Jul 2014
Alright thank you so much for helping me. I am gonna try using the point slope formula and then try using the circle mask too. By saying using the 2 sections from FAQ, you mean the 2 paragraph code right? Still trying to understand it step by step. Thanks anyway for your patience and help. Appreciate it a lot.
Image Analyst
on 30 Jul 2014
I was probably talking about the section that tells you how to create a circle mask and the one telling you how to batch process a sequence of files.
Tommy
on 31 Jul 2014
I finally understand the code you're trying to say. Do you know how to cut the image in half from the centroid vertically or horizontally?
Tommy
on 31 Jul 2014
i like to add on. I already found a past work of you drawing a line from the centroid vertically and horizontally. Is there a way to use the separated parts?
Image Analyst
on 31 Jul 2014
Yes you could but it's not as robust, and if the fish is aligned vertically you may not be able to tell which side is head and which side is tail. I suggest you use the circle mask method where you find the endpoints and place a circle mask over each endpoint and count the pixels.
Tommy
on 1 Aug 2014
oh, but can you explain how can i use the separated sides? if the fish is vertical, i can change the like to horizontal too.
Image Analyst
on 1 Aug 2014
That method is no easier than the method I suggested, and yet less robust, so I don't why you want to use it. You'd have to label the image (after splitting apart) then call regionprops and see which side is larger, but whether it is "right" or not depends on where you split it. It's possible that if you split it at the centroid that each half may have the same number of pixels. Not a problem with the method I outlined to you.
Tommy
on 1 Aug 2014
i know the steps to do it but i am like stuck. For the method you suggested, how do i get the endpoints? i know have to use centroid, orientation and majoraxislength to obtain it but i have no idea how. And i do not know how to put the circle inside. That's why if you are free, i hope you can write me a sample of it.
Image Analyst
on 1 Aug 2014
Maybe on the weekend if I have time. In the mean time, upload a binary image with at least two fish in it with no other stuff like blue pixels, lines, circles, text, axes, tick marks, white or gray surround, screenshots, or any other "stuff" - just the bare binary image.
Tommy
on 1 Aug 2014
Image Analyst
on 1 Aug 2014
Will you ever have two in one picture, or only part of one in the field of view?
Image Analyst
on 4 Aug 2014
Tommy, I used a different approach. See attached m-file below the image.

Tommy
on 4 Aug 2014
thank you sir! i will read through and try the codes out and get back to you. i really appreciate your patience and time with me.
Tommy
on 4 Aug 2014
it works nicely. thank you sir. one question, what is the position for the centroid, head and tail?
Image Analyst
on 4 Aug 2014
If you want, you can change the last lines of code to be like this:
% Put a label at the head
if numWhitePixels2 > numWhitePixels1
text(x(index2), y(index2), 'Point 2 = Head', 'FontSize', fontSize, 'Color', 'r');
text(x(index1), y(index1), 'Point 1 = Tail', 'FontSize', fontSize, 'Color', 'r');
xHead = x(index2);
yHead = y(index2);
xTail = x(index1);
yTail = y(index1);
else
text(x(index1), y(index1), 'Point 1 = Head', 'FontSize', fontSize, 'Color', 'r');
text(x(index2), y(index2), 'Point 2 = Tail', 'FontSize', fontSize, 'Color', 'r');
xHead = x(index1);
yHead = y(index1);
xTail = x(index2);
yTail = y(index2);
end
This will make variables xHead, xTail, yHead, and yTail.
Tommy
on 5 Aug 2014
alright, thank you sir. erm do you know how to plot a x-graph and y-graph? like value against x or value against y.
Image Analyst
on 5 Aug 2014
Not sure I understand your question. I don't know what "erm" means and I'm sure you've heard of the plot() function, so I don't know what to say.
Image Analyst
on 7 Aug 2014
You aren't still splitting your fish down the middle are you? And what's with that line along the left column that I had to delete? Other than that I don't know what you're talking about so post a screenshot.
More Answers (0)
See Also
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)