# Critique my SSD implementation: is it correct?

I have implemented sum of squared differences in C++. Is my code correct, can it be improved for 3 channel images?

Also I have one important question; what is the difference between the **Sum of Squared Errors, Sum of Squared Differences and Mean Squared Error**? Are they different names for the same thing or are they used for different applications?

Are there better algorithms that give me a measure of the difference between 2 images? My understanding is there is **Variance** (std deviation squared) which can also measure the difference between 2 images however it is less *better*? than SSD. However SSD isn't that great either I hear.

```
int calcSSD( Mat& img1, Mat& img2) const {
int sum = 0;
int r, c;
uchar* i1Ptr, *i2Ptr;
for (r = 0; r < img1.rows; ++r) {
i1Ptr = img1.ptr<uchar>(r);
i2Ptr = img2.ptr<uchar>(r);
for (c = 0; c < img1.cols; ++c) {
int diff = i1Ptr[c] - i2Ptr[c];
sum += diff * diff;
}
}
return sum;
}
```

your function can be replaced with a simple:

one-liner (which would also handle different types and multiple channels correctly).

please do not write loops like this with opencv.

@berak thanks. Is there a difference between Sum of Squared Errors, Sum of Squared Differences and Mean Squared Error?

no, there isn't. different names only reflect different context. (e.g. if you have an

expectationand ameasure, then you calculate theerror, while for similarity you'd name itdistance)(and since it's all distances, less IS better !)

also, have a look here for alternative similarity measures.

really, it all depends, on what you're trying to achieve (the context). which is here ?

which problem are you trying to solve, here ?

Generally, this is my practical application of my learning. I have learnt the theory of SSD and now I am implementing it to cement learning. So I want to make sure my implementation is correct and that my understanding is correct (what it is and when to apply it/how its useful). Specifically I am intending to use to do crude segmentation. I grid an image and then accumulate/group cells based off their SSD closeness (I compare intensity SSD, magnitude SSD and gradient direction SSD).