ConExtraction.cpp 13 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
// ConExtraction.cpp: implementation of the CConExtraction class.
#include "ConExtraction.h"
#include <string.h>
// Construction/Destruction
CConExtraction::CConExtraction():m_nbd(2),m_area(100),m_size(2)
{
   m_Contourvec.clear();
   vector<CContour>().swap(m_Contourvec);
   {
	   std::vector<CContour>tmp = m_Contourvec;
	   m_Contourvec.swap(tmp);
   }

   //m_ForeTargetvec.clear();
   //m_rect=NULL;//???
   m_rect.bottom = 0;
   m_rect.top = 0;
   m_rect.left = 0;
   m_rect.right = 0;
   //mark value:it is used to mark the contour point in order to repeatly scan 
}
/*
//copy construction function
CConExtraction::CConExtraction(const int &m_nbd, const int &m_area, const int &m_size)
{
	this->m_nbd=m_nbd;
	this->m_area=m_area;
	this->m_size=m_size;
}
*/

CConExtraction::~CConExtraction()
{
	m_Contourvec.clear();
	vector<CContour>().swap(m_Contourvec);
	{
		std::vector<CContour>tmp = m_Contourvec;
		m_Contourvec.swap(tmp);
	}
	//m_ForeTargetvec.clear();
}


/******************************************************************************
* Function:        ExtractContours
* Description:     
* Calls:          FetchContour
* Called By:       VibeModelGetTrace
* Input:           pSrcImage     当前图像前景背景(前景为255 背景为0)
                   width         图像宽度
				   height        图像高度
				   step          图像RGB step
* Output:          
* Return:          m_Contourvec
*******************************************************************************/
vector<CContour> CConExtraction::ExtractContours(
unsigned char *pSrcImage,// the point to the source imagedata
float *MaskImgCfd,
const int &width,        // the  pixel number of each coloum
const int &height,       // the pixel number of each row(列)
const int &step          // the byte number of each colum
)
{
	m_Contourvec.clear();
	//m_Contourvec.swap(vector<CContour>(0));
	vector<CContour>().swap(m_Contourvec);
	{
		std::vector<CContour>tmp = m_Contourvec;
		m_Contourvec.swap(tmp);
	}
	
	unsigned char *img = pSrcImage; // the point to the source imagedata
	//unsigned char *Diaimg = pSrcImage;
	//unsigned char *Removeimg = pSrcImage;

	int x = 0;
	int y = 0; // the parameter of the height and the width of the image

	//make zero borders
	// 将边界设为背景
	for (x = 0; x < width; x++)
	{
		pSrcImage[x] = 0;
		pSrcImage[step * (height - 1) + x] = 0;
	}
	for (y = 0; y < height; y++)
	{
		pSrcImage[step * y] = 0;
		pSrcImage[step * y + width - 1] = 0;
	}

	/*
	memset(pSrcImage, 0, width);
	memset(pSrcImage + step * (height - 1), 0, width);

	for (y = 1; y < height - 1; ++y)
	{
		pSrcImage += step;
		*pSrcImage = *(pSrcImage + width - 1) = 0;

	}
	*/
    //remove the unrelevant noise
    //RemoveNoise(Removeimg,width,height,step);

	//it is used to remove the hole of the contour 
	//Dilation(Diaimg,width,height,step);

    //find the  external contour point
	int prev=img[0+step];
	int pcur;

	iCPoint origin; // 外轮廓开始时的坐标

	CContour m_contour;
	
	for (y = 1; y < height - 1; ++y)
	{
		for (x = 1; x < width - 1; ++x)
		{
			pcur = img[step * y + x];
		
			if ((prev == 0) && (pcur == 255)) // external contour to extract
			{
				origin.x = x;
				origin.y = y;

				m_contour = FetchContour(img + step * y + x, MaskImgCfd, step, origin);

				// for debug use
				//m_contour.label=0;
			 	
				if (m_contour.label)
				{
					m_Contourvec.push_back(m_contour);
				}
			}
			else if ((prev == 255) && (pcur == 0)) // inner contour to fill
			{

			}
			else
			{
				prev=pcur;
			} 		
		} //for
	} //for

	return m_Contourvec;
}
/*
vector<CForegroundTarget> CConExtraction::DetectTarget(
unsigned char *pSrcImage,
const int &width,
const int &height,
const int &step
)
{
	m_Contourvec.clear();
	//m_Contourvec.swap(vector<CContour>(0));
	m_ForeTargetvec.clear();
	//m_ForeTargetvec.swap(vector<CForegroundTarget>(0));

	unsigned char *img = pSrcImage; //the point to the source imagedata
	unsigned char *Diaimg = pSrcImage;
	unsigned char *Removeimg = pSrcImage;

	int x = 0;
	int y = 0; // the parameter of the height and the width of the image

	//make zero borders
	memset(pSrcImage, 0, width);
	memset(pSrcImage + step * (height - 1), 0, width);

	for (y = 1; y < height - 1; ++y)
	{
		pSrcImage += step;
		*pSrcImage = *(pSrcImage + width - 1) = 0;
	}

     //remove the unrelevant noise
    RemoveNoise(Removeimg, width, height, step);

	//it is used to fill the hole of the contour 
	Dilation(Diaimg, width, height, step);

   //find the  external contour point
	int prev = img[0 + step];
	int pcur;

	iCPoint origin; //外轮廓开始时的坐标

	CContour m_contour;
	
    CForegroundTarget m_ForeTarget;

	for (y = 1; y < height - 1; ++y)
	{
		//img+=step;
		for (x = 1; x < width - 1; ++x)
		{
			pcur = img[step * y + x];
		
			if ((prev == 0) && (pcur == 255)) //external contour to extract
			{
				origin.x = x;
				origin.y = y;

				m_contour = FetchContour(img + step * y + x, step, origin);	
				if (m_contour.label)
				{
					//m_Contourvec.push_back(m_contour);
					m_ForeTarget.m_Contour = m_contour;
					m_ForeTarget.m_point.x = m_contour.xCenter;
					m_ForeTarget.m_point.y = m_contour.yCenter;
					m_ForeTargetvec.push_back(m_ForeTarget);
				}
			}
			else
			{
				prev=pcur;
			}
		} //for
	} //for

	return  m_ForeTargetvec;
}
*/

