How to find each black bar's begin and end row numbers ?
... (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++;
}
}
This is not a "do my homework"-forum. What did you try before asking here?
Of course, @FooBar. I tried it like above. Any other ways please?