mercredi 26 juin 2019

How to perform component labelling in a .bmp(binary image) containing digits 9 4 3 6?

I have the binary .bmp image of size 284*1280. The image contains the digits 9 4 3 6. I want to perform component labelling on the image and mark the labels whenever the digits occur. Initially, it is a binary image with only 0 and 1 in the 2d array(0 marked as background and 1 marked as the digits)

I tried to write a component labelling function(checking 8 neighbours) and incrementing a counter whenever I find a component labelled 1

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
int func(int w, int h, int a[][1280], int i, int j, int c)
{
 if(i==h||j==w)
  {
   return 0;
  }
 if(a[i][j+1]==1)
  {
a[i][j+1]=c; return func(w,h,a,i,j+1,c);
  }
 if(a[i+1][j]==1)
  {
a[i+1][j]=c; return func(w,h,a,i+1,j,c);
  }
 if(a[i+1][j+1]==1)
  {
a[i+1][j+1]=c; return func(w,h,a,i+1,j+1,c);
  }
 else
  {
    return 0;
  }
  }

 unsigned char* read_bmp(char *fname, int* _w, int* _h)
   {
    unsigned char head[54];
    FILE *f=fopen(fname,"rb");
    //BMP header is 54 bytes
    fread(head,1,54,f);
    int w=head[18]+(((int)head[19]) << 8)+(((int)head[20]) << 16)+ 
    (((int)head[21]) << 24);
    int h=head[22]+(((int)head[23]) << 8)+(((int)head[24]) << 16)+ 
    (((int)head[25]) << 24);
    //lines are aligned on 4-byte boundary
    int lineSize = (w / 8 + (w / 8) % 4);
    int fileSize=lineSize * h;
    unsigned char *img, *data;
    img =(unsigned char*)malloc(w * h), data =(unsigned 
    char*)malloc(fileSize);
    //skip the header
    fseek(f,54,SEEK_SET);
    //skip palette - two rgb quads, 8 bytes
    fseek(f,8,SEEK_CUR);
    //read data
    fread(data,1,fileSize,f);
    //decode bits
    int i, j, k, rev_j;
    for(j=0, rev_j=h-1;j<h;j++,rev_j--)
    {
      for(i=0;i<w/8;i++)
      {
        int fpos= j * lineSize + i, pos = rev_j * w + i * 8;
         for(k=0;k<8;k++)
          {
           img[pos+(7-k)]=(data[fpos] >> k) & 1;
          }
       }
    }
     free(data);
     *_w = w; *_h = h;
     return img;
   }


   int main()
   {
    int w, h, i, j, c1=0, c2=0, c3=0, c4=0, c5=0, c6=0;
    unsigned char* img=read_bmp("binary.bmp",&w,&h);
    int array[h][1280];
    char ch;
    for(j=0;j<h;j++)
     {
       for(i=0;i<1280;i++)
       {
        array[j][i]=(int(img[j * w + i])==0);
       }
     }
      register int c=2;
      for(i=0;i<h;i++)
      {
        for(j=0;j<1280;j++)
        {
          if(array[i][j]==1)
          {
            array[i][j]=c;
            func(w,h,array,i,j,c);
          }
       }
     }
      for(i=0;i<h;i++)
      {
       for(j=0;j<w;j++)
       {
         printf("%d",array[i][j]);
       }
      printf("\n");
     }
     return 0;
 }   

I am getting an array of just 0 and 2, whereas it should contain 0,2,3,4,5 labels for other digits. How to fix it?

Aucun commentaire:

Enregistrer un commentaire