Ask Your Question
-1

How to find each black bar's begin and end row numbers ?

asked 2016-12-24 00:54:45 -0600

Raghu G gravatar image

updated 2016-12-24 07:36:37 -0600

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

Comments

2

This is not a "do my homework"-forum. What did you try before asking here?

FooBar gravatar imageFooBar ( 2016-12-24 02:49:22 -0600 )edit

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

Raghu G gravatar imageRaghu G ( 2016-12-24 10:58:04 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-12-25 07:29:12 -0600

essamzaky gravatar image

You can use find contours to detect the black bars In your sample image you need to convert image to Black and white , the B/W should be inverted before passed to findcontours

use the following sample as a guide Contours sample

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-12-24 00:54:45 -0600

Seen: 183 times

Last updated: Dec 25 '16