Ask Your Question

Raghu G's profile - activity

2016-12-25 11:07:27 -0600 received badge  Supporter (source)
2016-12-25 11:07:24 -0600 received badge  Scholar (source)
2016-12-24 10:58:04 -0600 commented question How to find each black bar's begin and end row numbers ?

Of course, @FooBar. I tried it like above. Any other ways please?

2016-12-24 07:36:37 -0600 received badge  Editor (source)
2016-12-24 01:19:39 -0600 asked a question How to find each black bar's begin and end row numbers ?

image description

... (in vertical direction)

A cv::Mat binarized image.

Thanks

I tried like following,

img is given image in this question.

bitwise_not(img, img_inv);

int totalrows = img_inv.rows;
int totalcols = img_inv.cols;

int hist[totalrows];
int blackspacecount = 0;
bool isbackground = true;

for(int i=0; i<totalrows; i++)
{
    hist[i] = countNonZero(img_inv(Rect(0,i,totalcols,1)));

    if(hist[i] > 0)
    {
        if(isbackground == true)
        {
            blackspacecount++;
            isbackground = false;
        }
    }
    else
    {
        isbackground = true;
    }
}



int blackspaces[blackspacecount][2];

int p = 0;

for(int i=1; i<totalrows; i++)
{
    if ( hist[i] > 0 && hist[i-1] == 0)
    {
        blackspaces[p][0] = i;    // Begin
    }
    if ( hist[i] == 0 && hist[i-1] > 0)
    {
        blackspaces[p][1] = i-1;    // End
        p++;
    }
}