Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Розрахункова.doc
Скачиваний:
0
Добавлен:
01.04.2025
Размер:
113.15 Кб
Скачать

Індивідуальне завдання

Варіант 14

Значення: Х1(1,1), Х2(1,2), Х3(4,5), Х4(4,6), Х5(12,13), Х6(12,14) , Х7(7,8), Х8(6,8), Х9(7,5), Х10(0,1).

Здійснити розпізнавання образів із застосуванням:

  1. алгоритму порогової величини, Т=2;

  2. Максимінного алгоритму;

  3. алгоритму К-внутрішніх групових середніх, K=3.

Код програми розпізнавання образів алгоритмом порогової величини Т=2

#include <iostream>

#include <math.h>

using namespace std;

struct point

{

float x;

float y;

int center;

int naleg;

};

float vids (point a, point b) {

float len=0;

len=sqrt(pow((a.x-b.x),2)+pow((a.y-b.y),2));

return len;

}

int main ()

{

int n=0;

float T=2;

cout<<"T= "<<T<<endl;

cout<<" n=10 "<<endl;

n=10;

n--;

point mas[n];

for (int i=0; i<=n; i++)

{

mas[i].center=mas[i].naleg=mas[i].x=mas[i].y=0;

}

mas[0].x=1.0; mas[0].y=1.0;

mas[1].x=1.0; mas[1].y=2.0;

mas[2].x=4.0; mas[2].y=5.0;

mas[3].x=4.0; mas[3].y=6.0;

mas[4].x=12.0; mas[4].y=13.0;

mas[5].x=12.0; mas[5].y=14.0;

mas[6].x=7.0; mas[6].y=8.0;

mas[7].x=6.0; mas[7].y=8.0;

mas[8].x=7.0; mas[8].y=5.0;

mas[9].x=0.0; mas[9].y=1.0;

//Print array

cout<<"Array: "<<endl;

cout<<"[N] [x:y]"<<endl;

for(int i=0; i<=n; i++)

{

cout<<"["<<i<<"] ["<<mas[i].x<<":"<<mas[i].y<<"]"<<endl;

}

int nofc=-1;

int masofc[n];

for(int i=0; i<=n; i++)

{

masofc[i]=0;

}

//pruznachennya 1 centa

mas[0].center=1;

mas[0].naleg=1;

nofc++;

masofc[nofc]=1;

float maslen[n][n];

for (int i=0; i<=n; i++)

{

for (int j=0; j<=nofc; j++)

{

maslen[i][j]=vids(mas[i],mas[masofc[j]]);

}

float minlen=maslen[i][0];

int minp=0;

for (int j=0; j<=nofc; j++ )

{

if (maslen[i][j]<minlen)

{

minlen=maslen[i][j];

minp=j;

}

}

if (minlen<=T)

{

mas[i].naleg=masofc[minp];

}

if (minlen>T)

{

nofc++;

masofc[nofc]=i;

mas[i].center=i;

mas[i].naleg=i;

}

}

cout<<"Rezultal: "<<endl;

cout<<"[N] [x:y] [c] [n]"<<endl;

for (int i=0; i<=n; i++)

{

cout<<"["<<i<<"] ["<<mas[i].x<<":"<<mas[i].y<<"] ["<<mas[i].center<<"] ["<<mas[i].naleg<<"]"<<endl;

}

cout<<"Program Done!"<<endl;

return 0;

}

Результат виконання:

Код програми розпізнавання образів за допомогою максимінного алгоритму

#include <iostream>

#include <math.h>

using namespace std;

struct point

{

float x;

float y;

int z;

};

float vids (point a, point b)

{

float len=0;

len=sqrt(pow((a.x-b.x),2)+pow((a.y-b.y),2));

return len;

}

int main ()

{

int n=10;

float k=0.5;

cout<<"T= "<<k<<endl;

cout<<"n= "<<n<<endl;

n--;

point mas[n];

mas[0].x=1.0; mas[0].y=1.0;

mas[1].x=1.0; mas[1].y=2.0;

mas[2].x=4.0; mas[2].y=5.0;

mas[3].x=4.0; mas[3].y=6.0;

mas[4].x=12.0; mas[4].y=13.0;

mas[5].x=12.0; mas[5].y=14.0;

mas[6].x=7.0; mas[6].y=8.0;

mas[7].x=6.0; mas[7].y=8.0;

mas[8].x=7.0; mas[8].y=5.0;

mas[9].x=0.0; mas[9].y=1.0;

for (int i=0; i<=n; i++) {

mas[i].z=0; }

cout<<"Array :"<<endl;

cout<<"[N] [x:y] [c]"<<endl;

for(int w=0; w<=n; w++)

cout<<"["<<w<<"] ["<<mas[w].x<<":"<<mas[w].y<<"] ["<<mas[w].z<<"]"<<endl;

int nclaster=1;//liculnuk klasteriv

int masclaster[n];//Masuv n of claster - pos in mas

masclaster[nclaster-1]=0;//

mas[0].z=nclaster;// vuburaemo 0 element centrom 1 klastera

float len[n][n];//mas len from center of claster[1] to point[2]

float lca=0; //

float amin[n];// mas of min d

float l=0;

int p=0;

int flag=2;//flag of end

while (flag)

{

for (int i=0; i<=n; i++) //miraemo vidstan vid centra clastera do tochok

{

if(mas[i].z!=0)

{

for(int j=0; j<=n; j++)

{

len[nclaster-1][j]=0;

if(mas[j].z==0)

{

len[nclaster-1][j]=vids(mas[i],mas[j]);

}

}

{

for(int j=0; j<=n;j++)

{

amin[j]=len[0][j]; //vids vi 0 clastera do j tochku

}

for(int j=0; j<=n; j++)

{

for (int k=0; k<=nclaster-1; k++)

{

if(amin[j]>len[k][j])

{

amin[j]=len[k][j];

}

}

}

l=amin[0];

p=0;

for (int j=0;j<=n; j++)

{

if(amin[j]>l)

{

l=amin[j];

p=j;

}

}

if (l<k*lca)

{

flag=0;

break;

}

//dodavannya novogo clastera

if(mas[p].z==0)

{

nclaster++;

lca=lca+l;

lca=lca/nclaster;

mas[p].z=nclaster;// bind of new claster

masclaster[nclaster-1]=p;

}

}

}

}

flag=0;

}

//Print clasters

cout<<"Clasters:"<<endl;

cout<<"[N] [x:y] [p]"<<endl;

for(int i=0;i<=n;i++)

{

if (mas[i].z!=0)

cout<<"["<<mas[i].z<<"] ["<<mas[i].x<<":"<<mas[i].y<<"] ["<<i<<"]"<<endl;

}

return 0;

}

