数字化图像是一个m×m 的像素矩阵。在单色图像中,每个像素的值要么为0,要么为1,值为0的像素表示图像的背景,而值为1的像素则表示图元上的一个点,我们称其为图元像素。如果一个像素在另一个像素的左侧、上部、右侧或下部,则称这两个像素为相邻像素。识别图元就是对图元像素进行标记,当且仅当两个像素属于同一图元时,它们的标号相同。
queue.h-----------------------------
#include<iostream> struct location { int x; int y; }; struct Node { location data; Node* t; }; class LinkedQueue { // FIFO对象 public: LinkedQueue() {front = rear = NULL;} // 构造函数 ~LinkedQueue(); // 析构函数 bool IsEmpty() const {return ((front) ? false : true);} void Add(const location); void Delete(location& x); Node *front; // 指向第一个节点 Node *rear; //指向最后一个节点 } ; LinkedQueue::~LinkedQueue() { while(front!=NULL) { Node* m=front; front=front->t; delete m; } } void LinkedQueue::Add(const location x) { if (IsEmpty()) { front=new Node; front->data=x; front->t=NULL; rear=front; } else { Node* p=new Node; p->data=x; p->t=NULL; rear->t=p; rear=p; } } void LinkedQueue::Delete(location& x) { x=front->data; Node* p=front; front=front->t; delete p; }
识别图元.h----------------------
#include "queue.h" using namespace std; #define M 8 #define N 9 int tuyuan[M][N]= { 0,0,0,0,0,0,0,0,0, 0,0,0,1,0,0,0,0,0, 0,0,0,1,1,0,0,0,0, 0,0,0,0,0,1,0,0,0, 0,0,1,0,1,1,0,1,0, 0,1,1,1,0,1,0,1,0, 0,1,1,1,0,0,1,1,0, 0,0,0,0,0,0,0,0,0 }; location move1[4]={{1,0},{-1,0},{0,1},{0,-1}}; void main() { int i,j; int id=2; LinkedQueue queue; location x; location t; for (i=0;i<M;i++) { for (j=0;j<N;j++) { if (tuyuan[i][j]==1) { tuyuan[i][j]=id; x.x=i; x.y=j; queue.Add(x); while(queue.IsEmpty()==false) { queue.Delete(t); for (int k=0;k<4;k++) { x.x=t.x+move1[k].x; x.y=t.y+move1[k].y; if (tuyuan[x.x][x.y]==1) { tuyuan[x.x][x.y]=id; queue.Add(x); } } } id++; } } } for (i=0;i<M;i++) { for (j=0;j<N;j++) { cout<<tuyuan[i][j]<<" "; } cout<<"\n"; } system("pause"); }