答案 0 :(得分:0)
我想你要求计算图像中每个黑色像素的长度,你可以通过用指针扫描图像来计算长度。当我们使用指向图像的指针时,指针指向第一行。这是代码
Mat image; //this is the image that contain the above image;
int numberOfRow=image.rows;//number of rows in the image
int length[4];
int forTheFirstTime=0;
int index=0;
int a=0;
for(int j=0;j<numberOfRow;j++)
{
uchar* data=image.ptr<uchar>(j);//the pointer to each row in the image
if(data[0]==0) //if the pixel at the first is black
{
a++;
}
if(data[0]==1)
{
if(forTheFirstTime==0)//if the white pixel is for the first time
{
length[index]=a;
a=0;
index++
}
else
{
//do nothing
}
}
}
更多搜索google使用指针扫描图片。
答案 1 :(得分:0)
img
是问题中的给定图像。
我是这样编码的。
Mat img_inv;
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++;
}
}
还有其他方法吗?