# centroid using matlab

This topic is 5169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi, i've decided to use matlab,and am trying to write little program that will find the centroid to the following specification [C, threshim] = centroid(im, thresh) This function should take an image of an object, threshold it to produce a black and white image, and return the x and y coordinates of the object's centroid as a 2 element vector C. It should also return the thresholded image as threshim. (how do i do that?) I want to measure the centroid relative to the top left corner of the image. The argument im should be a grey scale image represented by a 2D matrix of numbers where a value of 0 represents black (no light) and a value of 255 represents white (full brightness). The argument thresh should specif the threshold value used to convert a grey scale image into an image having only two possible values, 0 (background) or 1 (object). I need to find a way to threshold the values, so that i get an appropriate seperation of the object and the background. Basically, this is what i want to do.The matrix of image values is thresholded at a value of 130. im threshim threshold of 130 100 205 47 9 83 0 1 0 0 0 141 187 33 11 96 ------------> 1 1 0 0 0 98 143 96 17 108 0 1 0 0 0 The pixels with a value > 130 end up with a value of 1 in the thresholded image, all other pixels end up with a value of 0. so basically the function must threshold the input image and then calculate the centroid of the object (the centroid of the pixels with value 1). If a centroid cannot be computed for some reason i guess i'd need some sort of error message as i mentioned above,i'm new to matlab, and i'd greatly appreciate it if someone could help me with making this funciton...

##### Share on other sites
basically what i want to be able to do in the end, is write a function testcentroid, to the spec testcentroid(imname, thresh)

The function should take as it's arguement a string (imname,which specifies the name of the image file to read) and thresh, the threshold value to be passed to the function "centroid" from above...

once the image file name is given,the function will read in the specified image and obtain a 2d array of pixel values (0to255). This array, is passed to the centroid function,with the threshold value. the centroid function should then send back the centroid and thresholded image. this function (testcentroid) should then display the thresehold image on the screen and plot a mark on the image....to indicate where the threshold is... basically,this function doesnt return any data, and only plots the threshold image and cetnroid

the centroid function does not display the image or its centroid. All it does is calculate where the centroid is and return it as a 2-vector along with the thresholded image. The displaying of the
image and its centroid is only done by testcentroid.

##### Share on other sites
this is what i have so far

Reads image file shape1.jpg and returns a 2D array of image values which is assigned to the variable im

imshow(im);
Displays the image in a figure window.

[C, threshim] = centroid(im, 100);
Find centroid of the object in 'im' using a threshold of 100.

and for testcentroid, i have

imshow(threshim);
hold on;
plot(C(1), C(2), 'r*');

hopefully taht should Plot a red '*' at X = C(1) and y = C(2)

this is all i can do .... am i right so far above? and how would i exactly do it? still slowly learning it from websites and an old book,which isn't very good it seems

##### Share on other sites
The threshold image is quite easy. When using comparison operators with an array and a scalar, the comparison is performed element by element in the array with the scalar, and the result is an array of ones and zeros (for true and false, respectively).
threshim = im > thresh;

However, I don't understand what you mean by the centroid. Do you simply want the center of all ones in the image?

1. 1
Rutin
48
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 16
• 9
• 10
• 13
• ### Forum Statistics

• Total Topics
633003
• Total Posts
3009846
• ### Who's Online (See full list)

There are no registered users currently online

×