# 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;
}

edit retag close merge delete

your function can be replaced with a simple:

double dist = norm(a,b, NORM_L2SQR);


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

please do not write loops like this with opencv.

( 2018-07-12 02:36:30 -0500 )edit

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

( 2018-07-12 02:41:15 -0500 )edit

no, there isn't. different names only reflect different context. (e.g. if you have an expectation and a measure, then you calculate the error, while for similarity you'd name it distance)

(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 ?

( 2018-07-12 02:44:34 -0500 )edit

which problem are you trying to solve, here ?

( 2018-07-12 05:03:32 -0500 )edit
2

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).

( 2018-07-12 05:14:42 -0500 )edit