-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathahd_mask3.cpp
More file actions
executable file
·167 lines (147 loc) · 5.59 KB
/
Copy pathahd_mask3.cpp
File metadata and controls
executable file
·167 lines (147 loc) · 5.59 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* ahd_mask2.cpp
*
* Created on: 14 May 2010
* Author: sjf
*/
#include <stdlib.h>
#include <math.h>
#include "base.h"
#include "image.h"
#include "bayer.h"
#include "sys/times.h"
#include "util.h"
#include "colorspace.h"
#include "limits.h"
#include "image.h"
#include "bilinear.h"
#include "ahd.h"
#include "mask.h"
//G = (11*R + 16*G + 5*B) /32
#define R_LUM 11
#define G_LUM 16
#define B_LUM 5
#define LUM_SUM 32
int dist3_(int a, int b, int c, int a1, int b1, int c1){
return abs(SQ(a-a1) + SQ(b-b1) + SQ(c-c1));
}
void detect_edges_w_laplacian(img *bilin, pixel *edges){
for (uint y = 0; y < bilin->height; y++) {
for (uint x = 0; x < bilin->width; x++) {
/* Sobel edge filter */
uint gx = abs( get_pixel(bilin,x+1,y-1)[G] +
2 * get_pixel(bilin,x+1,y)[G] +
get_pixel(bilin,x+1,y+1)[G] +
-get_pixel(bilin,x-1,y-1)[G] +
2 *-get_pixel(bilin,x-1,y)[G] +
-get_pixel(bilin,x-1,y+1)[G]);
uint gy = abs( get_pixel(bilin,x-1,y-1)[G] +
2 * get_pixel(bilin,x, y-1)[G] +
get_pixel(bilin,x+1,y-1)[G] +
-get_pixel(bilin,x-1,y+1)[G] +
2 *-get_pixel(bilin,x, y+1)[G] +
-get_pixel(bilin,x+1,y+1)[G]);
pixel *res = get_gray(edges,x,y,bilin->width);
*res = (gx + gy > settings->edge_threshold) ? 255 : 00;
*res = clampc(gx + gy); /* without thresholding */
// /* Laplacian edge filter */
// /* Detects edges, but not direction */
//// uint g = -(get_gray_pixel(grayscale,x+1,y-1) +
//// 2*get_gray_pixel(grayscale,x+1,y) +
//// get_gray_pixel(grayscale,x+1,y+1) +
////
//// 2*get_gray_pixel(grayscale,x,y-1) +
//// 2*get_gray_pixel(grayscale,x,y+1) +
////
//// get_gray_pixel(grayscale,x-1,y-1) +
//// 2*get_gray_pixel(grayscale,x-1,y) +
//// get_gray_pixel(grayscale,x-1,y+1));
//
//// get_gray_pixel(grayscale,x+1,y) +
//// get_gray_pixel(grayscale,x-2,y) +
//// get_gray_pixel(grayscale,x,y+2) +
//// get_gray_pixel(grayscale,x,y-2));
//// g += 12 * get_gray_pixel(grayscale,x,y);
////
//// g+=255;
//
// /* Laplacian edge filter */
// /* Detects edges, but not direction */
// int gx = -(get_gray_pixel(grayscale,x-1,y-1) +
// get_gray_pixel(grayscale,x-1,y) +
// get_gray_pixel(grayscale,x-1,y+1));
//
// gx += (get_gray_pixel(grayscale,x,y-1) +
// get_gray_pixel(grayscale,x,y+1) +
//
// get_gray_pixel(grayscale,x+1,y-1) +
// get_gray_pixel(grayscale,x+1,y) +
// get_gray_pixel(grayscale,x+1,y+1));
//
// gx -= 2 * get_gray_pixel(grayscale,x,y);
//
//
// int gy = -(get_gray_pixel(grayscale,x-1,y-1) +
// get_gray_pixel(grayscale,x ,y-1) +
// get_gray_pixel(grayscale,x+1,y-1));
//
// gy += (get_gray_pixel(grayscale,x+1,y) +
// get_gray_pixel(grayscale,x-1,y) +
//
// get_gray_pixel(grayscale,x+1,y+1) +
// get_gray_pixel(grayscale,x ,y+1) +
// get_gray_pixel(grayscale,x-1,y+1));
//
// gy -= 2 * get_gray_pixel(grayscale,x,y);
// //g += 255;
// int g = MAX(gx,gy);
// pixel *res = get_gray(edges,x,y,grayscale->width);
// /*res = (g > settings->edge_threshold) ? 255 : 00;
// *res = clampc(g); /* without thresholding */
}
}
}
void mask_ahd3(img *image){
Info("Performing AHD Mask v3 interpolation");
uint width = image->width;
uint height = image->height;
/* Save bayer image for use with AHD+mask */
size_t bufsize = height * width * RGB * sizeof(pixel);
pixel *src = mallocz<pixel>(bufsize);
memcpy(src,image->buffer,bufsize);
/* Do bilinear interpolation on the whole image */
host_bilinear(image);
/** Create mask **/
/* Convert interpolated image to grayscale */
size_t gscale_bufsize = height * width * sizeof(pixel);
// pixel *gray_scale = mallocz<pixel>(gscale_bufsize);
// img *gray_img = new_image(height,width,gray_scale);
// img_to_grayscale(image,gray_scale);
// if (settings->save_temps){
// save_grayscale(gray_scale,width,height,"img/bilinear_grayscale.ppm");
// }
/* Detect edges */
img *edge_img = new_image(height,width,NULL);
detect_edges_w_laplacian(image, edge_img->buffer);
if (settings->save_temps){
save_grayscale(edge_img->buffer,width,height,"img/edges_v3.ppm");
}
/* Dilate edges */
pixel *dilated = mallocz<pixel>(gscale_bufsize);
for (uint i = 0; i < settings->dilations; i++) {
dilate(edge_img, dilated);
SWAP(dilated,edge_img->buffer);
}
dilated = edge_img->buffer;
if (settings->save_temps){
save_grayscale(dilated,width,height,"img/mask_v3.ppm");
}
/* Swap bayer image back */
pixel *bilinear = image->buffer;
image->buffer = src;
/* Apply AHD interpolation to the masked areas */
apply_mask_ahd(image,dilated,bilinear);
free(src);
free_image(edge_img);
return;
}