11 #define debug(format, ...) fprintf (stderr, (format),## __VA_ARGS__); fflush(stderr)
12 #define debug_lineno() debug("%-25s line %4d \n", __func__, __LINE__ )
15 #define IMG_IN_NAME "images/test-image.png"
16 #define IMG_OUT_NAME "images/test-image-%s.png"
20 expand_one_mono(int x
, int y
, int c
,
21 CvPoint
*nexts
, int *n_nexts
, guint8
*im
, guint8
*mask
, int w
, int h
){
22 guint8 p
= im
[y
* w
+ x
];
23 guint8
*m
= &mask
[y
* w
+ x
];
26 nexts
[*n_nexts
].x
= x
;
27 nexts
[*n_nexts
].y
= y
;
33 floodfill_mono_superfast(IplImage
*im
, IplImage
*mim
, CvPoint start
)
35 guint8
* data
= (guint8
*)im
->imageData
;
36 guint8
* mdata
= (guint8
*)mim
->imageData
;
44 //malloc 2 lists of points. These *could* be as large as the image (but never should be)
45 void * mem
= malloc(w
* h
* 2 * sizeof(CvPoint
));
47 nexts
= starts
+ w
* h
;
55 for (i
= 0; i
< n_starts
; i
++){
58 int c
= data
[y
* w
+ x
];
60 expand_one_mono(x
- 1, y
, c
, nexts
, &n_nexts
, data
, mdata
, w
, h
);
63 expand_one_mono(x
+ 1, y
, c
, nexts
, &n_nexts
, data
, mdata
, w
, h
);
66 expand_one_mono(x
, y
- 1, c
, nexts
, &n_nexts
, data
, mdata
, w
, h
);
69 expand_one_mono(x
, y
+ 1, c
, nexts
, &n_nexts
, data
, mdata
, w
, h
);
72 CvPoint
*tmp
= starts
;
83 static UNUSED IplImage
*
84 test_find_edges_hist(IplImage
*im
)
88 CvSize small_size
= {w
/ 8, h
/ 8};
89 CvPoint middle
= {w
/2, h
/2};
91 IplImage
*small
= cvCreateImage(small_size
, IPL_DEPTH_8U
, 1); /*for quicker histogram */
92 IplImage
*mask
= cvCreateImage(cvGetSize(im
), IPL_DEPTH_8U
, 1);
93 IplImage
*green
= cvCreateImage(cvGetSize(im
), IPL_DEPTH_8U
, 1);
94 cvSplit(im
, NULL
, green
, NULL
, NULL
);
96 cvResize(green
, small
, CV_INTER_NN
);
99 int hist_size
[] = {255};
100 float range
[] = {0, 255};
101 float *ranges
[] = {range
};
102 CvHistogram
* hist
= cvCreateHist(1, hist_size
, CV_HIST_ARRAY
, ranges
, 1);
103 cvCalcHist(&small
, hist
, 0, NULL
);
105 int pixels
= small
->width
* small
->height
;
106 int min_black
= pixels
/ 8;
107 int max_black
= pixels
/ 2;
108 int totals
[256] = {0};
110 int best_d
= pixels
+ 1;
114 for (int i
= 0; i
< 255; i
++){
115 int v
= (int)cvQueryHistValue_1D(hist
, i
+ 2);
118 if (total
> min_black
){
120 int diff
= totals
[i
] - totals
[i
- 5];
125 if (total
>= max_black
){
132 printf("found best threshold %d -- %d pixel change at %d/%d pixels\n",
133 best_t
, best_d
, totals
[best_t
], pixels
);
135 cvCmpS(green
, best_t
, mask
, CV_CMP_GT
);
136 IplImage
*mask2
= cvCreateImage(cvGetSize(im
), IPL_DEPTH_8U
, 1);
137 memset(mask2
->imageData
, 255, w
*h
);
138 floodfill_mono_superfast(mask
, mask2
, middle
);
144 int main(int argc
, char **argv
)
146 struct timeval tv1
, tv2
;
149 IplImage
*im_in
= cvLoadImage(IMG_IN_NAME
, CV_LOAD_IMAGE_UNCHANGED
);
150 //IplImage *im_out = cvCreateImage(CvSize size, int depth, int channels);
151 //IplImage *im_out = cvCloneImage(im);
153 gettimeofday(&tv1
, NULL
);
156 IplImage
*im_out
= test_find_edges_hist(im_in
);
157 //IplImage *im_out = test_find_edges(im_in);
160 gettimeofday(&tv2
, NULL
);
161 t
= ((tv2
.tv_sec
- tv1
.tv_sec
) * 1000000 +
162 tv2
.tv_usec
- tv1
.tv_usec
);
163 printf("took %u microseconds (%0.5f of a frame)\n",
164 t
, (double)t
* (25.0 / 1000000.0));
168 if(asprintf(&filename
, IMG_OUT_NAME
, "final") == -1){};
169 cvSaveImage(filename
, im_out
, 0);