In this article I will touch on the basic principles of how to work with images. For this, I chose the OpenCV library . It is distributed for free, so download it is not difficult.


(x) average value for the first picture,
(y) this is for the second,
(x) the standard deviation for the first picture, and accordingly
(y) for the second,
(x, y) is already a covariance. I will remind her to be
(x, y) =
(x, y) -
(x) *
(y). We continue C1 and C2, the correction factors that we need because of the smallness of the denominator. Moreover, they are equal to the square of the number, equal to the number of colors corresponding to a given image bit depth multiplied by 0.01 and 0.03, respectively.IplImage* img1 = cvvLoadImage("before.bmp"); IplImage* img2 = cvvLoadImage("after.bmp"); img->depth That is quite simple.
(x), that is, the mean value . In fact, we work with a picture as with a two-dimensional array. Even here, the resulting value will need to be divided by the size of the image, found as follows: (img->width)*(img->height) double avg(IplImage* img){ unsigned char* ptr; int x,y,b=0,g=0,r=0,col=0; for(x=0;x<=img->width;x++){ for(y=0;y<=img->height;y++){ ptr=(uchar*)(img->imageData+y*img->widthStep); // b=ptr[3*x]; g=ptr[3*x+1]; // r=ptr[3*x+2]; // b - blue, r - red, g - green col+=b+g+r; }} return col/3.00; } double var(IplImage* img, double mu){ //variance unsigned char* ptr; int x,y,b=0,g=0,r=0; double col=0; for(x=0;x<=img->width;x++){ for(y=0;y<=img->height;y++){ ptr=(uchar*)(img->imageData+y*img->widthStep); b=ptr[3*x]; col+=(abs(1.0*b-mu))*(abs(1.0*b-mu)); g=ptr[3*x+1]; col+=(abs(1.0*g-mu))*(abs(1.0*g-mu)); r=ptr[3*x+2]; col+=(abs(1.0*r-mu))*(abs(1.0*r-mu)); }} return col; } double cov(IplImage* img1,IplImage* img2,double mu1,double mu2){ //covariance unsigned char* ptr; int x,y,b1=0,g1=0,r1=0,b2=0,g2=0,r2=0; double col=0; for(x=0;x<=img1->width;x++){ for(y=0;y<=img1->height;y++){ ptr=(uchar*)(img1->imageData+y*img1->widthStep); b1=ptr[3*x]; g1=ptr[3*x+1]; r1=ptr[3*x+2]; ptr=(uchar*)(img2->imageData+y*img2->widthStep); b2=ptr[3*x]; g2=ptr[3*x+1]; r2=ptr[3*x+2]; col+=(b1-mu1)*(b2-mu2)+(g1-mu1)*(g2-mu2)+(r1-mu2)*(r2-mu2); }} return col; } cvReleaseImage(&img1); cvReleaseImage(&img2); Source: https://habr.com/ru/post/126848/
All Articles