aboutsummaryrefslogtreecommitdiff
blob: 78ec9a986930c40ca9d21a580b76e8ce4305f4d4 (plain) (blame)
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
#include "dcp_core.h"

#include <algorithm>
using std::sort;

struct Pixel
{
	int value;
	int i, j;
};

void CalcAirlight(IplImage *darkchannel, IplImage *input, double A[])
{
	int height = input->height;
	int width = input->width;
	int widthstep = input->widthStep;
	int gwidthstep = darkchannel->widthStep;
	int nch = input->nChannels;

	struct Pixel *v_darkchannel = (struct Pixel *)malloc(sizeof(struct Pixel) * height * width);
	int count = 0;
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			int value = *(uchar *)(darkchannel->imageData + i * gwidthstep + j);
			struct Pixel p = { value, i, j };
			v_darkchannel[count++] = p;
		}
	}
	sort(v_darkchannel, v_darkchannel + count, [](struct Pixel &a, struct Pixel &b){ return a.value > b.value; });

	IplImage *mask = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
	cvZero(mask);

	for (int i = 0; i < count * 0.001; i++)
	{
		struct Pixel p = v_darkchannel[i];
		*(uchar *)(mask->imageData + p.i * gwidthstep + p.j) = 255;
	}
	

	for (int k = 0; k < 3; k++)
	{
		struct Pixel *v_channel = (struct Pixel *)malloc(sizeof(struct Pixel) * height * width);
		int count = 0;

		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width; j++)
			{
				int flag = *(uchar *)(mask->imageData + i * gwidthstep + j);
				if (flag == 0)
					continue;

				int value = *(uchar *)(input->imageData + i * widthstep + j * nch + k);
				struct Pixel p = { value, i, j };
				v_channel[count++] = p;
			}
		}

		sort(v_channel, v_channel + count, [](struct Pixel &a, struct Pixel &b){ return a.value > b.value; });
		
		double channel_airlight = 0;
		for (int i = 0; i < count * 0.01; i++)
		{
			channel_airlight += v_channel[i].value;
		}
		channel_airlight = channel_airlight / (count * 0.01);
		A[k] = channel_airlight;

		free(v_channel);
	}
	free(v_darkchannel);
}
Powered by cgit v1.2.3 (git 2.41.0)