-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEdgeDensity.cpp
More file actions
97 lines (83 loc) · 1.83 KB
/
Copy pathEdgeDensity.cpp
File metadata and controls
97 lines (83 loc) · 1.83 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "ImageQuality.h"
#define M_PI 3.14159265358979323846
/**
* Creates an empty (all values = 0.0) image quality matrix.
*/
void ImageQuality::_create(double**& _tmp, int width, int height)
{
_tmp = new double*[height];
for(int y=0; y<height; y++)
{
_tmp[y] = new double[width];
for(int x=0; x<width; x++)
{
_tmp[y][x] = 0.0;
}
}
}
/**
* Deallocates a passed image quality matrix.
*/
void ImageQuality::_delete(double** _tmp, int size)
{
for(int i=0; i<size; i++)
{
delete[] _tmp[i];
}
delete[] _tmp;
}
/**
* Calculates Sobel Edge.
*
* @param img Source image
* @param (OUT) numPix Total number of processed pixel
* @return Sum
*/
double ImageQuality::calcSobelEdge(IplImage *img, int& numPix)
{
// 3x3 Sobel mask (X)
const int maskX[3][3]={ -1, 0, 1,
-2, 0, 2,
-1, 0, 1};
const int maskY[3][3]={ 1, 2, 1,
0, 0, 0,
-1,-2,-1};
double sum = 0.0;
double dx = 0.0, dy = 0.0;
int countPix = 0;
int i, j;
IplImage* eyeImg = NULL;
eyeImg = cvCloneImage(img);
// Loop - ignoring the edges
for(int y=1; y<img->height-1; y++)
{
for(int x=1; x<img->width-1; x++)
{
dx=0.0;
dy=0.0;
// Horizontal gradient (X)
for(j=-1;j<=1;j++)
{
for(i=-1;i<=1;i++)
{
dx = dx + (unsigned char)(img->imageData[(x+i)+(y+j)*img->widthStep])*maskX[j+1][i+1];
}
}
// Vertical gradient (Y)
for(j=-1;j<=1;j++)
{
for(i=-1;i<=1;i++)
{
dy= dy + (unsigned char)(img->imageData[(x+i)+(y+j)*img->widthStep])*maskY[j+1][i+1];
}
}
countPix++;
// Gradient magnitude
sum += sqrt((dx*dx)+(dy*dy));
}
}
numPix = countPix;
//ImageUtility::showImage("Sobel Mask", eyeImg);
cvReleaseImage(&eyeImg);
return sum;
}