Результат виконання:

Розпізнавання образів за допомогою алгоритму К-внутрішніх групових середніх, де K=3.

#include <iostream>

#include <math.h>

using namespace std;

struct point

{

float x;

float y;

int claster;

};

struct pointc

{

float x;

float y;

int count;

};

float vids (point a, pointc b)

{

float len=0;

len=sqrt(pow((a.x-b.x),2)+pow((a.y-b.y),2));

return len;

}

int main ()

{

int n=10;

int k=3;

k--;

cout<<"K= "<<k+1<<endl;

n--;

point mas[n];

for (int i=0; i<=n; i++)

{

mas[i].claster=-1;

mas[i].x=mas[i].y=0;

}

mas[0].x=1.0; mas[0].y=1.0;

mas[1].x=1.0; mas[1].y=2.0;

mas[2].x=4.0; mas[2].y=5.0;

mas[3].x=4.0; mas[3].y=6.0;

mas[4].x=12.0; mas[4].y=13.0;

mas[5].x=12.0; mas[5].y=14.0;

mas[6].x=7.0; mas[6].y=8.0;

mas[7].x=6.0; mas[7].y=8.0;

mas[8].x=7.0; mas[8].y=5.0;

mas[9].x=0.0; mas[9].y=1.0;

//Print array

cout<<"Array : "<<endl;

cout<<"[N] [x:y]\n"<<endl;

for(int i=0; i<=n; i++)

{

cout<<"["<<i<<"] ["<<mas[i].x<<":"<<mas[i].y<<"]"<<endl;

}

pointc masdold[k];

pointc masdnew[k];

for (int i=0; i<=k; i++)

{

masdold[i].x=mas[i].x;

masdold[i].y=mas[i].y;

masdold[i].count=0;

}

for (int i=0; i<=k; i++)

{

masdnew[i].x=-100;

masdnew[i].y=-100;

masdnew[i].count=0;

}

float maslen[n][k];

for (int i=0; i<=n; i++)

{

for (int j=0; j<=k; j++)

{

maslen[i][j]=0;

}

}

int flag=1;

while(flag!=0)

{

for (int i=0; i<=n; i++)

{

for (int j=0; j<=k; j++) //vidstan do sectoriv

{

maslen[i][j]=0;

maslen[i][j]=vids(mas[i],masdold[j]);

}

}

//Search min distanse

int minp=-1;

int minlen=1000;

for (int i=0; i<=n; i++)

{

minlen=1000;

minp=-1;

for (int j=0; j<=k; j++)

{

if (maslen[i][j]<minlen)

{

minlen=maslen[i][j];

minp=j;

}

}

mas[i].claster=minp;

masdold[minp].count++;

}

for (int i=0; i<=k; i++)

{

masdnew[i].x=-100; //

masdnew[i].y=-100; //

masdnew[i].count=0;

}

float serx=0;

float sery=0;

//cout<<"Find new centers of claster"<<endl;

for (int i=0; i<=k; i++)

{

serx=sery=0;

for (int j=0; j<=n; j++)

{

if (mas[j].claster==i)

{

serx+=mas[j].x;

sery+=mas[j].y;

}

}

serx/=masdold[i].count;

sery/=masdold[i].count;

masdnew[i].x=serx;

masdnew[i].y=sery;

serx=sery=0;

}

for (int i=0; i<=k; i++)

{

if ((masdold[i].x==masdnew[i].x) || (masdold[i].x==masdnew[i].x))

{

flag=0;

}

}

if (flag!=0)

{

for (int i=0; i<=k; i++)

{

masdold[i].x=masdnew[i].x;

masdold[i].y=masdnew[i].y;

masdold[i].count=0;

}

}

}

cout<<"Rezultat: "<<endl;

cout<<"[N] [x:y] [n]"<<endl;

for (int i=0; i<=n; i++)

{

cout<<"["<<i<<"] ["<<mas[i].x<<":"<<mas[i].y<<"] ["<<mas[i].claster<<"]"<<endl;

}

cout<<"Masdold = "<<endl;

cout<<"[N] [x:y] [np]"<<endl;

for (int i=0; i<=k; i++)

{

cout<<"["<<i<<"] ["<<masdold[i].x<<":"<<masdold[i].y<<"] ["<<masdold[i].count<<"]"<<endl;

}

return 0;

}

Результат виконання: