A Fast Algorithm for Image Connected Domains Based on Feature Matrix

This paper describes a connected domain labeling method that can be applied to large-resolution images. This method can greatly simplify the calculation of image labeling, and the accuracy and speed of labeling are both controllable.
1. The overall idea
Finding connected domains in any image is to count and determine the relationship between effective pixels in different regions. Most connected domain algorithms need to traverse and calculate the entire image, and some require multiple traversals. Only the entire picture can calculate the accurate connected region;
the idea of ​​this paper is that not every pixel in a picture needs to be calculated, and using some representative pixels to judge the validity of a certain region can actually accurately express regional information;
Based on the above ideas, the method adopted in this paper is:
first, divide the image into multiple regions, and each region adopts a fixed length and width, so that the entire image is divided into several panes, such as using a 4x4 matrix window , for a 640x480 image to be divided, we can get 160x120 matrix windows, the connected area we need to calculate is composed of these divided windows; the second step,
after dividing the matrix windows, carry out the diagonal of each window The pixel value on the traversal (for the convenience of calculation, the same effect can be achieved by using two center lines), if the effective pixel value on the feature line of the window meets the requirements, the window is counted as 1, otherwise it is recorded as 0, Then the entire 160x120 matrix window is marked as 1 and 0; at this time, a matrix representing the characteristics of the whole image has been generated; the
third step is to perform connected domain calculation on the feature matrix, first calculate the continuous area of ​​​​each row, and mark it, each All the continuous areas in the row are marked, and the row information, column information, and effective number are saved; then the marked array is traversed to calculate whether the area of ​​the effective area of ​​each row is also an effective area (the calculation of the 8 neighbors at this time , becomes 2 domains, upper and lower two lines), if it is valid, mark it as the same area, after the traversal is completed, you can get the marks of all valid areas; finally write the mark into the coordinate position corresponding to the original matrix, and get the mark good feature matrix;
The fourth step is to extract the marked feature matrix. Starting from label 1, it is the first connected area. The coordinate information of all connected areas can be obtained through one traversal. Finally, after coordinate transformation, the coordinates in the feature matrix are converted to The coordinates in the original image get the final connected domain information.
2. Characteristic analysis
The above method has three design points:
(1) Windowing, the whole image is divided into several windows, and by calculating the effective value on the feature line, the calculation process is greatly simplified and the calculation amount is geometrically reduced, according to 4x4 The window originally needs to calculate 16 pixels, but actually only calculates 8 pixels. The speed of the first traversal of the entire image is increased by 2 times; the original image is reduced by 16 times, and the second traversal is used to calculate the connected regions of each row, and the speed is increased by 16. times; if 8x8 or larger windows are used, the speed can also be improved;
(2) Calculate the connected domain by row block, first calculate the connected domain of each row, and then follow the block method to separate each individual The blocks of the connected domain are calculated twice, which greatly simplifies the calculation process, and the speed is improved again;
(3) Finally, when extracting the coordinates of the connected domain, only the coordinates in the feature matrix need to be extracted to map to the original image, without Affected by the type of the original image, it is applicable to RGB, grayscale, and binarized images. Secondly, the algorithm greatly reduces memory consumption. Except for the original image, the additional memory that needs to be allocated is only part of the size of the original image. , even with very little memory consumption depending on the window settings.
Key code 1. Area division and RMS calculation

在这里插入代码片
//整图计算有效区域,划分成若个窗格进行搜索,生成特征矩阵,支持ROI区域计算
static int SearchCentre(unsigned char *pimage,int width,int w_stride,int h_stride,const COLOR_LAB_THRESHOLEDS *color_thread,SEARCH_AREA *Area,unsigned char *pResult)
{
   
    
    
 unsigned int i,j,k;
 int FailCount=0;
 COLOR_LAB LAB;
 uint16_t *row_ptr=NULL;
 
 for(i=Area->X_Start;i<Area->X_End;i+=h_stride) //行
 {
   
    
     
  
  for(j=Area->Y_Start*2;j<Area->Y_End*2;j+=w_stride*2) //列
  {
   
    
    
   FailCount=0;  //每个区域内不符合阈值的累计 
   k=0;
   for(k=0;k<w_stride*2;k+=2)
   {
   
    
    
    row_ptr=(uint16_t*)(pimage+(i+h_stride/2)*width*2+j+k); //取16位   行不变
    if(!ColorMatchLAB(*row_ptr,color_thread))
    {
   
    
    
     FailCount++;
    }
    if(FailCount>((w_stride+h_stride)>>2))
    {
   
    
       
     break;
    }
   }
   
   if(k==w_stride*2)
   {
   
    
    
    k=0;
    for(k=0;k<h_stride;k++)
    {
   
    
    
     row_ptr=(uint16_t *)(pimage+(i+k)*width*2+j+w_stride); //取16位   列不变    
     if(!ColorMatchLAB(*row_ptr,color_thread))
     {
   
    
    
      FailCount++

Guess you like

Origin blog.csdn.net/weixin_40672861/article/details/123261312