首页 > > 详细

辅导VS C++算法、辅导C++程序 辅导R语言程序|讲解R语言编程

// V-S
bool Watershed::WatershedTransformOfVS(IplImage *src, int &basinCount)
{
#define MASK -2 /* initial value of a threshold level */
#define INIT -1 /* initial value of imgOut */
#define WSHED 0 /* value of the pixels belonging to the watersheds */
#define INIT_DIST 0 /* initial value of imgDist */
/*
. --input: imgIn, decimal image;
--output: image of the labeled watersheds;
. Initializations:
--Value INIT is assigned to each pixel of imgOut:
--current_label = 0
--current_dist: integer variable
--imgDist:work image (of distances),initialized to 0;
*/

if (src == NULL)
{
return false;
}

const int width = src->width;
const int height = src->height;
const int widthStep = src->widthStep;
const int channels = src->nChannels;
uchar *imgIn = (uchar *)src->imageData;

if (channels != 1) // src
{
return false;
}

// V-S
//
//
//
BASIN_NUM *imgOut = new BASIN_NUM[height * widthStep]; //
for (int i = 0; i < height * widthStep; i ++) imgOut[i] = INIT; //

int *imgDist = new int[height * widthStep]; //
for (int i = 0; i < height * widthStep; i ++)
imgDist[i] = INIT_DIST; //

int currentNum = 0; //
int currentDist = 0; //

int Direct[][2] = {{0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}}; //

//
//
//
vector vPointsGradient; //
for (int col = 0; col < height; col ++)
{
for (int row = 0; row < width; row ++)
{
if (255 == (int)imgIn[col * widthStep + row])
{
continue;
}
POINT_GRADIENT point;
point.x = row;
point.y = col;
point.gradient = (int)imgIn[col * widthStep + row];
vPointsGradient.push_back(point);
}
}
sort(vPointsGradient.begin(), vPointsGradient.end(), cmp);

//
//
//
unsigned int nCurGradient; //
vector vPointsCurGrad; //

int iPoint = vPointsGradient.size() - 1; //
nCurGradient = vPointsGradient[iPoint].gradient; //

for ( ; iPoint >= 0; iPoint --) //
{
if (nCurGradient == vPointsGradient[iPoint].gradient) //
{
vPointsCurGrad.push_back(vPointsGradient[iPoint]);
continue;
}

// >>>nGradient

//
//
//
POINT_GRADIENT p ; //
POINT_GRADIENT p_; // p
queue fifoQue;
for (int iPoint = 0; iPoint < vPointsCurGrad.size(); iPoint ++) //
{
p = POINT_GRADIENT(vPointsCurGrad[iPoint].x, vPointsCurGrad[iPoint].y,
vPointsCurGrad[iPoint].gradient); //
imgOut[p.x + p.y * widthStep] = MASK;

bool flag = false; // p flagtrue
for (int iDirect = 0; iDirect < 8; iDirect ++)
{
// p_ p
p_.x = p.x + Direct[iDirect][0];
p_.y = p.y + Direct[iDirect][1];
if (p_.x < 0 || p_.x >= width || p_.y < 0 || p_.y >= height)
{
continue;
}
p_.gradient = (int)imgIn[p_.x + p_.y * widthStep];

if (imgOut[p_.x + p_.y * widthStep] > 0 || imgOut[p_.x + p_.y * widthStep] == WSHED)
{
flag = true;
break;
}
}
if (flag)
{ /* p */
imgDist[p.x + p.y * widthStep] = 1;
fifoQue.push(p);
}
}
/* fifoQue1 n+1,n */

POINT_GRADIENT fictPoint = POINT_GRADIENT(0, 0, 256); // fictitious_pixel ,
currentDist = 1;
fifoQue.push(fictPoint);

while (1) // repeat indefinitely
{
p = fifoQue.front();
fifoQue.pop();
if (p.gradient == fictPoint.gradient) // if p = fictPoint
{ /* bfs */
if (fifoQue.empty())
break;
else
{
fifoQue.push(fictPoint);
currentDist += 1; // bfs ,
p = fifoQue.front();
fifoQue.pop();
}
}

//>>>

for (int iDirect = 0; iDirect < 8; iDirect ++)
{
// p_ p
p_.x = p.x + Direct[iDirect][0];
p_.y = p.y + Direct[iDirect][1];
if (p_.x < 0 || p_.x >= width || p_.y < 0 || p_.y >= height)
{
continue;
}
p_.gradient = (int)imgIn[p_.x + p_.y * widthStep];

if (imgDist[p_.x + p_.y * widthStep] < currentDist &&
(imgOut[p_.x + p_.y * widthStep] > 0 || imgOut[p_.x + p_.y * widthStep] == WSHED))
{ /* p_ */
if (imgOut[p_.x + p_.y * widthStep] > 0) // p_
{
if (imgOut[p.x + p.y * widthStep] == MASK ||
imgOut[p.x + p.y * widthStep] == WSHED)
{ /* p */
imgOut[p.x + p.y * widthStep] = imgOut[p_.x + p_.y * widthStep];
}
else if(imgOut[p.x + p.y * widthStep] != imgOut[p_.x + p_.y * widthStep])
{ /* p p_ */
imgOut[p.x + p.y * widthStep] = WSHED; //
}
}
else // p_
{
if (imgOut[p.x + p.y * widthStep] == MASK)
{ /* p */
imgOut[p.x + p.y * widthStep] = WSHED; //
}
}
}
else if (imgOut[p_.x + p_.y * widthStep] == MASK &&
imgDist[p_.x + p_.y * widthStep] == 0)
{
imgDist[p_.x + p_.y * widthStep] = currentDist + 1;
fifoQue.push(p_);
}
}

//<<<
}

//
//
//
for (int iPoint = 0; iPoint < vPointsCurGrad.size(); iPoint ++) //
{
p = POINT_GRADIENT(vPointsCurGrad[iPoint].x, vPointsCurGrad[iPoint].y,
vPointsCurGrad[iPoint].gradient); //
imgDist[p.x + p.y * widthStep] = 0; /* p 0 */

if (imgOut[p.x + p.y * widthStep] == MASK)
{
currentNum += 1; //
fifoQue.push(p);
imgOut[p.x + p.y * widthStep] = currentNum;

//
POINT_GRADIENT pt; //
while (! fifoQue.empty())
{
pt = fifoQue.front();
fifoQue.pop();

POINT_GRADIENT pt_; // pt
for (int iDirect = 0; iDirect < 8; iDirect ++)
{
pt_.x = pt.x + Direct[iDirect][0];
pt_.y = pt.y + Direct[iDirect][1];
if (pt_.x < 0 || pt_.x >= width || pt_.y < 0 || pt_.y >= height)
{
continue;
}
pt_.gradient = (int)imgIn[pt_.x + pt_.y * widthStep];
if (pt_.gradient != pt.gradient)
{
continue;
}

if (imgOut[pt_.x + pt_.y * widthStep] == MASK)
{
fifoQue.push(pt_);
imgOut[pt_.x + pt_.y * widthStep] = currentNum;
}
}
}
}
}

// <<
nCurGradient = vPointsGradient[iPoint].gradient;
vPointsCurGrad.clear();
vPointsCurGrad.push_back(vPointsGradient[iPoint]);
}
basinCount = currentNum;
for (int col = 0; col < height; col ++)
{
for (int row = 0; row < width; row ++)
{
if (imgOut[col * widthStep + row] == 0) imgOut[col * widthStep + row] = 255;
imgIn[col * widthStep + row] = (uchar)imgOut[col * widthStep + row];
}
}

return true;
}
联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!