/******************************************************************************
* Function:        FetchContour
* Description:     
* Calls:          DELTAS
                   BoundingRect
* Called By:       ExtractContours
* Input:           pImage        指向当前点的指针
				   step          图像RGB step 每一行所占的字节数
				   pt            当前点坐标
* Output:       
* Return:          m_Contour
*******************************************************************************/
CContour CConExtraction::FetchContour(
unsigned char *pImage, // the pointer to the starting pixel position value of the external contour
float * cfg,
const int &step,       // the byte number of each colum
iCPoint &pt            // the starting point position of the external contour
)
{
	int deltas[16];
	// 相邻8个点距当前点的索引差
	DELTAS(deltas, step, 1);
	memcpy(deltas + 8, deltas, 8 * sizeof(deltas[0])); //initialize the deltas array

	// 对应的邻域内8个点的x,y偏移
	int CodeDeltas[8][2] = {{1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}};

	unsigned char *i0 = pImage;
	unsigned char *i1, *i3, *i4 = NULL;

	int s, s_end;
	s_end = s = 4;

	vector<iCPoint> m_Pointvec;
	m_Pointvec.clear();
	vector<iCPoint>().swap(m_Pointvec);
	{
		std::vector<iCPoint>tmp = m_Pointvec;
		m_Pointvec.swap(tmp);
	}
	
	do
	{
		s = (s - 1) & 7;
		i1 = i0 + deltas[s];
		if (0 != (*i1))
		{
			break;
		}
	}while(s != s_end); //find the second contour point
	
	if (s != s_end)
	{
		i3 = i0;
		for(; ;) 
		{
			//s_end = s;

			// 通过循环取数 且当前点一定为前景 保证不会陷入无限循环中
			for(; ;)
			{
				i4 = i3 + deltas[++s];
				if (0 != (*i4))
				{
					break;
				}
			} //for(; ;)

			s &= 7;
			
			// 已经检索过的点不再进行分析 加速
			if (255 == *i3)
			{
				*i3 = this->m_nbd;
			}
			
			m_Pointvec.push_back(pt);
				
			pt.x += CodeDeltas[s][0];
			pt.y += CodeDeltas[s][1];

			if ((i4 == i0) && (i3 == i1)) //the condition of the connected component areas
			{
				break;
			}

			i3 = i4;
			s = (s + 4) & 7;
		} //for(; ;)
	} //if(s!=s_end)
	else
	{
		*i0 = this->m_nbd; //labeled by the signed char
	}

    CContour m_Contour;
        
	if (this->m_size <= m_Pointvec.size()) //the first step to remove the noise point
	{
        //get the rectangle of the contour
	    BoundingRect(m_Pointvec);

		int width = m_rect.right - m_rect.left;
	    int height = m_rect.bottom - m_rect.top;
	    int numpixel = width * height;

        if (this->m_area <= numpixel) //the second step to remove the noise point
		{
			m_Contour.left = m_rect.left;
	        m_Contour.top = m_rect.top;
		    m_Contour.right = m_rect.right;
		    m_Contour.bottom = m_rect.bottom;
			m_Contour.xCenter = (m_Contour.left + m_Contour.right) / 2;
			m_Contour.yCenter = (m_Contour.top + m_Contour.bottom) / 2;	
			int count = 0;
			double totalCfd = 0;
			for (auto item : m_Pointvec)
			{
				if (cfg[item.y * step + item.x] > 0.001)
				{
					totalCfd += cfg[item.y * step + item.x];
					++count;
				}
			}
			if (count)
			{
				m_Contour.cfd = totalCfd / count;
				m_Contour.label = true;
			}
			else
			{
				m_Contour.label = false;
			}
		}
		else
		{
			m_Contour.left = 0;
	    	m_Contour.top = 0;
			m_Contour.right = 0;
		    m_Contour.bottom = 0;
			m_Contour.xCenter = 0;
			m_Contour.yCenter = 0;
			m_Contour.cfd = 0;
		    m_Contour.label = false;
		}
	}
	else
	{
		m_Contour.left = 0;
	    m_Contour.top = 0;
		m_Contour.right = 0;
		m_Contour.bottom = 0;
		m_Contour.xCenter = 0;
		m_Contour.yCenter = 0;
		m_Contour.cfd = 0;
		m_Contour.label = false;
	}

	m_Pointvec.clear();
	vector<iCPoint>().swap(m_Pointvec);
	{
		std::vector<iCPoint>tmp = m_Pointvec;
		m_Pointvec.swap(tmp);
	}

	return m_Contour;
}

