1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 #include "precomp.hpp"
44 #if defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 8)
45 # pragma GCC diagnostic ignored "-Warray-bounds"
48 typedef struct CvFFillSegment
62 #define ICV_PUSH( Y, L, R, PREV_L, PREV_R, DIR ) \
64 tail->y = (ushort)(Y); \
65 tail->l = (ushort)(L); \
66 tail->r = (ushort)(R); \
67 tail->prevl = (ushort)(PREV_L); \
68 tail->prevr = (ushort)(PREV_R); \
69 tail->dir = (short)(DIR); \
70 if( ++tail == buffer_end ) \
72 buffer->resize(buffer->size() * 2); \
73 tail = &buffer->front() + (tail - head); \
74 head = &buffer->front(); \
75 buffer_end = head + buffer->size(); \
79 #define ICV_POP( Y, L, R, PREV_L, PREV_R, DIR ) \
85 PREV_L = tail->prevl; \
86 PREV_R = tail->prevr; \
90 /****************************************************************************************\
91 * Simple Floodfill (repainting single-color connected component) *
92 \****************************************************************************************/
94 template<typename _Tp>
96 icvFloodFill_CnIR( uchar* pImage, int step, CvSize roi, CvPoint seed,
97 _Tp newVal, CvConnectedComp* region, int flags,
98 std::vector<CvFFillSegment>* buffer )
100 _Tp* img = (_Tp*)(pImage + step * seed.y);
103 int XMin, XMax, YMin = seed.y, YMax = seed.y;
104 int _8_connectivity = (flags & 255) == 8;
105 CvFFillSegment* buffer_end = &buffer->front() + buffer->size(), *head = &buffer->front(), *tail = &buffer->front();
107 L = R = XMin = XMax = seed.x;
112 while( ++R < roi.width && img[R] == val0 )
115 while( --L >= 0 && img[L] == val0 )
121 ICV_PUSH( seed.y, L, R, R + 1, R, UP );
123 while( head != tail )
125 int k, YC, PL, PR, dir;
126 ICV_POP( YC, L, R, PL, PR, dir );
130 {-dir, L - _8_connectivity, R + _8_connectivity},
131 {dir, L - _8_connectivity, PL - 1},
132 {dir, PR + 1, R + _8_connectivity}
139 if( XMax < R ) XMax = R;
140 if( XMin > L ) XMin = L;
141 if( YMax < YC ) YMax = YC;
142 if( YMin > YC ) YMin = YC;
145 for( k = 0; k < 3; k++ )
148 img = (_Tp*)(pImage + (YC + dir) * step);
149 int left = data[k][1];
150 int right = data[k][2];
152 if( (unsigned)(YC + dir) >= (unsigned)roi.height )
155 for( i = left; i <= right; i++ )
157 if( (unsigned)i < (unsigned)roi.width && img[i] == val0 )
161 while( --j >= 0 && img[j] == val0 )
164 while( ++i < roi.width && img[i] == val0 )
167 ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
176 region->rect.x = XMin;
177 region->rect.y = YMin;
178 region->rect.width = XMax - XMin + 1;
179 region->rect.height = YMax - YMin + 1;
180 region->value = cv::Scalar(newVal);
184 /****************************************************************************************\
185 * Gradient Floodfill *
186 \****************************************************************************************/
190 Diff8uC1(uchar _lo, uchar _up) : lo(_lo), interval(_lo + _up) {}
191 bool operator()(const uchar* a, const uchar* b) const
192 { return (unsigned)(a[0] - b[0] + lo) <= interval; }
193 unsigned lo, interval;
198 Diff8uC3(cv::Vec3b _lo, cv::Vec3b _up)
200 for( int k = 0; k < 3; k++ )
201 lo[k] = _lo[k], interval[k] = _lo[k] + _up[k];
203 bool operator()(const cv::Vec3b* a, const cv::Vec3b* b) const
205 return (unsigned)(a[0][0] - b[0][0] + lo[0]) <= interval[0] &&
206 (unsigned)(a[0][1] - b[0][1] + lo[1]) <= interval[1] &&
207 (unsigned)(a[0][2] - b[0][2] + lo[2]) <= interval[2];
209 unsigned lo[3], interval[3];
212 template<typename _Tp>
215 DiffC1(_Tp _lo, _Tp _up) : lo(-_lo), up(_up) {}
216 bool operator()(const _Tp* a, const _Tp* b) const
219 return lo <= d && d <= up;
224 template<typename _Tp>
227 DiffC3(_Tp _lo, _Tp _up) : lo(-_lo), up(_up) {}
228 bool operator()(const _Tp* a, const _Tp* b) const
231 return lo[0] <= d[0] && d[0] <= up[0] &&
232 lo[1] <= d[1] && d[1] <= up[1] &&
233 lo[2] <= d[2] && d[2] <= up[2];
238 typedef DiffC1<int> Diff32sC1;
239 typedef DiffC3<cv::Vec3i> Diff32sC3;
240 typedef DiffC1<float> Diff32fC1;
241 typedef DiffC3<cv::Vec3f> Diff32fC3;
243 static cv::Vec3i& operator += (cv::Vec3i& a, const cv::Vec3b& b)
251 template<typename _Tp, typename _WTp, class Diff>
253 icvFloodFillGrad_CnIR( uchar* pImage, int step, uchar* pMask, int maskStep,
254 CvSize /*roi*/, CvPoint seed, _Tp newVal, Diff diff,
255 CvConnectedComp* region, int flags,
256 std::vector<CvFFillSegment>* buffer )
258 _Tp* img = (_Tp*)(pImage + step*seed.y);
259 uchar* mask = (pMask += maskStep + 1) + maskStep*seed.y;
262 _WTp sum = _WTp((typename cv::DataType<_Tp>::channel_type)0);
263 int XMin, XMax, YMin = seed.y, YMax = seed.y;
264 int _8_connectivity = (flags & 255) == 8;
265 int fixedRange = flags & CV_FLOODFILL_FIXED_RANGE;
266 int fillImage = (flags & CV_FLOODFILL_MASK_ONLY) == 0;
267 uchar newMaskVal = (uchar)(flags & 0xff00 ? flags >> 8 : 1);
268 CvFFillSegment* buffer_end = &buffer->front() + buffer->size(), *head = &buffer->front(), *tail = &buffer->front();
274 mask[L] = newMaskVal;
279 while( !mask[R + 1] && diff( img + (R+1), &val0 ))
280 mask[++R] = newMaskVal;
282 while( !mask[L - 1] && diff( img + (L-1), &val0 ))
283 mask[--L] = newMaskVal;
287 while( !mask[R + 1] && diff( img + (R+1), img + R ))
288 mask[++R] = newMaskVal;
290 while( !mask[L - 1] && diff( img + (L-1), img + L ))
291 mask[--L] = newMaskVal;
297 ICV_PUSH( seed.y, L, R, R + 1, R, UP );
299 while( head != tail )
301 int k, YC, PL, PR, dir;
302 ICV_POP( YC, L, R, PL, PR, dir );
306 {-dir, L - _8_connectivity, R + _8_connectivity},
307 {dir, L - _8_connectivity, PL - 1},
308 {dir, PR + 1, R + _8_connectivity}
311 unsigned length = (unsigned)(R-L);
315 area += (int)length + 1;
317 if( XMax < R ) XMax = R;
318 if( XMin > L ) XMin = L;
319 if( YMax < YC ) YMax = YC;
320 if( YMin > YC ) YMin = YC;
323 for( k = 0; k < 3; k++ )
326 img = (_Tp*)(pImage + (YC + dir) * step);
327 _Tp* img1 = (_Tp*)(pImage + YC * step);
328 mask = pMask + (YC + dir) * maskStep;
329 int left = data[k][1];
330 int right = data[k][2];
333 for( i = left; i <= right; i++ )
335 if( !mask[i] && diff( img + i, &val0 ))
338 mask[i] = newMaskVal;
339 while( !mask[--j] && diff( img + j, &val0 ))
340 mask[j] = newMaskVal;
342 while( !mask[++i] && diff( img + i, &val0 ))
343 mask[i] = newMaskVal;
345 ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
348 else if( !_8_connectivity )
349 for( i = left; i <= right; i++ )
351 if( !mask[i] && diff( img + i, img1 + i ))
354 mask[i] = newMaskVal;
355 while( !mask[--j] && diff( img + j, img + (j+1) ))
356 mask[j] = newMaskVal;
359 (diff( img + i, img + (i-1) ) ||
360 (diff( img + i, img1 + i) && i <= R)))
361 mask[i] = newMaskVal;
363 ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
367 for( i = left; i <= right; i++ )
374 (unsigned)(idx = i-L-1) <= length) &&
375 diff( &val, img1 + (i-1))) ||
376 ((unsigned)(++idx) <= length &&
377 diff( &val, img1 + i )) ||
378 ((unsigned)(++idx) <= length &&
379 diff( &val, img1 + (i+1) ))))
382 mask[i] = newMaskVal;
383 while( !mask[--j] && diff( img + j, img + (j+1) ))
384 mask[j] = newMaskVal;
388 diff( &val, img + (i-1) )) ||
389 (((unsigned)(idx = i-L-1) <= length &&
390 diff( &val, img1 + (i-1) ))) ||
391 ((unsigned)(++idx) <= length &&
392 diff( &val, img1 + i )) ||
393 ((unsigned)(++idx) <= length &&
394 diff( &val, img1 + (i+1) ))))
395 mask[i] = newMaskVal;
397 ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
402 img = (_Tp*)(pImage + YC * step);
404 for( i = L; i <= R; i++ )
407 for( i = L; i <= R; i++ )
414 region->rect.x = XMin;
415 region->rect.y = YMin;
416 region->rect.width = XMax - XMin + 1;
417 region->rect.height = YMax - YMin + 1;
420 region->value = cv::Scalar(newVal);
423 double iarea = area ? 1./area : 0;
424 region->value = cv::Scalar(sum*iarea);
430 /****************************************************************************************\
431 * External Functions *
432 \****************************************************************************************/
434 typedef void (*CvFloodFillFunc)(
435 void* img, int step, CvSize size, CvPoint seed, void* newval,
436 CvConnectedComp* comp, int flags, void* buffer, int cn );
438 typedef void (*CvFloodFillGradFunc)(
439 void* img, int step, uchar* mask, int maskStep, CvSize size,
440 CvPoint seed, void* newval, void* d_lw, void* d_up, void* ccomp,
441 int flags, void* buffer, int cn );
444 cvFloodFill( CvArr* arr, CvPoint seed_point,
445 CvScalar newVal, CvScalar lo_diff, CvScalar up_diff,
446 CvConnectedComp* comp, int flags, CvArr* maskarr )
448 cv::Ptr<CvMat> tempMask;
449 std::vector<CvFFillSegment> buffer;
452 memset( comp, 0, sizeof(*comp) );
454 int i, type, depth, cn, is_simple;
455 int buffer_size, connectivity = flags & 255;
462 nv_buf._[0] = nv_buf._[1] = nv_buf._[2] = nv_buf._[3] = 0;
464 struct { cv::Vec3b b; cv::Vec3i i; cv::Vec3f f; } ld_buf, ud_buf;
465 CvMat stub, *img = cvGetMat(arr, &stub);
466 CvMat maskstub, *mask = (CvMat*)maskarr;
469 type = CV_MAT_TYPE( img->type );
470 depth = CV_MAT_DEPTH(type);
471 cn = CV_MAT_CN(type);
473 if( connectivity == 0 )
475 else if( connectivity != 4 && connectivity != 8 )
476 CV_Error( CV_StsBadFlag, "Connectivity must be 4, 0(=4) or 8" );
478 is_simple = mask == 0 && (flags & CV_FLOODFILL_MASK_ONLY) == 0;
480 for( i = 0; i < cn; i++ )
482 if( lo_diff.val[i] < 0 || up_diff.val[i] < 0 )
483 CV_Error( CV_StsBadArg, "lo_diff and up_diff must be non-negative" );
484 is_simple &= fabs(lo_diff.val[i]) < DBL_EPSILON && fabs(up_diff.val[i]) < DBL_EPSILON;
487 size = cvGetMatSize( img );
489 if( (unsigned)seed_point.x >= (unsigned)size.width ||
490 (unsigned)seed_point.y >= (unsigned)size.height )
491 CV_Error( CV_StsOutOfRange, "Seed point is outside of image" );
493 cvScalarToRawData( &newVal, &nv_buf, type, 0 );
494 buffer_size = MAX( size.width, size.height ) * 2;
495 buffer.resize( buffer_size );
499 int elem_size = CV_ELEM_SIZE(type);
500 const uchar* seed_ptr = img->data.ptr + img->step*seed_point.y + elem_size*seed_point.x;
502 for(i = 0; i < elem_size; i++)
503 if (seed_ptr[i] != nv_buf.b[i])
508 if( type == CV_8UC1 )
509 icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, nv_buf.b[0],
510 comp, flags, &buffer);
511 else if( type == CV_8UC3 )
512 icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, cv::Vec3b(nv_buf.b),
513 comp, flags, &buffer);
514 else if( type == CV_32SC1 )
515 icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, nv_buf.i[0],
516 comp, flags, &buffer);
517 else if( type == CV_32FC1 )
518 icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, nv_buf.f[0],
519 comp, flags, &buffer);
520 else if( type == CV_32SC3 )
521 icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, cv::Vec3i(nv_buf.i),
522 comp, flags, &buffer);
523 else if( type == CV_32FC3 )
524 icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, cv::Vec3f(nv_buf.f),
525 comp, flags, &buffer);
527 CV_Error( CV_StsUnsupportedFormat, "" );
534 /* created mask will be 8-byte aligned */
535 tempMask = cvCreateMat( size.height + 2, (size.width + 9) & -8, CV_8UC1 );
540 mask = cvGetMat( mask, &maskstub );
541 if( !CV_IS_MASK_ARR( mask ))
542 CV_Error( CV_StsBadMask, "" );
544 if( mask->width != size.width + 2 || mask->height != size.height + 2 )
545 CV_Error( CV_StsUnmatchedSizes, "mask must be 2 pixel wider "
546 "and 2 pixel taller than filled image" );
549 int width = tempMask ? mask->step : size.width + 2;
550 uchar* mask_row = mask->data.ptr + mask->step;
551 memset( mask_row - mask->step, 1, width );
553 for( i = 1; i <= size.height; i++, mask_row += mask->step )
556 memset( mask_row, 0, width );
557 mask_row[0] = mask_row[size.width+1] = (uchar)1;
559 memset( mask_row, 1, width );
562 for( i = 0; i < cn; i++ )
564 int t = cvFloor(lo_diff.val[i]);
565 ld_buf.b[i] = CV_CAST_8U(t);
566 t = cvFloor(up_diff.val[i]);
567 ud_buf.b[i] = CV_CAST_8U(t);
569 else if( depth == CV_32S )
570 for( i = 0; i < cn; i++ )
572 int t = cvFloor(lo_diff.val[i]);
574 t = cvFloor(up_diff.val[i]);
577 else if( depth == CV_32F )
578 for( i = 0; i < cn; i++ )
580 ld_buf.f[i] = (float)lo_diff.val[i];
581 ud_buf.f[i] = (float)up_diff.val[i];
584 CV_Error( CV_StsUnsupportedFormat, "" );
586 if( type == CV_8UC1 )
587 icvFloodFillGrad_CnIR<uchar, int, Diff8uC1>(
588 img->data.ptr, img->step, mask->data.ptr, mask->step,
589 size, seed_point, nv_buf.b[0],
590 Diff8uC1(ld_buf.b[0], ud_buf.b[0]),
591 comp, flags, &buffer);
592 else if( type == CV_8UC3 )
593 icvFloodFillGrad_CnIR<cv::Vec3b, cv::Vec3i, Diff8uC3>(
594 img->data.ptr, img->step, mask->data.ptr, mask->step,
595 size, seed_point, cv::Vec3b(nv_buf.b),
596 Diff8uC3(ld_buf.b, ud_buf.b),
597 comp, flags, &buffer);
598 else if( type == CV_32SC1 )
599 icvFloodFillGrad_CnIR<int, int, Diff32sC1>(
600 img->data.ptr, img->step, mask->data.ptr, mask->step,
601 size, seed_point, nv_buf.i[0],
602 Diff32sC1(ld_buf.i[0], ud_buf.i[0]),
603 comp, flags, &buffer);
604 else if( type == CV_32SC3 )
605 icvFloodFillGrad_CnIR<cv::Vec3i, cv::Vec3i, Diff32sC3>(
606 img->data.ptr, img->step, mask->data.ptr, mask->step,
607 size, seed_point, cv::Vec3i(nv_buf.i),
608 Diff32sC3(ld_buf.i, ud_buf.i),
609 comp, flags, &buffer);
610 else if( type == CV_32FC1 )
611 icvFloodFillGrad_CnIR<float, float, Diff32fC1>(
612 img->data.ptr, img->step, mask->data.ptr, mask->step,
613 size, seed_point, nv_buf.f[0],
614 Diff32fC1(ld_buf.f[0], ud_buf.f[0]),
615 comp, flags, &buffer);
616 else if( type == CV_32FC3 )
617 icvFloodFillGrad_CnIR<cv::Vec3f, cv::Vec3f, Diff32fC3>(
618 img->data.ptr, img->step, mask->data.ptr, mask->step,
619 size, seed_point, cv::Vec3f(nv_buf.f),
620 Diff32fC3(ld_buf.f, ud_buf.f),
621 comp, flags, &buffer);
623 CV_Error(CV_StsUnsupportedFormat, "");
627 int cv::floodFill( InputOutputArray _image, Point seedPoint,
628 Scalar newVal, Rect* rect,
629 Scalar loDiff, Scalar upDiff, int flags )
631 CvConnectedComp ccomp;
632 CvMat c_image = _image.getMat();
633 cvFloodFill(&c_image, seedPoint, newVal, loDiff, upDiff, &ccomp, flags, 0);
636 return cvRound(ccomp.area);
639 int cv::floodFill( InputOutputArray _image, InputOutputArray _mask,
640 Point seedPoint, Scalar newVal, Rect* rect,
641 Scalar loDiff, Scalar upDiff, int flags )
643 CvConnectedComp ccomp;
644 CvMat c_image = _image.getMat(), c_mask = _mask.getMat();
645 cvFloodFill(&c_image, seedPoint, newVal, loDiff, upDiff, &ccomp, flags, c_mask.data.ptr ? &c_mask : 0);
648 return cvRound(ccomp.area);