-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathluckiness.c
47 lines (41 loc) · 1.31 KB
/
luckiness.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <math.h>
#include <opencv2/imgproc/imgproc_c.h>
#include "deblur.h"
#define SIGMA_L 20
static double luck_pixel(int x, int y, const CvMat *hom1, const CvMat *hom2)
{
CvMat *invhom1 = cvCreateMat(3, 3, CV_32FC1);
cvInvert(hom1, invhom1, CV_LU);
CvPoint2D32f src = cvPoint2D32f(x, y);
CvPoint2D32f d1, d2;
CvMat pt_src = cvMat(1, 1, CV_32FC2, &src);
CvMat pt_dst = cvMat(1, 1, CV_32FC2, &d1);
cvPerspectiveTransform(&pt_src, &pt_dst, invhom1);
pt_dst = cvMat(1, 1, CV_32FC2, &d2);
cvPerspectiveTransform(&pt_src, &pt_dst, hom2);
double dis = (src.x-d1.x)*(src.x-d1.x)+(src.y-d1.y)*(src.y-d1.y);
dis += (src.x-d2.x)*(src.x-d2.x)+(src.y-d2.y)*(src.y-d2.y);
double luck = exp(-dis/(2*SIGMA_L*SIGMA_L));
cvReleaseMat(&invhom1);
return luck;
}
double luck_image(const IplImage *img, IplImage *img_luck, const CvMat *hom1, const CvMat *hom2)
{
double sum = 0;
double luck ;
for (int i = 0; i < img->height; ++i)
for (int j = 0; j < img->width; ++j)
{
luck = luck_pixel(j, i, hom1, hom2);
CvScalar p = cvGet2D(img, i, j);
/*for (int k = 0; k < 3; ++k) p.val[k] /= 255.0;//?*/
p.val[0] /= 255 ;
p.val[1] /= 255 ;
p.val[2] /= 255 ;
p.val[3] = luck;
cvSet2D(img_luck, i, j, p);
sum += luck;
}
sum /= img->width * img->height;
return sum;
}