Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

count the number of black pixel for each column in a binary image

Hi, I am new in OpenCV.I want to calculate total number of black pixel for each column. that means if in column 1 there is total 8 black pixel i want to save 8 in a array. For each column the number of total black pixel is saved in the array.This may be a stupid question but plz give me the idea or code for my program is given below.

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <cxcore.h>
#include <cv.h>
#include <highgui.h>

int main()
{
IplImage* img3 =0;
img3=cvLoadImage("binary.tiff",CV_LOAD_IMAGE_UNCHANGED);

  int height    = img3->height;
  int width     = img3->width;
  int step1      = img3->widthStep;
  int channels  = img3->nChannels;
  unsigned char* data3=(unsigned char*)img3->imageData;

// create a window
  cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
  cvMoveWindow("mainWin", 100, 100);

//copy of img3 is img4

IplImage* img4 =cvCloneImage(img3);

//set to white all img4 data

 for(i=0;i<img4->height;i++)
  {
    for(j=0;j<img4->width;j++)
    {
        img4->imageData[i*step1+j]=255;
    }
  }
//count black pixel for img3 and put the pixel one after another in img4

  for(i=0;i<img3->height;i++)
  {
    for(k=0,j=0;j<img3->width;j++)
    {
        if(data3[i*step1+j]==0)

        {
         img4->imageData[i*step1+k]=0;
         k++;
        }

     }
  }

//show the histogram image

 cvShowImage("mainWin", img4 );
 cvSaveImage("vertihisto.tiff", img4,0);

// wait for a key
 cvWaitKey(0);

 cvReleaseImage(&img3 );
cvReleaseImage(&img4 );
return 0;
}