//this function is used to get the external rectangle
void CConExtraction::BoundingRect(vector<iCPoint> Pointvec)
{
	int xmin, xmax;
	int ymin, ymax;
	
	vector<iCPoint>::iterator iter_begin = Pointvec.begin();
	vector<iCPoint>::iterator iter_end = Pointvec.end();

	xmin = xmax = (*iter_begin).x;
	ymin = ymax = (*iter_begin).y; //intialize the value

	iter_begin += 1;
	for(; iter_begin != iter_end; ++iter_begin)
	{
		int xPoint = (*iter_begin).x;
		int yPoint = (*iter_begin).y;
		if (xmin > xPoint)
		{
			xmin = xPoint;
		}
		if (xmax < xPoint)
		{
			xmax = xPoint;
		}
		if (ymin > yPoint)
		{
			ymin = yPoint;
		}
		if (ymax < yPoint)
		{
			ymax = yPoint;
		}
	}
     
	m_rect.top = ymin;
	m_rect.left = xmin;
	m_rect.right = xmax;
	m_rect.bottom = ymax;
}

/*
//this function is used to fill the hole of the contour
void CConExtraction::Dilation(
unsigned char *pSrcImage,
const int &width,
const int &height,
const int &step)
{

	int deltas[8];
	DELTAS(deltas, step, 1);

	int i, j;
	i = j = 0;

    int num;
	num = 0;

	unsigned char *lpSrc = NULL;
	unsigned char *lpDst = NULL;
	unsigned char *pixel = NULL;

	int label = 0;

	unsigned char *pDstImage = new unsigned char[height * width];

	for (i = 0; i < height; ++i)
	{
		for (j = 0; j < width; ++j)
		{
			pDstImage[i * width + j] = 0;
		}
	}
	
	for (i = 1; i < height - 1; ++i)
	{
		for (j = 1; j < width - 1; ++j)
		{
			label = i * step + j;

			lpSrc = pSrcImage + label;
		
			for (num = 0; num < 8; ++num)
			{
				pixel = lpSrc + deltas[num];

				if (1 == (*pixel))
				{
					pDstImage[label] = 1;
					break;
				}
			}
		}
	}

	for (i = 0; i < height; ++i)
	{
		for (j = 0; j < width; ++j)
		{
			label = i * width + j;
			pSrcImage[label] = pDstImage[label];
		}
	}

	delete  []pDstImage;
	pDstImage = NULL;
}
*/
/*
//this function is used to remove the noise of background before Fetching contour
void CConExtraction::RemoveNoise(
unsigned char *pSrcImage,
const int &width,
const int &height,
const int &step)
{
	int deltas[8];
	DELTAS(deltas, step, 1);

	unsigned char *pSrc = pSrcImage;

	int i, j;
	i = j = 0;

	int label = 0;
   
	unsigned char *pDstImage = new unsigned char[height * width];

	for (i = 0; i < height; ++i)
	{
		for (j = 0; j < width; ++j)
		{
			label = i * width + j;
			pDstImage[label] = pSrcImage[label];
		}
	}

	int label0, label1, label2, label3;
	label0 = label1 = label2 = label3 = 0;

	for (i = 2; i < height - 2; ++i)
	{
		for (j = 2; j < width - 2; ++j)
		{
			label0 = i * step + j;
			label1 = (i - 1) * step + j;
			label2 = (i + 1) * step + j;
			label3 = (i + 2) * step + j;
			if (1 == pSrc[label0])
			{
				if (0 == pSrc[label0 + 1] && 0 == pSrc[label0 - 1]
				   && 0 == pSrc[label1] && 0 == pSrc[label1 + 1] && 0 == pSrc[label1 - 1]
				   && 0 == pSrc[label2] && 0 == pSrc[label2 + 1] && 0 == pSrc[label2 - 1])
				{
					pDstImage[label0] = 0;
				}

				if (1 == pSrc[label0 + 1] && 0 == pSrc[label0 - 1] && 0 == pSrc[label0 + 2]
				   && 0 == pSrc[label1] && 0 == pSrc[label1 + 1] && 0 == pSrc[label1 - 1] && 0 == pSrc[label1 + 2]
				   && 0 == pSrc[label2] && 0 == pSrc[label2 + 1] && 0 == pSrc[label2 - 1] && 0 == pSrc[label2 + 2])
				{
					pDstImage[label0] = 0;
					pDstImage[label0 + 1] = 0;
				}
				if (1 == pSrc[label2] && 0 == pSrc[label2 + 1] && 0 == pSrc[label2 - 1]
			       && 0 == pSrc[label0 + 1] && 0 == pSrc[label0 - 1]
				   && 0 == pSrc[label1] && 0 == pSrc[label1 + 1] && 0 == pSrc[label1 - 1]
				   && 0 == pSrc[label3] && 0 == pSrc[label3 + 1] && 0 == pSrc[label3 - 1])
				{
					pDstImage[label0] = 0;
					pDstImage[label2] = 0;
				}
				if (1 == pSrc[label0 + 1] && 0 == pSrc[label0 - 1] && 0 == pSrc[label0 + 2]
				&& 1 == pSrc[label2] && 1 == pSrc[label2 + 1] && 0 == pSrc[label2 - 1] && 0 == pSrc[label2 + 2]
				&& 0 == pSrc[label1] && 0 == pSrc[label1 + 1] && 0 == pSrc[label1 - 1] && 0 == pSrc[label1 + 2]
				&& 0 == pSrc[label3] && 0 == pSrc[label1 + 1] && 0 == pSrc[label3 - 1] && 0 == pSrc[label3 + 2])
				{
					pDstImage[label0] = 0;
					pDstImage[label0 + 1] = 0;
					pDstImage[label2] = 0;
					pDstImage[label2 + 1] = 0;
				}
			}
		}
	}

	for (i = 0; i < height; ++i)
	{
		for (j = 0; j < width; ++j)
		{
			label = i * width + j;
			pSrcImage[label] = pDstImage[label];
		}
	}

	delete  []pDstImage;
	pDstImage = NULL;
}
*/