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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "precomp.hpp"
45 #if defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 8)
46 # pragma GCC diagnostic ignored "-Warray-bounds"
68 #define ICV_PUSH( Y, L, R, PREV_L, PREV_R, DIR ) \
70 tail->y = (ushort)(Y); \
71 tail->l = (ushort)(L); \
72 tail->r = (ushort)(R); \
73 tail->prevl = (ushort)(PREV_L); \
74 tail->prevr = (ushort)(PREV_R); \
75 tail->dir = (short)(DIR); \
76 if( ++tail == buffer_end ) \
78 buffer->resize(buffer->size() * 3/2); \
79 tail = &buffer->front() + (tail - head); \
80 head = &buffer->front(); \
81 buffer_end = head + buffer->size(); \
85 #define ICV_POP( Y, L, R, PREV_L, PREV_R, DIR ) \
91 PREV_L = tail->prevl; \
92 PREV_R = tail->prevr; \
115 ConnectedComp::ConnectedComp()
117 rect = Rect(0, 0, 0, 0);
121 area = harea = carea = perimeter = nholes = ninflections = 0;
123 avg = sdv = Scalar::all(0);
126 // Simple Floodfill (repainting single-color connected component)
128 template<typename _Tp>
130 floodFill_CnIR( Mat& image, Point seed,
131 _Tp newVal, ConnectedComp* region, int flags,
132 std::vector<FFillSegment>* buffer )
134 _Tp* img = image.ptr<_Tp>(seed.y);
135 Size roi = image.size();
138 int XMin, XMax, YMin = seed.y, YMax = seed.y;
139 int _8_connectivity = (flags & 255) == 8;
140 FFillSegment* buffer_end = &buffer->front() + buffer->size(), *head = &buffer->front(), *tail = &buffer->front();
142 L = R = XMin = XMax = seed.x;
147 while( ++R < roi.width && img[R] == val0 )
150 while( --L >= 0 && img[L] == val0 )
156 ICV_PUSH( seed.y, L, R, R + 1, R, UP );
158 while( head != tail )
160 int k, YC, PL, PR, dir;
161 ICV_POP( YC, L, R, PL, PR, dir );
165 {-dir, L - _8_connectivity, R + _8_connectivity},
166 {dir, L - _8_connectivity, PL - 1},
167 {dir, PR + 1, R + _8_connectivity}
174 if( XMax < R ) XMax = R;
175 if( XMin > L ) XMin = L;
176 if( YMax < YC ) YMax = YC;
177 if( YMin > YC ) YMin = YC;
180 for( k = 0; k < 3; k++ )
184 if( (unsigned)(YC + dir) >= (unsigned)roi.height )
187 img = image.ptr<_Tp>(YC + dir);
188 int left = data[k][1];
189 int right = data[k][2];
191 for( i = left; i <= right; i++ )
193 if( (unsigned)i < (unsigned)roi.width && img[i] == val0 )
197 while( --j >= 0 && img[j] == val0 )
200 while( ++i < roi.width && img[i] == val0 )
203 ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
213 region->rect.x = XMin;
214 region->rect.y = YMin;
215 region->rect.width = XMax - XMin + 1;
216 region->rect.height = YMax - YMin + 1;
220 /****************************************************************************************\
221 * Gradient Floodfill *
222 \****************************************************************************************/
226 Diff8uC1(uchar _lo, uchar _up) : lo(_lo), interval(_lo + _up) {}
227 bool operator()(const uchar* a, const uchar* b) const
228 { return (unsigned)(a[0] - b[0] + lo) <= interval; }
229 unsigned lo, interval;
234 Diff8uC3(Vec3b _lo, Vec3b _up)
236 for( int k = 0; k < 3; k++ )
237 lo[k] = _lo[k], interval[k] = _lo[k] + _up[k];
239 bool operator()(const Vec3b* a, const Vec3b* b) const
241 return (unsigned)(a[0][0] - b[0][0] + lo[0]) <= interval[0] &&
242 (unsigned)(a[0][1] - b[0][1] + lo[1]) <= interval[1] &&
243 (unsigned)(a[0][2] - b[0][2] + lo[2]) <= interval[2];
245 unsigned lo[3], interval[3];
248 template<typename _Tp>
251 DiffC1(_Tp _lo, _Tp _up) : lo(-_lo), up(_up) {}
252 bool operator()(const _Tp* a, const _Tp* b) const
255 return lo <= d && d <= up;
260 template<typename _Tp>
263 DiffC3(_Tp _lo, _Tp _up) : lo(-_lo), up(_up) {}
264 bool operator()(const _Tp* a, const _Tp* b) const
267 return lo[0] <= d[0] && d[0] <= up[0] &&
268 lo[1] <= d[1] && d[1] <= up[1] &&
269 lo[2] <= d[2] && d[2] <= up[2];
274 typedef DiffC1<int> Diff32sC1;
275 typedef DiffC3<Vec3i> Diff32sC3;
276 typedef DiffC1<float> Diff32fC1;
277 typedef DiffC3<Vec3f> Diff32fC3;
279 template<typename _Tp, typename _MTp, typename _WTp, class Diff>
281 floodFillGrad_CnIR( Mat& image, Mat& msk,
282 Point seed, _Tp newVal, _MTp newMaskVal,
283 Diff diff, ConnectedComp* region, int flags,
284 std::vector<FFillSegment>* buffer )
286 int step = (int)image.step, maskStep = (int)msk.step;
287 uchar* pImage = image.ptr();
288 _Tp* img = (_Tp*)(pImage + step*seed.y);
289 uchar* pMask = msk.ptr() + maskStep + sizeof(_MTp);
290 _MTp* mask = (_MTp*)(pMask + maskStep*seed.y);
293 int XMin, XMax, YMin = seed.y, YMax = seed.y;
294 int _8_connectivity = (flags & 255) == 8;
295 int fixedRange = flags & FLOODFILL_FIXED_RANGE;
296 int fillImage = (flags & FLOODFILL_MASK_ONLY) == 0;
297 FFillSegment* buffer_end = &buffer->front() + buffer->size(), *head = &buffer->front(), *tail = &buffer->front();
303 mask[L] = newMaskVal;
308 while( !mask[R + 1] && diff( img + (R+1), &val0 ))
309 mask[++R] = newMaskVal;
311 while( !mask[L - 1] && diff( img + (L-1), &val0 ))
312 mask[--L] = newMaskVal;
316 while( !mask[R + 1] && diff( img + (R+1), img + R ))
317 mask[++R] = newMaskVal;
319 while( !mask[L - 1] && diff( img + (L-1), img + L ))
320 mask[--L] = newMaskVal;
326 ICV_PUSH( seed.y, L, R, R + 1, R, UP );
328 while( head != tail )
330 int k, YC, PL, PR, dir;
331 ICV_POP( YC, L, R, PL, PR, dir );
335 {-dir, L - _8_connectivity, R + _8_connectivity},
336 {dir, L - _8_connectivity, PL - 1},
337 {dir, PR + 1, R + _8_connectivity}
340 unsigned length = (unsigned)(R-L);
344 area += (int)length + 1;
346 if( XMax < R ) XMax = R;
347 if( XMin > L ) XMin = L;
348 if( YMax < YC ) YMax = YC;
349 if( YMin > YC ) YMin = YC;
352 for( k = 0; k < 3; k++ )
355 img = (_Tp*)(pImage + (YC + dir) * step);
356 _Tp* img1 = (_Tp*)(pImage + YC * step);
357 mask = (_MTp*)(pMask + (YC + dir) * maskStep);
358 int left = data[k][1];
359 int right = data[k][2];
362 for( i = left; i <= right; i++ )
364 if( !mask[i] && diff( img + i, &val0 ))
367 mask[i] = newMaskVal;
368 while( !mask[--j] && diff( img + j, &val0 ))
369 mask[j] = newMaskVal;
371 while( !mask[++i] && diff( img + i, &val0 ))
372 mask[i] = newMaskVal;
374 ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
377 else if( !_8_connectivity )
378 for( i = left; i <= right; i++ )
380 if( !mask[i] && diff( img + i, img1 + i ))
383 mask[i] = newMaskVal;
384 while( !mask[--j] && diff( img + j, img + (j+1) ))
385 mask[j] = newMaskVal;
388 (diff( img + i, img + (i-1) ) ||
389 (diff( img + i, img1 + i) && i <= R)))
390 mask[i] = newMaskVal;
392 ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
396 for( i = left; i <= right; i++ )
403 (unsigned)(idx = i-L-1) <= length) &&
404 diff( &val, img1 + (i-1))) ||
405 ((unsigned)(++idx) <= length &&
406 diff( &val, img1 + i )) ||
407 ((unsigned)(++idx) <= length &&
408 diff( &val, img1 + (i+1) ))))
411 mask[i] = newMaskVal;
412 while( !mask[--j] && diff( img + j, img + (j+1) ))
413 mask[j] = newMaskVal;
417 diff( &val, img + (i-1) )) ||
418 (((unsigned)(idx = i-L-1) <= length &&
419 diff( &val, img1 + (i-1) ))) ||
420 ((unsigned)(++idx) <= length &&
421 diff( &val, img1 + i )) ||
422 ((unsigned)(++idx) <= length &&
423 diff( &val, img1 + (i+1) ))))
424 mask[i] = newMaskVal;
426 ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
431 img = (_Tp*)(pImage + YC * step);
433 for( i = L; i <= R; i++ )
436 for( i = L; i <= R; i++ )
443 region->label = saturate_cast<int>(newMaskVal);
445 region->rect.x = XMin;
446 region->rect.y = YMin;
447 region->rect.width = XMax - XMin + 1;
448 region->rect.height = YMax - YMin + 1;
454 /****************************************************************************************\
455 * External Functions *
456 \****************************************************************************************/
458 int cv::floodFill( InputOutputArray _image, InputOutputArray _mask,
459 Point seedPoint, Scalar newVal, Rect* rect,
460 Scalar loDiff, Scalar upDiff, int flags )
463 std::vector<FFillSegment> buffer;
468 int i, connectivity = flags & 255;
475 nv_buf._[0] = nv_buf._[1] = nv_buf._[2] = nv_buf._[3] = 0;
477 struct { Vec3b b; Vec3i i; Vec3f f; } ld_buf, ud_buf;
478 Mat img = _image.getMat(), mask;
480 mask = _mask.getMat();
481 Size size = img.size();
483 int type = img.type();
484 int depth = img.depth();
485 int cn = img.channels();
487 if( connectivity == 0 )
489 else if( connectivity != 4 && connectivity != 8 )
490 CV_Error( CV_StsBadFlag, "Connectivity must be 4, 0(=4) or 8" );
492 bool is_simple = mask.empty() && (flags & FLOODFILL_MASK_ONLY) == 0;
494 for( i = 0; i < cn; i++ )
496 if( loDiff[i] < 0 || upDiff[i] < 0 )
497 CV_Error( CV_StsBadArg, "lo_diff and up_diff must be non-negative" );
498 is_simple = is_simple && fabs(loDiff[i]) < DBL_EPSILON && fabs(upDiff[i]) < DBL_EPSILON;
501 if( (unsigned)seedPoint.x >= (unsigned)size.width ||
502 (unsigned)seedPoint.y >= (unsigned)size.height )
503 CV_Error( CV_StsOutOfRange, "Seed point is outside of image" );
505 scalarToRawData( newVal, &nv_buf, type, 0);
506 size_t buffer_size = MAX( size.width, size.height ) * 2;
507 buffer.resize( buffer_size );
511 size_t elem_size = img.elemSize();
512 const uchar* seed_ptr = img.ptr(seedPoint.y) + elem_size*seedPoint.x;
515 for(; k < elem_size; k++)
516 if (seed_ptr[k] != nv_buf.b[k])
521 if( type == CV_8UC1 )
522 floodFill_CnIR(img, seedPoint, nv_buf.b[0], &comp, flags, &buffer);
523 else if( type == CV_8UC3 )
524 floodFill_CnIR(img, seedPoint, Vec3b(nv_buf.b), &comp, flags, &buffer);
525 else if( type == CV_32SC1 )
526 floodFill_CnIR(img, seedPoint, nv_buf.i[0], &comp, flags, &buffer);
527 else if( type == CV_32FC1 )
528 floodFill_CnIR(img, seedPoint, nv_buf.f[0], &comp, flags, &buffer);
529 else if( type == CV_32SC3 )
530 floodFill_CnIR(img, seedPoint, Vec3i(nv_buf.i), &comp, flags, &buffer);
531 else if( type == CV_32FC3 )
532 floodFill_CnIR(img, seedPoint, Vec3f(nv_buf.f), &comp, flags, &buffer);
534 CV_Error( CV_StsUnsupportedFormat, "" );
543 Mat tempMask( size.height + 2, size.width + 2, CV_8UC1 );
544 tempMask.setTo(Scalar::all(0));
549 CV_Assert( mask.rows == size.height+2 && mask.cols == size.width+2 );
550 CV_Assert( mask.type() == CV_8U );
553 memset( mask.ptr(), 1, mask.cols );
554 memset( mask.ptr(mask.rows-1), 1, mask.cols );
556 for( i = 1; i <= size.height; i++ )
558 mask.at<uchar>(i, 0) = mask.at<uchar>(i, mask.cols-1) = (uchar)1;
562 for( i = 0; i < cn; i++ )
564 ld_buf.b[i] = saturate_cast<uchar>(cvFloor(loDiff[i]));
565 ud_buf.b[i] = saturate_cast<uchar>(cvFloor(upDiff[i]));
567 else if( depth == CV_32S )
568 for( i = 0; i < cn; i++ )
570 ld_buf.i[i] = cvFloor(loDiff[i]);
571 ud_buf.i[i] = cvFloor(upDiff[i]);
573 else if( depth == CV_32F )
574 for( i = 0; i < cn; i++ )
576 ld_buf.f[i] = (float)loDiff[i];
577 ud_buf.f[i] = (float)upDiff[i];
580 CV_Error( CV_StsUnsupportedFormat, "" );
582 uchar newMaskVal = (uchar)((flags & ~0xff) == 0 ? 1 : ((flags >> 8) & 255));
584 if( type == CV_8UC1 )
585 floodFillGrad_CnIR<uchar, uchar, int, Diff8uC1>(
586 img, mask, seedPoint, nv_buf.b[0], newMaskVal,
587 Diff8uC1(ld_buf.b[0], ud_buf.b[0]),
588 &comp, flags, &buffer);
589 else if( type == CV_8UC3 )
590 floodFillGrad_CnIR<Vec3b, uchar, Vec3i, Diff8uC3>(
591 img, mask, seedPoint, Vec3b(nv_buf.b), newMaskVal,
592 Diff8uC3(ld_buf.b, ud_buf.b),
593 &comp, flags, &buffer);
594 else if( type == CV_32SC1 )
595 floodFillGrad_CnIR<int, uchar, int, Diff32sC1>(
596 img, mask, seedPoint, nv_buf.i[0], newMaskVal,
597 Diff32sC1(ld_buf.i[0], ud_buf.i[0]),
598 &comp, flags, &buffer);
599 else if( type == CV_32SC3 )
600 floodFillGrad_CnIR<Vec3i, uchar, Vec3i, Diff32sC3>(
601 img, mask, seedPoint, Vec3i(nv_buf.i), newMaskVal,
602 Diff32sC3(ld_buf.i, ud_buf.i),
603 &comp, flags, &buffer);
604 else if( type == CV_32FC1 )
605 floodFillGrad_CnIR<float, uchar, float, Diff32fC1>(
606 img, mask, seedPoint, nv_buf.f[0], newMaskVal,
607 Diff32fC1(ld_buf.f[0], ud_buf.f[0]),
608 &comp, flags, &buffer);
609 else if( type == CV_32FC3 )
610 floodFillGrad_CnIR<Vec3f, uchar, Vec3f, Diff32fC3>(
611 img, mask, seedPoint, Vec3f(nv_buf.f), newMaskVal,
612 Diff32fC3(ld_buf.f, ud_buf.f),
613 &comp, flags, &buffer);
615 CV_Error(CV_StsUnsupportedFormat, "");
623 int cv::floodFill( InputOutputArray _image, Point seedPoint,
624 Scalar newVal, Rect* rect,
625 Scalar loDiff, Scalar upDiff, int flags )
627 return floodFill(_image, Mat(), seedPoint, newVal, rect, loDiff, upDiff, flags);
632 cvFloodFill( CvArr* arr, CvPoint seed_point,
633 CvScalar newVal, CvScalar lo_diff, CvScalar up_diff,
634 CvConnectedComp* comp, int flags, CvArr* maskarr )
637 memset( comp, 0, sizeof(*comp) );
639 cv::Mat img = cv::cvarrToMat(arr), mask = cv::cvarrToMat(maskarr);
640 int area = cv::floodFill(img, mask, seed_point, newVal,
641 comp ? (cv::Rect*)&comp->rect : 0,
642 lo_diff, up_diff, flags );
646 comp->value = newVal;