cvfloodfill.cpp.svn-base
来自「非结构化路识别」· SVN-BASE 代码 · 共 1,138 行 · 第 1/3 页
SVN-BASE
1,138 行
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "_cv.h"
typedef struct CvFFillSegment
{
ushort y;
ushort l;
ushort r;
ushort prevl;
ushort prevr;
short dir;
}
CvFFillSegment;
#define UP 1
#define DOWN -1
#define ICV_PUSH( Y, L, R, PREV_L, PREV_R, DIR )\
{ \
tail->y = (ushort)(Y); \
tail->l = (ushort)(L); \
tail->r = (ushort)(R); \
tail->prevl = (ushort)(PREV_L); \
tail->prevr = (ushort)(PREV_R); \
tail->dir = (short)(DIR); \
if( ++tail >= buffer_end ) \
tail = buffer; \
}
#define ICV_POP( Y, L, R, PREV_L, PREV_R, DIR ) \
{ \
Y = head->y; \
L = head->l; \
R = head->r; \
PREV_L = head->prevl; \
PREV_R = head->prevr; \
DIR = head->dir; \
if( ++head >= buffer_end ) \
head = buffer; \
}
/****************************************************************************************\
* Simple (monochrome) Floodfill *
\****************************************************************************************/
static CvStatus
icvFloodFill_8u_C1IR( uchar* pImage, int step, CvSize roi, CvPoint seed,
uchar* _newVal, CvConnectedComp* region, int flags,
CvFFillSegment* buffer, int buffersize )
{
uchar* img = pImage + step * seed.y;
int i, L, R;
int area = 0;
int val0 = 0;
uchar newVal = _newVal[0];
int XMin, XMax, YMin = seed.y, YMax = seed.y;
int _8_connectivity = (flags & 255) == 8;
CvFFillSegment* buffer_end = buffer + buffersize, *head = buffer, *tail = buffer;
L = R = seed.x;
val0 = img[L];
if( val0 == newVal )
{
XMin = XMax = seed.x;
goto exit_func;
}
img[L] = newVal;
while( ++R < roi.width && img[R] == val0 )
img[R] = newVal;
while( --L >= 0 && img[L] == val0 )
img[L] = newVal;
XMax = --R;
XMin = ++L;
ICV_PUSH( seed.y, L, R, R + 1, R, UP );
while( head != tail )
{
int k, YC, PL, PR, dir;
ICV_POP( YC, L, R, PL, PR, dir );
int data[][3] =
{
{-dir, L - _8_connectivity, R + _8_connectivity},
{dir, L - _8_connectivity, PL - 1},
{dir, PR + 1, R + _8_connectivity}
};
if( region )
{
area += R - L + 1;
if( XMax < R ) XMax = R;
if( XMin > L ) XMin = L;
if( YMax < YC ) YMax = YC;
if( YMin > YC ) YMin = YC;
}
for( k = (unsigned)(YC - dir) >= (unsigned)roi.height; k < 3; k++ )
{
dir = data[k][0];
img = pImage + (YC + dir) * step;
int left = data[k][1];
int right = data[k][2];
for( i = left; i <= right; i++ )
{
if( (unsigned)i < (unsigned)roi.width && img[i] == val0 )
{
int j = i;
img[i] = newVal;
while( --j >= 0 && img[j] == val0 )
img[j] = newVal;
while( ++i < roi.width && img[i] == val0 )
img[i] = newVal;
ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
}
}
}
}
exit_func:
if( region )
{
region->area = area;
region->rect.x = XMin;
region->rect.y = YMin;
region->rect.width = XMax - XMin + 1;
region->rect.height = YMax - YMin + 1;
region->value = newVal;
}
return CV_NO_ERR;
}
#define ICV_EQ_C3( p1, p2 ) \
((p1)[0] == (p2)[0] && (p1)[1] == (p2)[1] && (p1)[2] == (p2)[2])
#define ICV_SET_C3( p, q ) \
((p)[0] = (q)[0], (p)[1] = (q)[1], (p)[2] = (q)[2])
static CvStatus
icvFloodFill_8u_C3IR( uchar* pImage, int step, CvSize roi, CvPoint seed,
uchar* _newVal, CvConnectedComp* region, int flags,
CvFFillSegment* buffer, int buffersize )
{
uchar* img = pImage + step * seed.y;
int i, L, R;
int area = 0;
int val0[3] = { 0, 0, 0 };
uchar newVal[3] = { _newVal[0], _newVal[1], _newVal[2] };
int XMin, XMax, YMin = seed.y, YMax = seed.y;
int _8_connectivity = (flags & 255) == 8;
CvFFillSegment* buffer_end = buffer + buffersize, *head = buffer, *tail = buffer;
L = R = seed.x;
ICV_SET_C3( val0, img + L*3 );
if( ICV_EQ_C3( val0, newVal ))
{
XMin = XMax = seed.x;
goto exit_func;
}
ICV_SET_C3( img + L*3, newVal );
while( --L >= 0 && ICV_EQ_C3( img + L*3, val0 ))
ICV_SET_C3( img + L*3, newVal );
while( ++R < roi.width && ICV_EQ_C3( img + R*3, val0 ))
ICV_SET_C3( img + R*3, newVal );
XMin = ++L;
XMax = --R;
ICV_PUSH( seed.y, L, R, R + 1, R, UP );
while( head != tail )
{
int k, YC, PL, PR, dir;
ICV_POP( YC, L, R, PL, PR, dir );
int data[][3] =
{
{-dir, L - _8_connectivity, R + _8_connectivity},
{dir, L - _8_connectivity, PL - 1},
{dir, PR + 1, R + _8_connectivity}
};
if( region )
{
area += R - L + 1;
if( XMax < R ) XMax = R;
if( XMin > L ) XMin = L;
if( YMax < YC ) YMax = YC;
if( YMin > YC ) YMin = YC;
}
for( k = (unsigned)(YC - dir) >= (unsigned)roi.height; k < 3; k++ )
{
dir = data[k][0];
img = pImage + (YC + dir) * step;
int left = data[k][1];
int right = data[k][2];
for( i = left; i <= right; i++ )
{
if( (unsigned)i < (unsigned)roi.width && ICV_EQ_C3( img + i*3, val0 ))
{
int j = i;
ICV_SET_C3( img + i*3, newVal );
while( --j >= 0 && ICV_EQ_C3( img + j*3, val0 ))
ICV_SET_C3( img + j*3, newVal );
while( ++i < roi.width && ICV_EQ_C3( img + i*3, val0 ))
ICV_SET_C3( img + i*3, newVal );
ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
}
}
}
}
exit_func:
if( region )
{
region->area = area;
region->rect.x = XMin;
region->rect.y = YMin;
region->rect.width = XMax - XMin + 1;
region->rect.height = YMax - YMin + 1;
region->value = CV_RGB( newVal[2], newVal[1], newVal[0] );
}
return CV_NO_ERR;
}
/* because all the operations on floats that are done during non-gradient floodfill
are just copying and comparison on equality,
we can do the whole op on 32-bit integers instead */
static CvStatus
icvFloodFill_32f_C1IR( int* pImage, int step, CvSize roi, CvPoint seed,
int* _newVal, CvConnectedComp* region, int flags,
CvFFillSegment* buffer, int buffersize )
{
int* img = pImage + (step /= sizeof(pImage[0])) * seed.y;
int i, L, R;
int area = 0;
int val0 = 0;
int newVal = _newVal[0];
int XMin, XMax, YMin = seed.y, YMax = seed.y;
int _8_connectivity = (flags & 255) == 8;
CvFFillSegment* buffer_end = buffer + buffersize, *head = buffer, *tail = buffer;
L = R = seed.x;
val0 = img[L];
if( val0 == newVal )
{
XMin = XMax = seed.x;
goto exit_func;
}
img[L] = newVal;
while( ++R < roi.width && img[R] == val0 )
img[R] = newVal;
while( --L >= 0 && img[L] == val0 )
img[L] = newVal;
XMax = --R;
XMin = ++L;
ICV_PUSH( seed.y, L, R, R + 1, R, UP );
while( head != tail )
{
int k, YC, PL, PR, dir;
ICV_POP( YC, L, R, PL, PR, dir );
int data[][3] =
{
{-dir, L - _8_connectivity, R + _8_connectivity},
{dir, L - _8_connectivity, PL - 1},
{dir, PR + 1, R + _8_connectivity}
};
if( region )
{
area += R - L + 1;
if( XMax < R ) XMax = R;
if( XMin > L ) XMin = L;
if( YMax < YC ) YMax = YC;
if( YMin > YC ) YMin = YC;
}
for( k = (unsigned)(YC - dir) >= (unsigned)roi.height; k < 3; k++ )
{
dir = data[k][0];
img = pImage + (YC + dir) * step;
int left = data[k][1];
int right = data[k][2];
for( i = left; i <= right; i++ )
{
if( (unsigned)i < (unsigned)roi.width && img[i] == val0 )
{
int j = i;
img[i] = newVal;
while( --j >= 0 && img[j] == val0 )
img[j] = newVal;
while( ++i < roi.width && img[i] == val0 )
img[i] = newVal;
ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
}
}
}
}
exit_func:
if( region )
{
region->area = area;
region->rect.x = XMin;
region->rect.y = YMin;
region->rect.width = XMax - XMin + 1;
region->rect.height = YMax - YMin + 1;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?