Ask Your Question
1

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

asked 2013-03-19 12:10:57 -0600

user5 gravatar 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;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2013-03-20 04:56:50 -0600

Guanta gravatar image

updated 2013-03-20 09:36:01 -0600

You can iterate by columns of a matrix using col(). Then for each column you use cv::countNonZero and subtract that from your column-size and then you have the number of zero-pixels for each column:

cv::Mat1b img = cv::imread("img.png", CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat1i black_pixels(img.cols, 1);

for(int x = 0; x < img.cols; x++){
  cv::Mat col = img.col(x);
  black_pixels(x,0) = img.rows - cv::countNonZero(col);
}

To your code: I don't know how it is related to the question. Some remarks: use the C++ instead of the C interface if you can, i.e. use Mat instead of IplImage then you can also iterate through it much easier. Furthermore, OpenCV has a histogram function, see http://docs.opencv.org/modules/imgproc/doc/histograms.html?highlight=calchist#void%20calcHist%28const%20Mat%20images,%20int%20nimages,%20const%20int%20channels,%20InputArray%20mask,%20OutputArray%20hist,%20int%20dims,%20const%20int%20histSize,%20const%20float*%20ranges,%20bool%20uniform,%20bool%20accumulate%29 . Maybe you want to use this instead of your calculation. Good luck!

Edit: Since you are dealing with binary data you can of course also use cv::sum(col)[0] instead of cv::countNonZero(col) - may be slightly faster.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-03-19 12:10:57 -0600

Seen: 5,659 times

Last updated: Mar 20 '13