1
$\begingroup$

I have images (data is represented by grayscale values from 0 to 255) and the total sum of the grayscale values represent the mass of the object. I know that every grayscale is related to mass, given by the equation

$y=a x$

where $y$ is the mass of the object in kg, $x_i\in(0,255)$ is the grayscale value and $a$ relates the mass to a grayscale value in kg/grayscale.

I have as many images as needed, and for each image I know the mass of the object, but the image data is not evenly distributed. So I could have an image with 10 pixels with grayscale value 10, 20 pixels with grayscale value 11, 30 pixels with grayscale value 12, etc. And the sum represents the mass of the object (10*10+20*11+30*12+...=100 kg, for example). So I measure data in the form of

$y = a \sum_{i=0}^{255}(n_i x_i)$

where $n_i$ is the amount of grayscale values $x_i$ in the image.

Now I would like to fit a linear line through data (eq linear least squares) in order to estimate $a$. H Can someone tell me if this is possible with lls, or perhaps with an other algorithm? (I'm pretty sure it will be possible, but I didnt know good search terms and could find anything...)

$\endgroup$
  • $\begingroup$ Can you explain more about how you plan to fit data? Do you have multiple images with known masses? If so, yes, you can use the least-square technique to get a linear fit to determine the value of $a$ if you have at least two images. $\endgroup$ – Batominovski Oct 22 '18 at 21:09
  • $\begingroup$ I have edited the question to make this more clear. Let me know if this helps? $\endgroup$ – Ashgard Weterings Oct 23 '18 at 7:40
0
$\begingroup$

Suppose that you have $N$ images with (known) masses $M_1,M_2,\ldots,M_N$. For $j=1,2,\ldots,N$, let $X_j$ denote the total grayscale value sum of the $j$-th image (that is, $X_{j}=\sum\limits_{i=0}^{255}\,i\,n_{i,j}$ with $n_{i,j}$ is the number of pixels with grayscale value $i$ in the $j$-th image). You want to determine the least-square estimator for $\hat{a}$ from the equation $$M_j=a\,X_j\text{ for }j=1,2,\ldots,N\,.$$ Define $$f(a):=\sum_{j=1}^N\,\left(M_j-a\,X_j\right)^2\text{ for all }a\in\mathbb{R}\,.$$ Then, $a:=\hat{a}$ minimizes the value of $f(a)$. Since $$f'(a)=-2\,\sum_{j=1}^N\,X_j\,\left(M_j-a\,X_j\right)\text{ and }f'\left(\hat{a}\right)=0\,,$$ we conclude that $$\hat{a}=\frac{\sum\limits_{j=1}^N\,X_j\,M_j}{\sum\limits_{j=1}^N\,X_j^2}$$ is the least-square estimator of $a$.


On the other hand, if $M_1,M_2,\ldots,M_N$ comes with (known) uncertainties $\sigma_1,\sigma_2,\ldots,\sigma_N\in\mathbb{R}_{>0}$, respectively. It is best to use the maximum-likelihood estimator. Define $$g(a):=\sum_{j=1}^N\,\left(\frac{M_j-a\,X_j}{\sigma_j}\right)^2\text{ for all }a\in\mathbb{R}\,.$$ Ergo, if $A$ is the maximum-likelihood estimator for $a$, then $a:=A$ minimizes $g(a)$. Because $$g'(a)=-2\,\sum_{j=1}^N\,\frac{X_j}{\sigma_j}\,\left(\frac{M_j-a\,X_j}{\sigma_j}\right)\text{ and }g'(A)=0\,,$$ we obtain $$A=\frac{\sum\limits_{j=1}^N\,\frac{X_j\,M_j}{\sigma_j^2}}{\sum\limits_{j=1}^N\,\frac{X_j^2}{\sigma_j^2}}\,.$$ If $\sigma_1=\sigma_2=\ldots=\sigma_N$, then the least-square estimator $\hat{a}$ coincides with the maximum-likelihood estimator $A$.

$\endgroup$
  • $\begingroup$ Thanks for your replay. I now see where my mistake is. I said that the relation between $M$ and $X$ is linear, but it is not. I didnt want to add another problem because the nonlinearity can be removed by taking the exponent on both sides. I though it was not allowed to sum the $X$'s due to the nonlinearity, but that is allowed. And it is also allowed after taking the exponent. So indeed the solution is quite simple. Thanks for thinking along. $\endgroup$ – Ashgard Weterings Oct 23 '18 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.