Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>[Answer edited to reflect updates]</strong></p> <p>In your code, you used</p> <pre><code>C1=centroids(1,1); C2=centroids(1,2); </code></pre> <p>After this step, C1 and C2 become single-element scalars. You can check this with <code>size(C1)</code> and <code>size(C2)</code>, which will return <code>[1 1]</code> as the answer. I'm guessing that you wanted to plot the first C1- and C2-point, then extend it all the way to element pair N. That's not necessary, the <code>plot</code> function can handle vectors (and even matrices, but that will appear as a series of plots).</p> <p>I'm not familiar with the image processing toolbox, and I do not have that toolbox so I can't check the function outputs. But as far as I can tell, what you need is a 30-row 2-column array of centroid position data, from which you will curve-fit its position at unknown x. I've removed some plotting functions; I trust this will make the code clearer.</p> <pre><code>for N=1:30 I = figure.image.(['j' num2str(N)]); bw=(I); ss = bwlabel(bw); s = regionprops(bw,'centroid'); centroids = cat(1, centroids, s.Centroid); %Concatenate s.Centroid below centroids end %At this point, "centroids" should be a 30-by-2 array size(centroids) % check if the output from this is [30 2] x=centroids(:,1); y=centroids(:,2); poly=polyfit(x,y,2); %poly is a vector of curve-fitted polynomial coefficients pval=polyval(poly,x); %pval is a vector of curve-fitted values evaluated at x parabola=plot(x,pval); %plot the parabola </code></pre> <p>To get the y-position of the parabola at a point x_i, use <code>polyval(poly,x_i)</code>.</p> <p>Note particularly the proper syntax for the <code>cat</code> function; cat(A,B) concatenates B below A, you can't use <code>cat</code> with only one argument, as in your original code. This is likely the root cause of your headache, since with only 1 argument MATLAB simply takes "s.Centroid" as your new "centroids" array instead of adding it below the existing "centroids" array.</p> <p><strong>Reply to EDIT #3</strong></p> <p>In this part of your code</p> <pre><code>poly=polyfit(C1,C2,2); %'poly' is a vector of polynomial coefficients g=roots(poly); %g solves for polynomial roots v =max(g) %v is the largest root (assumed to be the expected ground-level destination) plot(xPlot,polyval(poly,xPlot),'y') %plots polynomial at given x-values %here, one expects to plot a point for the expected destination. %the call to the plot function should follow syntax similar to the previous line plot(v,'go') %plot function has syntax plot(x,y,options) %therefore it should look like plot(v,polyval(poly,v),'go') </code></pre> <p>I've added some comments. The problem is with the line where you called <code>plot</code> to plot the expected destination; the syntax seems to be wrong. If only 1 data argument is given, MATLAB assumes the given data to be the y-value, using its array index as the x-value (i.e. plotting y(1) at 1, y(2) at 2, etc). This is not what you want for variable v.</p> <p>You can also use <code>plot(v,0,'go')</code> as mentioned by Jonas, but it never hurts to double-check if the extrapolated polynomial value is actually near 0 ;)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload