C++ 用c+标记连接部件+;导致未使用的标签
我正在尝试创建一个连接组件标签原型,用于我的毕业设计。它主要起作用,只会导致标签使用大的数字,而跳过较小的数字 我的代码连接来自给定数组的相似值(通过卡方检验的相似性),然后有一些阈值来决定应该将中央数组值与其8个相邻数组值进行多少比较。然后,根据这一点,我创建了一个新的数组“cluster”,它将中心值的值与其相似的邻居相加,然后除以2。然后根据这一点,所有相似的值都必须被标记 结果是: 正如你所看到的,有4个物体,只有最高的数字是26而不是4。那么我如何解决这个问题呢C++ 用c+标记连接部件+;导致未使用的标签,c++,connected-components,labeling,C++,Connected Components,Labeling,我正在尝试创建一个连接组件标签原型,用于我的毕业设计。它主要起作用,只会导致标签使用大的数字,而跳过较小的数字 我的代码连接来自给定数组的相似值(通过卡方检验的相似性),然后有一些阈值来决定应该将中央数组值与其8个相邻数组值进行多少比较。然后,根据这一点,我创建了一个新的数组“cluster”,它将中心值的值与其相似的邻居相加,然后除以2。然后根据这一点,所有相似的值都必须被标记 结果是: 正如你所看到的,有4个物体,只有最高的数字是26而不是4。那么我如何解决这个问题呢 #i
#include<iostream>
#include<math.h>
#include "timer.h"
#include <openacc.h>
using namespace std;
int main()
{
#if _OPENACC
acc_init(acc_device_nvidia);
#endif
double array[8][8]=
{
{33,30,30,0,28,27,25,22}
,{32,30,29,0,26,25,23,21}
,{31,29,28,0,27,24,22,20}
,{30,28,27,0,25,23,20,19}
,{30,27,25,0,24,22,18,15}
,{27,25,21,0,22,4,16,13}
,{25,23,22,0,20,25,17,12}
,{24,23,22,0,19,17,15,11}
} ;
int height = 8;
int width = 8;
double cc[9][9];
double top_right[9][9]= {};
double thresh_array[9][9]= {};
double top[9][9]= {};
double top_left[9][9]= {};
double left[9][9]= {};
double center[9][9]= {};
double right[9][9]= {};
double bot_right[9][9]= {};
double bot[9][9]= {};
double bot_left[9][9]= {};
int label [9][9] = {} ;
double cluster[9][9] = {};
double cluster_v;
// Dis-Similarity Check via Chi-Square equation to find the similarity between central pixel and 8-neighbors
// i+2 so it would only compare central pixels with its neighbors,to reduce the calculations needed
for (int i=1; i<8; i+=2)
{
for (int j=0; j <( width); j++)
{
if (array[i][j]!= 0)
{
top_left[i-1][j] = ( (pow((array[i][j] - array[i-1][j-1]),2.0)) / (array[i][j] + array[i-1][j-1]) );
top[i-1][j] = ( (pow((array[i][j]-array[i-1][j]),2.0)) / (array[i][j] + array[i-1][j]) );
top_right[i-1][j] = ( (pow((array[i][j]- array[i-1][j+1]),2.0)) / (array[i][j] + array [i-1][j+1]) );
left[i-1][j] = ( (pow((array[i][j]-array[i][j-1]),2.0)) / (array[i][j] + array[i][j-1]) );
right[i-1][j] = ( (pow((array[i][j]-array[i][j+1]),2.0))/(array[i][j] + array[i][j+1]));
bot_left [i-1][j] = ((pow((array[i][j]-array[i+1][j-1]),2.0)) / (array[i][j] + array[i+1][j-1]));
bot [i-1][j] = ( (pow((array[i][j]-array[i+1][j]),2.0))/(array[i][j] + array[i+1][j]));
bot_right [i-1][j] = ( (pow((array[i][j]-array[i+1][j+1]),2.0))/(array[i][j] + array[i+1][j+1]));
}
}
}
double threshold=1000;
// calculating the smallest threshold to construct a new array with only similar neighbors showing
for (int i=1; i<(height); i+=2)
{
for (int j=0; j < (width); j++)
{
threshold = 1000;
(threshold <= top_left[i-1][j])? : threshold = top_left[i-1][j];
(threshold <= top[i-1][j])? : threshold = top[i-1][j];
(threshold <= top_right[i-1][j])? : threshold = top_right[i-1][j];
(threshold <=left[i-1][j])? : threshold = left[i-1][j];
(threshold <= right[i-1][j])? : threshold = right[i-1][j];
(threshold <= bot[i-1][j])? : threshold = bot[i-1][j];
(threshold <= bot_left[i-1][j])? : threshold = bot_left[i-1][j];
(threshold <=bot_right[i-1][j])? : threshold = bot_right[i-1][j];
thresh_array[i-1][j] = threshold;
}
}
// constructing the new array "cluster"
// if the pixels are less that a threshold of 0.016, it is summed with the central pixel and divided by 2
for (int i=1; i<height; i+=2)
{
for (int j=0; j < (width); j++)
{
if ( thresh_array[i-1][j] <0.016 && array[i-1][j] !=0)
{
cluster_v =0;
if(top_left[i-1][j] == thresh_array[i-1][j])
{
cluster_v = ((array[i-1][j-1] + array[i][j])/2);
cluster[i-1][j-1] = cluster_v;
cluster[i][j] = cluster_v;
}
if (top[i-1][j] == thresh_array[i-1][j])
{
cluster_v = ((array[i-1][j] + array[i][j])/2);
cluster[i-1][j] = cluster_v;
cluster[i][j] = cluster_v;
}
if (top_right[i-1][j] == thresh_array[i-1][j])
{
cluster_v = ((array[i-1][j+1] + array[i][j])/2);
cluster[i-1][j+1] = cluster_v;
cluster[i][j] = cluster_v;
}
if (left[i-1][j] == thresh_array[i-1][j])
{
cluster_v = ((array[i][j-1] + array[i][j])/2);
cluster[i][j-1] = cluster_v;
cluster[i][j] = cluster_v;
}
if (right[i-1][j] == thresh_array[i-1][j])
{
cluster_v = ((array[i][j+1] + array[i][j])/2);
cluster[i][j+1] = cluster_v;
cluster[i][j] = cluster_v;
}
if (bot_left[i-1][j] == thresh_array[i-1][j])
{
cluster_v = ((array[i+1][j-1] + array[i][j])/2);
cluster[i+1][j-1] = cluster_v;
cluster[i][j] = cluster_v;
}
if (bot[i-1][j] == thresh_array[i-1][j])
{
cluster_v = ((array[i+1][j] + array[i][j])/2);
cluster[i+1][j] = cluster_v;
cluster[i][j] = cluster_v;
}
if (bot_right[i-1][j] == thresh_array[i-1][j])
{
cluster_v = ((array[i+1][j+1] + array[i][j])/2);
cluster[i+1][j+1] = cluster_v;
cluster[i][j] = cluster_v;
}
}
}
}
// First pass of connected components labeling, checks the current pixel with top and left pixels from the new array
// according to conditions
int x=1, y=0;
for (int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
if(cluster[i][j] >0)
{
if(cluster[i-1][j] >0 )
label[i][j] = x;
if (cluster[i-1][j+1] >0)
label[i][j] = x;
if(cluster[i-1][j-1] >0)
label[i][j] = x;
if(cluster [i][j-1] >0)
label[i][j] = x;
if (cluster[i+1][j] >0)
label[i][j] = x;
if(cluster [i][j+1] >0)
label[i][j] = x;
if(cluster [i+1][j+1] >0)
label[i][j] = x;
if(cluster [i+1][j-1] >0)
label[i][j] = x;
x+=1;
}
if(cluster[i][j] ==0)
label[i][j]=0;
cout << cluster[i][j] <<" ";
}
cout << endl;
}
cout << endl;
cout << "First pass" << endl;
for (int i=0; i<8; i++)
{
for (int j=0; j<8; j++)
{
cout << label[i][j] <<" " ;
}
cout << endl;
}
cout << endl;
// second pass of connected components labeling
int z=0;
while (z<(1000))
{
for(int j=0; j<height; j++)
{
for(int i=(width-1); i>=0; i--)
{
if(label[i][j] >0 )
{
if(label[i-1][j] >0 )
label[i][j] = min(label[i][j],label[i-1][j]);
if (label[i-1][j+1] >0)
label[i][j] = min(label[i][j],label[i-1][j+1]);
if(label[i-1][j-1] >0)
label[i][j] = min(label[i][j],label[i-1][j-1]);
if(label [i][j-1] >0)
label[i][j] = min(label[i][j],label[i][j-1]);
if (label[i+1][j] >0)
label[i][j] = min(label[i][j], label[i+1][j]);
if(label [i][j+1] >0)
label[i][j] = min(label[i][j], label[i][j+1]);
if(label [i+1][j+1] >0)
label[i][j] = min(label[i][j], label[i+1][j+1]);
if(label [i+1][j-1] >0)
label[i][j] = min(label[i][j], label[i+1][j-1]);
}
}
}
z+=1;
}
cout << "Second Pass" << endl;
for (int i=0; i<8; i++)
{
for (int j=0; j<8; j++)
{
cout << label[i][j] <<" " ;
}
cout << endl;
}
}
#包括
#包括
#包括“timer.h”
#包括
使用名称空间std;
int main()
{
#如果_OPENACC
acc_init(acc_device_nvidia);
#恩迪夫
双数组[8][8]=
{
{33,30,30,0,28,27,25,22}
,{32,30,29,0,26,25,23,21}
,{31,29,28,0,27,24,22,20}
,{30,28,27,0,25,23,20,19}
,{30,27,25,0,24,22,18,15}
,{27,25,21,0,22,4,16,13}
,{25,23,22,0,20,25,17,12}
,{24,23,22,0,19,17,15,11}
} ;
整数高度=8;
整数宽度=8;
双cc[9][9];
双头右[9][9]={};
双阈值数组[9][9]={};
双层顶[9][9]={};
双层顶部_左[9][9]={};
左双[9][9]={};
双中心[9][9]={};
双右[9][9]={};
双bot_右[9][9]={};
双bot[9][9]={};
双bot_左[9][9]={};
int标签[9][9]={};
双簇[9][9]={};
双簇v;
//通过卡方方程进行不相似性检查,找出中心像素和8个相邻像素之间的相似性
//i+2,因此它只会将中心像素与其相邻像素进行比较,以减少所需的计算
对于(int i=1;这是个问题吗?如果你想使用较低的数字,你可以重新标记它们0,1,2,3
。我不打算通读你的全部代码,但通常连接的组件标记算法会指定中间标签,当你发现组件属于一起时,这些标签最终会被覆盖。@biker是的,这就是问题所在,我你能给我一些建议吗?第二次通过后,用1
替换所有1
s,用2
替换所有4
s,用3
替换所有11
s,用4
替换所有26
s,如果我想在一个更大的阵列上应用这个,比如pgm图像?我的测试显示了这么多未使用的标签,一个接一个的标签是非常低效的。再说一遍,为什么未使用的标签是一个问题?至于重新标签,最坏的情况是,你必须用查找表再次通过图像。但是我想直接回答你的问题,我不明白修改连接组件算法以仅使用编号最低的标签的任何方法。