You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

411 lines
11 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #if _MSC_VER > 1000
  2. // deactivation of warning 4786
  3. // (identifier was truncated to '255' characters in the debug information)
  4. #pragma warning(disable: 4786)
  5. #endif // _MSC_VER > 1000
  6. #include "ComponentLabeling.h"
  7. //! Conversion from freeman code to coordinate increments (counterclockwise)
  8. static const CvPoint freemanCodeIncrement[8] =
  9. { {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1} };
  10. /**
  11. - FUNCI�:
  12. - FUNCIONALITAT:
  13. - PAR�METRES:
  14. -
  15. - RESULTAT:
  16. -
  17. - RESTRICCIONS:
  18. -
  19. - AUTOR: rborras
  20. - DATA DE CREACI�: 2008/04/29
  21. - MODIFICACI�: Data. Autor. Descripci�.
  22. */
  23. inline unsigned char GET_ABOVE_IMAGEPIXEL( unsigned char *currentPixel, IplImage *image )
  24. {
  25. return *(currentPixel - image->widthStep);
  26. }
  27. inline unsigned char GET_BELOW_IMAGEPIXEL( unsigned char *currentPixel, IplImage *image )
  28. {
  29. return *(currentPixel + image->widthStep);
  30. }
  31. inline unsigned char GET_IMAGE_PIXEL( IplImage *image, CvPoint p )
  32. {
  33. return *(image->imageData + p.x + p.y *image->widthStep);
  34. }
  35. inline bool GET_IMAGEMASK_PIXEL( IplImage *mask, CvPoint p )
  36. {
  37. if( mask != NULL )
  38. return ((unsigned char)*(mask->imageData + p.x + p.y *mask->widthStep)) > 0;
  39. else
  40. return true;
  41. }
  42. inline bool GET_BELOW_VISITEDPIXEL( bool *currentPixel, int imageWidth )
  43. {
  44. return *( currentPixel + imageWidth );
  45. }
  46. /**
  47. - FUNCI�: ASSIGN_LABEL
  48. - FUNCIONALITAT: Assigns label value to label image
  49. - PAR�METRES:
  50. -
  51. - RESULTAT:
  52. -
  53. - RESTRICCIONS:
  54. -
  55. - AUTOR: rborras
  56. - DATA DE CREACI�: 2008/04/29
  57. - MODIFICACI�: Data. Autor. Descripci�.
  58. */
  59. inline void ASSIGN_LABEL( CvPoint p, t_labelType *labels, int imageWidth, int newLabel )
  60. {
  61. *(labels + p.y * imageWidth + p.x) = newLabel;
  62. }
  63. inline void ASSIGN_VISITED( CvPoint p, bool *visitedPoints, int imageWidth )
  64. {
  65. *(visitedPoints + p.y * imageWidth + p.x) = true;
  66. }
  67. /**
  68. - FUNCI�: ComponentLabeling
  69. - FUNCIONALITAT: Calcula els components binaris (blobs) d'una imatge amb connectivitat a 8
  70. - PAR�METRES:
  71. - inputImage: image to segment (pixel values different than blobColor are treated as background)
  72. - maskImage: if not NULL, all the pixels equal to 0 in mask are skipped in input image
  73. - backgroundColor: color of background (ignored pixels)
  74. - blobs: blob vector destination
  75. - RESULTAT:
  76. -
  77. - RESTRICCIONS:
  78. -
  79. - AUTOR: rborras
  80. - DATA DE CREACI�: 2008/04/21
  81. - MODIFICACI�: Data. Autor. Descripci�.
  82. - NOTA: Algorithm based on "A linear-time component labeling algorithm using contour tracing technique",
  83. F.Chang et al
  84. */
  85. bool ComponentLabeling( IplImage* inputImage,
  86. IplImage* maskImage,
  87. unsigned char backgroundColor,
  88. Blob_vector &blobs )
  89. {
  90. int i,j;
  91. // row major vector with visited points
  92. bool *visitedPoints, *pVisitedPoints, internalContour, externalContour;
  93. unsigned char *pInputImage, *pMask, *pAboveInputImage, *pBelowInputImage,
  94. *pAboveMask, *pBelowMask;
  95. int imageWidth, imageHeight, currentLabel, contourLabel = 0;
  96. // row major vector with labelled image
  97. t_labelType *labelledImage, *pLabels;
  98. //! current blob pointer
  99. BlobPtr currentBlob;
  100. CvSize imageSizes;
  101. CvPoint currentPoint;
  102. // verify input image
  103. if( !CV_IS_IMAGE( inputImage ) )
  104. return false;
  105. // verify that input image and mask image has same size
  106. if( maskImage )
  107. {
  108. if( !CV_IS_IMAGE(maskImage) ||
  109. maskImage->width != inputImage->width ||
  110. maskImage->height != inputImage->height )
  111. return false;
  112. }
  113. else
  114. {
  115. pMask = NULL;
  116. pAboveMask = NULL;
  117. pBelowMask = NULL;
  118. }
  119. imageSizes = cvSize(inputImage->width,inputImage->height);
  120. imageWidth = inputImage->width;
  121. imageHeight = inputImage->height;
  122. // create auxiliary buffers
  123. labelledImage = (t_labelType*) malloc( inputImage->width * inputImage->height * sizeof(t_labelType) );
  124. visitedPoints = (bool*) malloc( inputImage->width * inputImage->height * sizeof(bool) );
  125. // initialize it to 0
  126. memset(labelledImage, 0, inputImage->width * inputImage->height * sizeof(t_labelType) ) ;
  127. memset(visitedPoints, false, inputImage->width * inputImage->height * sizeof(bool) ) ;
  128. // initialize pointers and label counter
  129. pLabels = labelledImage;
  130. pVisitedPoints = visitedPoints;
  131. currentLabel = 1;
  132. for (j = 0; j < imageHeight; j++ )
  133. {
  134. // don't verify if we area on first or last row, it will verified on pointer access
  135. pAboveInputImage = (unsigned char*) inputImage->imageData + (j-1) * inputImage->widthStep;
  136. pBelowInputImage = (unsigned char*) inputImage->imageData + (j+1) * inputImage->widthStep;
  137. pInputImage = (unsigned char*) inputImage->imageData + j * inputImage->widthStep;
  138. if( maskImage )
  139. {
  140. pMask = (unsigned char*) maskImage->imageData + j * maskImage->widthStep;
  141. // don't verify if we area on first or last row, it will verified on pointer access
  142. pAboveMask = (unsigned char*) maskImage->imageData + (j-1) * maskImage->widthStep;
  143. pBelowMask = (unsigned char*) maskImage->imageData + (j+1) * maskImage->widthStep;
  144. }
  145. for (i = 0; i < imageWidth; i++, pInputImage++, pMask++, pAboveInputImage++, pBelowInputImage++,
  146. pAboveMask++, pBelowMask++ )
  147. {
  148. // ignore background pixels or 0 pixels in mask
  149. if ( (*pInputImage == backgroundColor) || (maskImage && *pMask == 0 ))
  150. {
  151. pLabels++;
  152. pVisitedPoints++;
  153. continue;
  154. }
  155. // new external contour: current label == 0 and above pixel is background
  156. if( j > 0 )
  157. {
  158. externalContour = ((*pAboveInputImage == backgroundColor) ||
  159. (maskImage && *pAboveMask == 0)) &&
  160. (*pLabels == 0);
  161. }
  162. else
  163. externalContour = (*pLabels == 0);
  164. // new internal contour: below pixel is background and not visited
  165. if( !externalContour && j < imageHeight - 1 )
  166. {
  167. internalContour = *pBelowInputImage == backgroundColor &&
  168. !GET_BELOW_VISITEDPIXEL( pVisitedPoints, imageWidth);
  169. }
  170. else
  171. {
  172. internalContour = false;
  173. }
  174. if( externalContour )
  175. {
  176. currentPoint = cvPoint(i,j);
  177. // assign label to labelled image
  178. *pLabels = currentLabel;
  179. // create new blob
  180. currentBlob = BlobPtr( new CBlob(currentLabel, currentPoint, imageSizes ) );
  181. // contour tracing with currentLabel
  182. contourTracing( inputImage, maskImage, currentPoint,
  183. labelledImage, visitedPoints,
  184. currentLabel, false, backgroundColor, currentBlob->GetExternalContour() );
  185. // add new created blob
  186. blobs.push_back(currentBlob);
  187. currentLabel++;
  188. }
  189. else
  190. {
  191. if( internalContour )
  192. {
  193. currentPoint = cvPoint(i,j);
  194. if( *pLabels == 0 )
  195. {
  196. // take left neightbour value as current
  197. if( i > 0 )
  198. contourLabel = *(pLabels - 1);
  199. }
  200. else
  201. {
  202. contourLabel = *pLabels;
  203. }
  204. if(contourLabel>0)
  205. {
  206. currentBlob = blobs[contourLabel-1];
  207. CBlobContour newContour(currentPoint, currentBlob->GetStorage());
  208. // contour tracing with contourLabel
  209. contourTracing( inputImage, maskImage, currentPoint, labelledImage, visitedPoints,
  210. contourLabel, true, backgroundColor, &newContour );
  211. currentBlob->AddInternalContour( newContour );
  212. }
  213. }
  214. // neither internal nor external contour
  215. else
  216. {
  217. // take left neightbour value as current if it is not labelled
  218. if( i > 0 && *pLabels == 0 )
  219. *pLabels = *(pLabels - 1);
  220. }
  221. }
  222. pLabels++;
  223. pVisitedPoints++;
  224. }
  225. }
  226. // free auxiliary buffers
  227. free( labelledImage );
  228. free( visitedPoints );
  229. return true;
  230. }
  231. /**
  232. - FUNCI�:
  233. - FUNCIONALITAT:
  234. - PAR�METRES:
  235. -
  236. - RESULTAT:
  237. -
  238. - RESTRICCIONS:
  239. -
  240. - AUTOR: rborras
  241. - DATA DE CREACI�: 2008/04/29
  242. - MODIFICACI�: Data. Autor. Descripci�.
  243. */
  244. void contourTracing( IplImage *image,
  245. IplImage *maskImage,
  246. CvPoint contourStart, t_labelType *labels, bool *visitedPoints, t_labelType label,
  247. bool internalContour, unsigned char backgroundColor, CBlobContour *currentBlobcontour )
  248. {
  249. CvPoint t, tnext, tsecond;
  250. short initialMovement, movement;
  251. if( internalContour )
  252. {
  253. initialMovement = 7;//3;
  254. }
  255. else
  256. {
  257. initialMovement = 3;//7;
  258. }
  259. tsecond = tracer( image, maskImage, contourStart, visitedPoints, initialMovement,
  260. backgroundColor, movement );
  261. // assign current label to tnext
  262. ASSIGN_LABEL( contourStart, labels, image->width, label );
  263. // contour corresponds to isolated pixel?
  264. if( tsecond.x == contourStart.x && tsecond.y == contourStart.y )
  265. {
  266. // we are finished with the contour
  267. return;
  268. }
  269. // add chain code to current contour
  270. currentBlobcontour->AddChainCode(movement);
  271. // assign label to next point
  272. ASSIGN_LABEL( tsecond, labels, image->width, label );
  273. tnext.x = tsecond.x;
  274. tnext.y = tsecond.y;
  275. t.x = tnext.x;
  276. t.y = tnext.y;
  277. // while T is different than contourStart and Tnext is different than T
  278. // follow contour until start point is reached again
  279. while ( t.x != contourStart.x || t.y != contourStart.y ||
  280. tsecond.x != tnext.x || tsecond.y != tnext.y )
  281. {
  282. t.x = tnext.x;
  283. t.y = tnext.y;
  284. initialMovement = (movement + 5) % 8;
  285. // search for next contour point
  286. tnext = tracer( image, maskImage, t, visitedPoints, initialMovement,
  287. backgroundColor, movement );
  288. // assign label to contour point
  289. ASSIGN_LABEL( tnext, labels, image->width, label );
  290. // add chain code to current contour
  291. currentBlobcontour->AddChainCode(movement);
  292. }
  293. }
  294. /**
  295. - FUNCI�: tracer
  296. - FUNCIONALITAT: Searches for next point of a contour
  297. - PAR�METRES:
  298. -
  299. - RESULTAT:
  300. -
  301. - RESTRICCIONS:
  302. -
  303. - AUTOR: rborras
  304. - DATA DE CREACI�: 2008/04/30
  305. - MODIFICACI�: Data. Autor. Descripci�.
  306. */
  307. CvPoint tracer( IplImage *image, IplImage *maskImage, CvPoint P, bool *visitedPoints,
  308. short initialMovement,
  309. unsigned char backgroundColor, short &movement )
  310. {
  311. int d;
  312. CvPoint pNext;
  313. for (d = 0; d <= 7; d++ )
  314. {
  315. movement = (initialMovement + d) % 8;
  316. pNext.x = P.x + freemanCodeIncrement[movement].x;
  317. pNext.y = P.y + freemanCodeIncrement[movement].y;
  318. // the point is inside image ?
  319. if( pNext.x < 0 || pNext.x >= image->width ||
  320. pNext.y < 0 || pNext.y >= image->height )
  321. {
  322. // try other movement
  323. continue;
  324. }
  325. // image has blobColor value in the new point?
  326. if( (GET_IMAGE_PIXEL( image, pNext ) != backgroundColor ) && GET_IMAGEMASK_PIXEL(maskImage, pNext ) )
  327. {
  328. return pNext;
  329. }
  330. else
  331. {
  332. // mark point as visited
  333. ASSIGN_VISITED( pNext, visitedPoints, image->width );
  334. }
  335. }
  336. // no possible movement was found
  337. movement = -1;
  338. pNext.x = P.x;
  339. pNext.y = P.y;
  340. return pNext;
  341. }