#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <iomanip>
#include <iostream>
using namespace std;
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"cxcore.lib")
#pragma comment(lib,"highgui.lib")
//排序算法
void BubbleSort(int a[],int len)
{
int temp1=0;
int flag=0;
for(int i=0;i<len;i++)
{
flag=0;
for(int j=0;j<len-i;j++)
{
if(a[j]<a[j+1])
{
temp1=a[j];
a[j]=a[j+1];
a[j+1]=temp1;
flag=1;
}
}
if(flag==0)
break;
}
}
void foundlabel(CvMat *img,int label1,int label2)
{
for(int i=0;i<img->rows;i++)
for(int j=0;j<img->cols;j++)
{
if(cvmGet(img,i,j)==label1)
{
cvmSet(img,i,j,label2);
}
}
}
// 若 N !=Tap,进行标记整理
int storeflag=0;
bool findNumber(int a[],int len,int x)
{
for(int k=0;k<len;k++)
{
if(a[k]==x)
{
storeflag=k;
return true;
break;
}
}
return false;
}
void zl(CvMat img)
{
int a[30];
int w=1;
a[0]=0;
a[1]=(int)cvGetReal2D(&img,0,0);
for(int i=0;i<img.rows;i++)
{
for(int j=0;j<img.cols;j++)
{
int t=(int)cvGetReal2D(&img,i,j);
if(!findNumber(a,w+1,t))
{
a[++w]=t;
}
}
}
int temp1=0;
int flag=0;
for(i=0;i<w+1;i++)
{
flag=0;
for(int j=0;j<w-i;j++)
{
if(a[j]>a[j+1])
{
temp1=a[j];
a[j]=a[j+1];
a[j+1]=temp1;
flag=1;
}
}
if(flag==0)
break;
}
int b[30];
int c[30];
int s=0;
int s1=0;
for(int d=0;d<=w;d++)
{
if((a[d+1]-a[d])>1)
{
b[s++]=a[d+1];
a[d+1]=a[d]+1;
c[s1++]=a[d]+1;
}
}
for(i=0;i<img.rows;i++)
for(int j=0;j<img.cols;j++)
{
int t=(int)cvGetReal2D(&img,i,j);
if(findNumber(b,s,t))
{
cvmSet(&img,i,j,c[storeflag]);
}
}
}
void label(CvMat img) //贴标签算法
{
int lab=0;
int N=0;
for(int i=0;i<img.rows;i++)
for(int j=0;j<img.cols;j++)
{
int temp=cvmGet(&img,i,j);
if(temp==0)
{
}
else
{
if(i==0)
{
if(j!=0)
{
int left=cvmGet(&img,i,j-1);
if(left==0)
{
lab=lab+1;
N=N+1;
cvmSet(&img,i,j,lab);
}
else
{
cvmSet(&img,i,j,left);
}
}
else
{
lab=lab+1;
N=N+1;
cvmSet(&img,i,j,lab);
}
}
else
{
if(j==0) //处理左边缘上的“1”
{
int up=cvmGet(&img,i-1,j);
int upRight=cvmGet(&img,i-1,j+1);
int label1=0;;
int label2=0;
if(up>upRight)
{
label1=up;
label2=upRight;
}
else
{
label1=upRight;
label2=up;
}
// cout<<"处理左边缘上的“1”"<<endl;
// cout<<label1<<","<<label2<<endl;
if(label1==0)
{
lab=lab+1;
N=N+1;
cvmSet(&img,i,j,lab);
}
else
{
if(label1>label2 && label2>0)
{
cvmSet(&img,i,j,label2);
N=N-1;
foundlabel(&img,label1,label2);
}
else if(label2==0||label1==label2)
{
cvmSet(&img,i,j,label1);
}
}
}
else if(j==img.width-1) //处理右边缘一列上的“1”
{
int left=cvmGet(&img,i,j-1);
int upLeft=cvmGet(&img,i-1,j-1);
int up=cvmGet(&img,i-1,j);
int a[4]={left,upLeft,up,0};
BubbleSort(a,3);
int label1=a[0];
int label2=a[1];
/* cout<<"处理右边缘一列上的“1”"<<endl;
for(int k=0;k<2;k++)
{
cout<<a[k]<<" ";
}
cout<<endl;
*/ if(label1==0)
{
lab=lab+1;
N=N+1;
cvmSet(&img,i,j,lab);
}
else
{
if(label2==0)
{
cvmSet(&img,i,j,label1);
}
else
{
if(label1>label2)
{
cvmSet(&img,i,j,label2);
N=N-1;
foundlabel(&img,label1,label2);
}
if(label2==label1)
{
cvmSet(&img,i,j,label1);
}
}
}
}
else // 处理不在边缘上的“1”
{
int left=cvmGet(&img,i,j-1);
int upLeft=cvmGet(&img,i-1,j-1);
int up=cvmGet(&img,i-1,j);
int upRight=cvmGet(&img,i-1,j+1);
int a[4]={left,upLeft,up,upRight};
/* cout<<"输入的a 值: "<<endl;
for(int k=0;k<4;k++)
{
cout<<a[k]<<" ";
}
cout<<endl;
*/ BubbleSort(a,3);
int label1=a[0];
int label2=a[1];
/* cout<<"处理不在边缘上的“1”"<<endl;
for(k=0;k<4;k++)
{
cout<<a[k]<<" ";
}
cout<<endl;
*/ if(label1==0)
{
lab=lab+1;
N=N+1;
cvmSet(&img,i,j,lab);
}
else
{
if(label1>label2 && label2>0)
{
cvmSet(&img,i,j,label2);
N=N-1;
foundlabel(&img,label1,label2);
}
else if(label2==0||label1==label2)
{
cvmSet(&img,i,j,label1);
}
}
}
}
}
}
//cout<<"Lab: "<<lab<<"N: "<<N<<endl;
if(N!=lab)
{
zl(img);
}
}
void main()
{
double a[]={1,0,1,0,1,0,0,0,0,0,0,1,0,0,1,0,0,1,1,0,0,0,0,0,1,
0,1,1,0,1,0,1,1,0,1,1,0,0,0,1,0,1,0,1,1,0,1,1,0,1,
0,1,0,0,1,0,1,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,
1,0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,1,1,0,0,1,0,0,1,1,
0,0,1,1,1,0,1,1,0,0,1,1,1,0,1,0,1,0,1,0,0,0,1,0,0};
CvMat M=cvMat(5,25,CV_64FC1,a);
label(M);
cout<<endl;
cout<<"贴标签算法: "<<endl;
cout<<endl;
for(int i=0;i<M.rows;i++)
{
for(int j=0;j<M.cols;j++)
{
cout<<(int)cvGetReal2D(&M,i,j)<<" ";
}
cout<<endl;
#include "cxcore.h"
#include "highgui.h"
#include <iomanip>
#include <iostream>
using namespace std;
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"cxcore.lib")
#pragma comment(lib,"highgui.lib")
//排序算法
void BubbleSort(int a[],int len)
{
int temp1=0;
int flag=0;
for(int i=0;i<len;i++)
{
flag=0;
for(int j=0;j<len-i;j++)
{
if(a[j]<a[j+1])
{
temp1=a[j];
a[j]=a[j+1];
a[j+1]=temp1;
flag=1;
}
}
if(flag==0)
break;
}
}
void foundlabel(CvMat *img,int label1,int label2)
{
for(int i=0;i<img->rows;i++)
for(int j=0;j<img->cols;j++)
{
if(cvmGet(img,i,j)==label1)
{
cvmSet(img,i,j,label2);
}
}
}
// 若 N !=Tap,进行标记整理
int storeflag=0;
bool findNumber(int a[],int len,int x)
{
for(int k=0;k<len;k++)
{
if(a[k]==x)
{
storeflag=k;
return true;
break;
}
}
return false;
}
void zl(CvMat img)
{
int a[30];
int w=1;
a[0]=0;
a[1]=(int)cvGetReal2D(&img,0,0);
for(int i=0;i<img.rows;i++)
{
for(int j=0;j<img.cols;j++)
{
int t=(int)cvGetReal2D(&img,i,j);
if(!findNumber(a,w+1,t))
{
a[++w]=t;
}
}
}
int temp1=0;
int flag=0;
for(i=0;i<w+1;i++)
{
flag=0;
for(int j=0;j<w-i;j++)
{
if(a[j]>a[j+1])
{
temp1=a[j];
a[j]=a[j+1];
a[j+1]=temp1;
flag=1;
}
}
if(flag==0)
break;
}
int b[30];
int c[30];
int s=0;
int s1=0;
for(int d=0;d<=w;d++)
{
if((a[d+1]-a[d])>1)
{
b[s++]=a[d+1];
a[d+1]=a[d]+1;
c[s1++]=a[d]+1;
}
}
for(i=0;i<img.rows;i++)
for(int j=0;j<img.cols;j++)
{
int t=(int)cvGetReal2D(&img,i,j);
if(findNumber(b,s,t))
{
cvmSet(&img,i,j,c[storeflag]);
}
}
}
void label(CvMat img) //贴标签算法
{
int lab=0;
int N=0;
for(int i=0;i<img.rows;i++)
for(int j=0;j<img.cols;j++)
{
int temp=cvmGet(&img,i,j);
if(temp==0)
{
}
else
{
if(i==0)
{
if(j!=0)
{
int left=cvmGet(&img,i,j-1);
if(left==0)
{
lab=lab+1;
N=N+1;
cvmSet(&img,i,j,lab);
}
else
{
cvmSet(&img,i,j,left);
}
}
else
{
lab=lab+1;
N=N+1;
cvmSet(&img,i,j,lab);
}
}
else
{
if(j==0) //处理左边缘上的“1”
{
int up=cvmGet(&img,i-1,j);
int upRight=cvmGet(&img,i-1,j+1);
int label1=0;;
int label2=0;
if(up>upRight)
{
label1=up;
label2=upRight;
}
else
{
label1=upRight;
label2=up;
}
// cout<<"处理左边缘上的“1”"<<endl;
// cout<<label1<<","<<label2<<endl;
if(label1==0)
{
lab=lab+1;
N=N+1;
cvmSet(&img,i,j,lab);
}
else
{
if(label1>label2 && label2>0)
{
cvmSet(&img,i,j,label2);
N=N-1;
foundlabel(&img,label1,label2);
}
else if(label2==0||label1==label2)
{
cvmSet(&img,i,j,label1);
}
}
}
else if(j==img.width-1) //处理右边缘一列上的“1”
{
int left=cvmGet(&img,i,j-1);
int upLeft=cvmGet(&img,i-1,j-1);
int up=cvmGet(&img,i-1,j);
int a[4]={left,upLeft,up,0};
BubbleSort(a,3);
int label1=a[0];
int label2=a[1];
/* cout<<"处理右边缘一列上的“1”"<<endl;
for(int k=0;k<2;k++)
{
cout<<a[k]<<" ";
}
cout<<endl;
*/ if(label1==0)
{
lab=lab+1;
N=N+1;
cvmSet(&img,i,j,lab);
}
else
{
if(label2==0)
{
cvmSet(&img,i,j,label1);
}
else
{
if(label1>label2)
{
cvmSet(&img,i,j,label2);
N=N-1;
foundlabel(&img,label1,label2);
}
if(label2==label1)
{
cvmSet(&img,i,j,label1);
}
}
}
}
else // 处理不在边缘上的“1”
{
int left=cvmGet(&img,i,j-1);
int upLeft=cvmGet(&img,i-1,j-1);
int up=cvmGet(&img,i-1,j);
int upRight=cvmGet(&img,i-1,j+1);
int a[4]={left,upLeft,up,upRight};
/* cout<<"输入的a 值: "<<endl;
for(int k=0;k<4;k++)
{
cout<<a[k]<<" ";
}
cout<<endl;
*/ BubbleSort(a,3);
int label1=a[0];
int label2=a[1];
/* cout<<"处理不在边缘上的“1”"<<endl;
for(k=0;k<4;k++)
{
cout<<a[k]<<" ";
}
cout<<endl;
*/ if(label1==0)
{
lab=lab+1;
N=N+1;
cvmSet(&img,i,j,lab);
}
else
{
if(label1>label2 && label2>0)
{
cvmSet(&img,i,j,label2);
N=N-1;
foundlabel(&img,label1,label2);
}
else if(label2==0||label1==label2)
{
cvmSet(&img,i,j,label1);
}
}
}
}
}
}
//cout<<"Lab: "<<lab<<"N: "<<N<<endl;
if(N!=lab)
{
zl(img);
}
}
void main()
{
double a[]={1,0,1,0,1,0,0,0,0,0,0,1,0,0,1,0,0,1,1,0,0,0,0,0,1,
0,1,1,0,1,0,1,1,0,1,1,0,0,0,1,0,1,0,1,1,0,1,1,0,1,
0,1,0,0,1,0,1,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,
1,0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,1,1,0,0,1,0,0,1,1,
0,0,1,1,1,0,1,1,0,0,1,1,1,0,1,0,1,0,1,0,0,0,1,0,0};
CvMat M=cvMat(5,25,CV_64FC1,a);
label(M);
cout<<endl;
cout<<"贴标签算法: "<<endl;
cout<<endl;
for(int i=0;i<M.rows;i++)
{
for(int j=0;j<M.cols;j++)
{
cout<<(int)cvGetReal2D(&M,i,j)<<" ";
}
cout<<endl;
}
}
结果: