我有一个任务,旨在从黑白图像中提取最大的对象,其中黑色是背景。我使用的是 2-pass 算法,这里有一个解释它是如何工作的链接: http://en.wikipedia .org/wiki/Connected-component_labeling#Two-pass
我的问题:1.我正在使用一个结构数组,我想将其尺寸定义为与“输入图像”相同,所以{我该怎么做}
2.我制作了一个包含两列和行数的数组作为等价表,但我不确定我是否正确,我该如何解决?
- 如何使用等价表“重新标记第二遍中的像素?如何编写第二遍的代码?
我的代码:
Image Image::MaxCC()
{
Image obj;
obj.height = height;
obj.width = width;
short ** original = image;
short ** output = new short*[height];
for (int i = 0; i < height; i++)
{
output[i] = new short[width];
}
obj.imageHeader = imageHeader;
obj.image = output;
//label array
//structure
struct label
{
int lab;
int counter;
};
label L[][]; //I want to use the dimensions of the input image which is obj.height and obj.width
//Initialize
for (int i = 0; i <= obj.height; i++)
{
for (int j = 0; j <= obj.width; j++)
{
L[i][j].lab = 0;
L[i][j].counter = 0;
}
}
int N = 0;
int count = 0;
//equivlance tabel
int eq[100][2];
int row = 1;
int x = 1;
int s;
// conditions [FIRST ITERATION]
for (int c = 0; c < obj.width; c++)
{
for (int r = 0; r < obj.height; r++)
{
// If the pixel is balck , add no label
if (image[r][c] == 0)
obj.image[r][c] = 0;
//Do the pixel's North and West neighbors have different pixel values than current pixel?
else if (image[r - 1][c] == 0 && image[r][c - 1] == 0)
{
L[r][c].lab = N++;
L[r][c].counter = count++;
obj.image[r][c] = L[r][c].lab;
}
//Does the pixel to the left (West) have the same value as the current pixel?
else if (image[r][c - 1] == image[r][c])
{
L[r][c - 1].counter = count++;
obj.image[r][c] = L[r][c - 1].lab;
}
//Does the pixel to the left (West) have a different value and the one to the North the same value as the current pixel?
else if (image[r - 1][c] == image[r][c] && image[r][c - 1] != image[r][c])
{
L[r - 1][c].counter = count++;
obj.image[r][c] = L[r - 1][c].lab;
}
//Do both pixels to the North and West have the same value as the current pixel but not the same label?
else if (image[r - 1][c] == image[r][c] && image[r][c - 1] == image[r][c] && L[r - 1][c].lab != L[r][c - 1].lab)
{
obj.image[r][c] = L[r - 1][c].lab;
eq[row][1] = x;
if (L[r - 1][c].counter << L[r][c - 1].counter)
{
L[r - 1][c].counter = count++;
s = L[r - 1][c].lab;
}
else
{
L[r][c - 1].counter = count++;
s = L[r][c - 1].lab;
eq[row][2] = s; //..
x++; row++;
}
}
}
//THE SECONF ITERATION ?
//Iteration to get the maximum counter
label max;
int temp;
for (int c = 0; c < obj.width; c++)
{
for (int r = 0; r < obj.height; c++)
{
temp = L[r][c].counter;
if (temp > max.counter)
max.counter = temp;
}
}
//iteratio to extract the bigger object
for (int c = 0; c < obj.width; c++)
{
for (int r = 0; r < obj.height; c++)
{
if (max.lab == L[r][c].lab)
obj.image[r][c] = 255;
else
obj.image[r][c] = 0;
}
}
}
return obj;
}