|
|
@ -14,6 +14,8 @@ int conf_samples = 25;
|
|
|
|
int conf_rotate = 0;
|
|
|
|
int conf_rotate = 0;
|
|
|
|
Rect conf_rect = { .x = 315, .y = 0, .w = 70, .h = 1910 };
|
|
|
|
Rect conf_rect = { .x = 315, .y = 0, .w = 70, .h = 1910 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int detect_level(ImageFloat *imgf);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* open video device
|
|
|
|
* open video device
|
|
|
|
* read multiple images into memory
|
|
|
|
* read multiple images into memory
|
|
|
@ -45,7 +47,7 @@ int main(int argc, char **argv) {
|
|
|
|
char outdate[LEN_FILENAME];
|
|
|
|
char outdate[LEN_FILENAME];
|
|
|
|
time_t curtime = time(NULL);
|
|
|
|
time_t curtime = time(NULL);
|
|
|
|
struct tm *curtime_tm = NULL;
|
|
|
|
struct tm *curtime_tm = NULL;
|
|
|
|
int i;
|
|
|
|
int i, c, b, level, cont, bright, min, max;
|
|
|
|
|
|
|
|
|
|
|
|
// for debugging only
|
|
|
|
// for debugging only
|
|
|
|
curtime_tm = localtime(&curtime);
|
|
|
|
curtime_tm = localtime(&curtime);
|
|
|
@ -92,9 +94,38 @@ int main(int argc, char **argv) {
|
|
|
|
// printf ("vidoiltank\n");
|
|
|
|
// printf ("vidoiltank\n");
|
|
|
|
// printf (" device: %s (%d x %d)\n", conf_devname, conf_width, conf_height);
|
|
|
|
// printf (" device: %s (%d x %d)\n", conf_devname, conf_width, conf_height);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// open video device
|
|
|
|
if ((video = vid_open("/dev/video0")) == NULL) return -1;
|
|
|
|
if ((video = vid_open("/dev/video0")) == NULL) return -1;
|
|
|
|
if (vid_start(video, conf_iomode, conf_width, conf_height) == -1) return -1;
|
|
|
|
if (vid_start(video, conf_iomode, conf_width, conf_height) == -1) return -1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// search for god brightness and contrast
|
|
|
|
|
|
|
|
printf ("check controls\n");
|
|
|
|
|
|
|
|
cont = 200; // just picked some random numbers
|
|
|
|
|
|
|
|
bright = 170; // just picked some random numbers
|
|
|
|
|
|
|
|
for (level = 0, c = 32; c <= 250; c += 64)
|
|
|
|
|
|
|
|
for (b = 32; b <= 250; b += 64) {
|
|
|
|
|
|
|
|
vid_setctrls(video, b, c);
|
|
|
|
|
|
|
|
do {} while ((tmpimg = vid_grabimage(video, img)) == NULL && errno == EAGAIN);
|
|
|
|
|
|
|
|
if (tmpimg == NULL) {
|
|
|
|
|
|
|
|
printf ("could not grab image\n");
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
img = tmpimg;
|
|
|
|
|
|
|
|
image_get_minmax (img, &conf_rect, &min, &max);
|
|
|
|
|
|
|
|
if (max-min > level) {
|
|
|
|
|
|
|
|
cont = c;
|
|
|
|
|
|
|
|
bright = b;
|
|
|
|
|
|
|
|
level = max-min;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("found best control: contrast: %d brightness: %d\n", cont, bright);
|
|
|
|
|
|
|
|
vid_setctrls(video, bright, cont);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// get samples
|
|
|
|
|
|
|
|
printf ("Read Samples\n");
|
|
|
|
for (i = 0; i < conf_samples; i++) {
|
|
|
|
for (i = 0; i < conf_samples; i++) {
|
|
|
|
do {} while ((tmpimg = vid_grabimage(video, img)) == NULL && errno == EAGAIN);
|
|
|
|
do {} while ((tmpimg = vid_grabimage(video, img)) == NULL && errno == EAGAIN);
|
|
|
|
if (tmpimg == NULL) {
|
|
|
|
if (tmpimg == NULL) {
|
|
|
@ -110,7 +141,7 @@ int main(int argc, char **argv) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
if (i == 0) {
|
|
|
|
snprintf (fn, 3*LEN_FILENAME, "%s/%s-img-%02d.jpg", conf_debugout, outdate, i);
|
|
|
|
snprintf (fn, 3*LEN_FILENAME, "%s/%s-img-%02d-%02d.jpg", conf_debugout, outdate, bright, cont);
|
|
|
|
image_draw_rect(img, &conf_rect, 0x00FFFF);
|
|
|
|
image_draw_rect(img, &conf_rect, 0x00FFFF);
|
|
|
|
image_save(img, fn, 98);
|
|
|
|
image_save(img, fn, 98);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -123,8 +154,63 @@ int main(int argc, char **argv) {
|
|
|
|
snprintf (fn, 3*LEN_FILENAME, "%s/%s-imgf-%03d.jpg", conf_debugout, outdate, conf_samples);
|
|
|
|
snprintf (fn, 3*LEN_FILENAME, "%s/%s-imgf-%03d.jpg", conf_debugout, outdate, conf_samples);
|
|
|
|
image_save(tmpimg, fn, 98);
|
|
|
|
image_save(tmpimg, fn, 98);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
level = detect_level(imgf);
|
|
|
|
|
|
|
|
tmpimg = image_copyF(imgf);
|
|
|
|
|
|
|
|
Rect r = {.x = 0, .y = level, .w = 10, .h = 1};
|
|
|
|
|
|
|
|
image_draw_rect(tmpimg, &r, 0x0000FF00);
|
|
|
|
|
|
|
|
snprintf (fn, 3*LEN_FILENAME, "%s/%s-img-data.jpg", conf_debugout, outdate);
|
|
|
|
|
|
|
|
image_save(tmpimg, fn, 98);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf ("%s Found Level: %5.2f Y-Pos: %d\n", outdate, 100.0 * (1.0-(float)level/(float)imgf->h), level);
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int detect_level(ImageFloat *imgf) {
|
|
|
|
|
|
|
|
int y, x, ylow, level;
|
|
|
|
|
|
|
|
float v = 0.0, vmin, vmax;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// calculate average, and save value in left row
|
|
|
|
|
|
|
|
for (y = 0; y < imgf->h; y++) {
|
|
|
|
|
|
|
|
for (v = 0.0, x = 0; x < imgf->w; x++)
|
|
|
|
|
|
|
|
v += imgf->data[x + y * imgf->w];
|
|
|
|
|
|
|
|
v = v / (float) x;
|
|
|
|
|
|
|
|
if (y == 0) vmin = vmax = v;
|
|
|
|
|
|
|
|
if (vmin > v) vmin = v;
|
|
|
|
|
|
|
|
if (vmax < v) vmax = v;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// save result in the top left position,
|
|
|
|
|
|
|
|
// together with a bar for display
|
|
|
|
|
|
|
|
imgf->data[y * imgf->w] = v;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// normalize 0.0 .. 1.0
|
|
|
|
|
|
|
|
for (y = 0; y < imgf->h; y++) for (x = 0; x < imgf->w; x++) {
|
|
|
|
|
|
|
|
imgf->data[x + y * imgf->w] = (imgf->data[x + y * imgf->w]-vmin)/(vmax-vmin);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// search for the level from below
|
|
|
|
|
|
|
|
vmin = 1.0;
|
|
|
|
|
|
|
|
vmax = 0.0;
|
|
|
|
|
|
|
|
int avg = 8;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
level = 0;
|
|
|
|
|
|
|
|
for (ylow = 0; ylow < imgf->h; ylow++) {
|
|
|
|
|
|
|
|
if (ylow == 0) v = imgf->data[(ylow) * imgf->w];
|
|
|
|
|
|
|
|
v = ((float)avg-1.0) * v/(float)avg + imgf->data[(ylow) * imgf->w]/(float) avg;
|
|
|
|
|
|
|
|
for (x = 0; x < 8; x++) imgf->data[x + (ylow) * imgf->w] = v;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ylow = imgf->h - 1; ylow >= 0; ylow--) {
|
|
|
|
|
|
|
|
if (ylow == imgf->h - 1) v = imgf->data[(ylow) * imgf->w];
|
|
|
|
|
|
|
|
v = ((float)avg-1.0) * v/(float)avg + imgf->data[(ylow) * imgf->w]/(float) avg;
|
|
|
|
|
|
|
|
for (x = 4; x < 16; x++) imgf->data[x + (ylow) * imgf->w] = v;
|
|
|
|
|
|
|
|
if (v < 0.1) level = ylow;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return level;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|