Thursday, May 21, 2009

opencv#8: Find the vertical projection of an image

Often, it will be necessary to find the sum of pixels in each column of an image. This is called the vertical projection. An application may in optical character recognition (OCR), where we can use the vertical projection to separate connected characters. The troughs in vertical projection mostly correspond to the junction of two characters.

This piece of code find the sum of column elements of a binary/gray image and stores it in a vector. It also finds the maximum value of vertical projection.

IplImage* img;
// find the vertical projection
std::vector v_vl_proj; // holds the column sum values
double max_vl_proj = 0; // holds the maximum value
for( int i=0;i<img->width;++i )
{
CvMat col;
CvScalar col_sum;
cvGetCol( img, &col, i ); // get individual columns
col_sum = cvSum( &col ); // find the sum of ith column
v_vl_proj.push_back( col_sum.val[0] ); // push back to vector
if( col_sum.val[0]>max_vl_proj ) max_vl_proj = col_sum.val[0];
}

The same logic can be used to find the row sum of the image.

No comments:

Post a Comment

Followers