我有一个阈值化的二进制图像,如下所示:
我想在图像中找到所有连接的对象。
代码将采用输入图像流并且不提供。连接组件作为输出。
我已经在C中实现了它,其中存储了矩阵,可以通过A[][]
格式直接访问。但是在HLS中,图像以流形式传入,我已在hls::Mat
中进行了转换。我不确定我是否可以在Mat上执行元素操作,以及mat是否可用于离线操作,就像矩阵一样。
我无法弄清楚如何在Vivado HLS中实现C代码。
unsigned char paddedImg[FULL_HD_HEIGHT+2][FULL_HD_WIDTH+2] = {0};
void connectedComponents(unsigned char* Image,int width, int height, int
connectType, int* noOfComponents, char* list)
{
unsigned char west, north,northWest, northEast,south,east,southEast,southWest = 0;
int label = 0;
int min = 0;
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
paddedImg[i+1][j+1] = Image[(i*width) + j];
}
}
for(int i=1; i<(height+1); i++)
{
for(int j=1; j<(width+1); j++)
{
west = paddedImg[i][j-1];
northWest = paddedImg[i-1][j-1];
north = paddedImg[i-1][j];
northEast = paddedImg[i-1][j+1];
if(paddedImg[i][j] != 0)
{
min = 25500;
if(connectType == 8)
{
if( west != 0 || north != 0 || northWest != 0 || northEast != 0)
{
if(west < min && west != 0) min = west;
if(northWest < min && northWest != 0) min = northWest;
if(north < min && north != 0) min = north;
if(northEast < min && northEast != 0) min = northEast;
paddedImg[i][j] = min;
}
else
{
paddedImg[i][j] = ++label;
}
}
else if(connectType == 4)
{
if( west != 0 || north != 0)
{
if(west < min && west != 0) min = west;
if(north < min && north != 0) min = north;
paddedImg[i][j] = min;
}
else
{
paddedImg[i][j] = ++label;
}
}
else
{
printf("invalid connect type\n");
}
}
}
}
for(int i=1; i<height+1; i++)
{
for(int j=1; j<width+1; j++)
{
if(paddedImg[i][j] != 0)
{
if(connectType == 8)
{
west = paddedImg[i][j-1];
northWest = paddedImg[i-1][j-1];
north = paddedImg[i-1][j];
northEast = paddedImg[i-1][j+1];
east = paddedImg[i][j+1];
southEast = paddedImg[i+1][j+1];
south = paddedImg[i+1][j];
southWest = paddedImg[i+1][j-1];
min = paddedImg[i][j];
if(west < min && west != 0) min = west;
if(northWest < min && northWest != 0) min = northWest;
if(north < min && north != 0) min = north;
if(northEast < min && northEast != 0) min = northEast;
if(east < min && east != 0) min = east;
if(southEast < min && southEast != 0) min = southEast;
if(south < min && south != 0) min = south;
if(southWest < min && southWest != 0) min = southWest;
if(west != 0) paddedImg[i][j-1] = min;
if(northWest != 0) paddedImg[i-1][j-1] = min;
if(north != 0) paddedImg[i-1][j] = min;
if(northEast != 0) paddedImg[i-1][j+1] = min;
if(east != 0) paddedImg[i][j+1] = min;
if(southEast != 0) paddedImg[i+1][j+1] = min;
if(south != 0) paddedImg[i+1][j] = min;
if(southWest != 0) paddedImg[i+1][j-1] = min;
}
else if(connectType == 4)
{
west = paddedImg[i][j-1];
north = paddedImg[i-1][j];
east = paddedImg[i][j+1];
south = paddedImg[i+1][j];
min = paddedImg[i][j];
if(west < min && west != 0) min = west;
if(north < min && north != 0) min = north;
if(east < min && east != 0) min = east;
if(south < min && south != 0) min = south;
if(west != 0) paddedImg[i][j-1] = min;
if(north != 0) paddedImg[i-1][j] = min;
if(east != 0) paddedImg[i][j+1] = min;
if(south != 0) paddedImg[i+1][j] = min;
}
else
{
printf("invalid connect type\n");
}
}
}
}
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
Image[i*width + j] = paddedImg[i+1][j+1];
}
}
*noOfComponents = min;
}
答案 0 :(得分:0)
您描述的算法似乎要求将整个图像加载到FPGA内存中,以允许随机访问元素。如果图像足够小,您可以通过事先将其从流读入内存来实现。
如果图像很大或者您不想缓冲整个图像,那么论文FPGA implementation of a Single Pass Connected Components Algorithm提出了一种通过一次查找连通分量的算法。