📄 clustertest.cpp
字号:
#include <iostream>
#include <stack>
#include "conio.h"
#include "stdlib.h"
using namespace std;
int a[]={1,1,0,1,1,1,0,0,1,1,0,1,0,0,0,0,0,0,1,1,0,1,0,1,1,1,0,1,1,1};
int mask[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
struct Node
{ int newy;
int newleft,newright;
int oldleft,oldright;
bool direction;
};
struct BASE_CLUSTER
{
int coordyt;
int coordyb;
int coordxl;
int coordxr;
int count;
};
Node M;
stack <Node> S;
BASE_CLUSTER cluster[128];
//////////////////////////////////////////////////////////////////////////////////////////////
void row_scan(int image[],int image_length,int row,int coord,int Direction,int& number,int& lastcoord)
{ int i;
i=coord;number=0; lastcoord=coord;
while(i<=image_length-1&&image[row*image_length+i]>0)
{ mask[row*image_length+i]=1;
if(i==0&&Direction==-1)
{
number=number+1;lastcoord=0;break;
}
if (i==image_length-1&&Direction==1)
{
number=number+1;lastcoord=image_length-1;break;
}
number=number+1;
lastcoord=i;
i=i+Direction;
}
}
void pop_scan (int image[],int image_length,int row,int coord,int& number,int& left,int& right)
{
int count=0;
row_scan(image,image_length,row,coord,-1,number,left);
count=number;
row_scan(image,image_length,row,coord,1,number,right);
number=count+number-1;
}
long abdoorDetection(int image[],int image_width, int image_height,BASE_CLUSTER cluster[])
{
int i,j,m,number,left,right;
long n=0;
for(i=0;i<image_height;i++)
{
for(j=0;j<image_width;j++)
{
if(image[i*image_width+j]>0&&mask[i*image_width+j]==0)
{
BASE_CLUSTER *p= new BASE_CLUSTER ; //聚类初始化
p->coordxl=j;p->coordxr=j;p->coordyb=i;p->coordyt=i;
pop_scan(image,image_width,i,j, number,left,right);
p->count=number;
M.newy=i;M.newleft=M.oldleft=left;M.newright=M.oldright=right;M.direction=true;
S.push(M);
// system("pause");
while(!S.empty())
{
Node Tag;
Tag.newy=S.top().newy;
Tag.newleft=S.top().newleft;
Tag.newright=S.top().newright;
Tag.oldleft=S.top().oldleft;
Tag.oldright=S.top().oldright;
Tag.direction=S.top().direction;
if(Tag.newy>p->coordyb) p->coordyb=Tag.newy;
if(Tag.newy<p->coordyt) p->coordyt=Tag.newy;
if(Tag.newleft<p->coordxl) p->coordxl=Tag.newleft;
if(Tag.newright>p->coordxr) p->coordxr=Tag.newright;
S.pop();
int newy=Tag.newy;
if(Tag.direction==true)
{
if(Tag.newleft<=Tag.oldleft-2)
{
for(m=Tag.newleft;m<=Tag.oldleft-2;m++) /*扫描newy-1行newleft--oldleft-2区域*/
{
if(newy-1>=0&&image[(newy-1)*image_width+m]>0&&mask[(newy-1)*image_width+m]==0)
{
pop_scan(image,image_width,newy-1,m, number,left,right);
if(number!=0) /*判断扫描结果是否为空*/
{
p->count=p->count+number;
M.newy=newy-1;M.newleft=left;M.oldleft=Tag.newleft;M.newright=right;M.oldright=Tag.newright;M.direction=false;
S.push(M);
}
}
}
}
if(Tag.newright>=Tag.oldright+2)
{
for(m=Tag.oldright+2;m<=Tag.newright;m++) /*扫描newy-1行newright>=Tag.oldright+2区域*/
{
if(newy-1>=0&&image[(newy-1)*image_width+m]>0&&mask[(newy-1)*image_width+m]==0)
{
pop_scan(image,image_width,newy-1,m, number,left,right);
if(number!=0) /*判断扫描结果是否为空*/
{ p->count=p->count+number;
M.newy=newy-1;M.newleft=left;M.oldleft=Tag.newleft;M.newright=right;M.oldright=Tag.newright;M.direction=false;
S.push(M);
}
}
}
}
for(m=Tag.newleft;m<=Tag.newright;m++) /*扫描newy+1行newleft--newright区域*/
{
if(newy+1<image_height&&image[(newy+1)*image_width+m]>0&&mask[(newy+1)*image_width+m]==0)
{
pop_scan(image,image_width,newy+1,m, number,left,right);
if(number!=0) /*判断扫描结果是否为空*/
{ p->count=p->count+number;
M.newy=newy+1;M.newleft=left;M.oldleft=Tag.newleft;M.newright=right;M.oldright=Tag.newright;M.direction=true;
S.push(M);
}
}
}
}
if(Tag.direction==false)
{
if(Tag.newleft<=Tag.oldleft-2)
{
for(m=Tag.newleft;m<=Tag.oldleft-2;m++) /*扫描newy+1行newleft--oldleft-2区域*/
{
if(newy+1<image_height&&image[(newy+1)*image_width+m]>0&&mask[(newy+1)*image_width+m]==0)
{
pop_scan(image,image_width,newy+1,m, number,left,right);
if(number!=0) /*判断扫描结果是否为空*/
{
p->count=p->count+number;
M.newy=newy+1;M.newleft=left;M.oldleft=Tag.newleft;M.newright=right;M.oldright=Tag.newright;M.direction=true;
S.push(M);
}
}
}
}
if(Tag.newright>=Tag.oldright+2)
{
for(m=Tag.oldright+2;m<=Tag.newright;m++) /*扫描newy+1行newright>=Tag.oldright+2区域*/
{
if(newy+1<image_height&&image[(newy+1)*image_width+m]>0&&mask[(newy+1)*image_width+m]==0)
{
pop_scan(image,image_width,newy+1,m, number,left,right);
if(number!=0) /*判断扫描结果是否为空*/
{
p->count=p->count+number;
M.newy=newy+1;M.newleft=left;M.oldleft=Tag.newleft;M.newright=right;M.oldright=Tag.newright;M.direction=true;
S.push(M);
}
}
}
}
for(m=Tag.newleft;m<=Tag.newright;m++) /*扫描newy-1行newleft--newright区域*/
{
if(newy-1>=0&&image[(newy-1)*image_width+m]>0&&mask[(newy-1)*image_width+m]==0)
{
pop_scan(image,image_width,newy-1,m, number,left,right);
if(number!=0) /*判断扫描结果是否为空*/
{
p->count=p->count+number;
M.newy=newy-1;M.newleft=left;M.oldleft=Tag.newleft;M.newright=right;M.oldright=Tag.newright;M.direction=false;
S.push(M);
}
}
}
}
} /*while end
/* 加入聚类数组*/
if(p->count>=4) /*判断面积大小*/
{cluster[n].count=p->count;
cluster[n].coordxl=p->coordxl;cluster[n].coordxr=p->coordxr;cluster[n].coordyb=p->coordyb;cluster[n].coordyt=p->coordyt;cluster[n].count=p->count;
n=n+1;
}
delete p;
}
//else mask[i*image_width+j]=1; //扫描表示模板
}
}
return n;
}
void main()
{
int i,j;
long cluster_number;
int image_height=3;
int image_width=10;
cluster_number=abdoorDetection(a,image_width,image_height,cluster);
cout<<"| |";cout<<cluster_number;cout<<"| |"<<endl;
for(i=0;i<image_height;i++)
{ for(j=0;j<image_width;j++)
{ cout<< mask[i*image_width+j];
}
cout<<endl;
}
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -