Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // K-MEAN
- #include "Task7.hpp"
- //________________________________________indexing
- void Task7::Index_f(Mat G, Mat Index, int y, int x, int idx)
- {
- if (Index.at<uchar>(y, x) == 0 && (G.at<uchar>(y, x) == 255))
- {
- Index.at<uchar>(y, x) = idx;
- Index_f(G, Index, y + 1, x, idx);
- Index_f(G, Index, y - 1, x, idx);
- Index_f(G, Index, y, x + 1, idx);
- Index_f(G, Index, y, x - 1, idx);
- Index_f(G, Index, y - 1, x - 1, idx);
- Index_f(G, Index, y + 1, x - 1, idx);
- Index_f(G, Index, y - 1, x + 1, idx);
- Index_f(G, Index, y + 1, x + 1, idx);
- }
- return;
- }
- int Task7::Moment(Mat Index, Mat Cont, int idx, int p, int q)
- {
- int sum_m = 0;
- for (int y = 0; y < Index.rows; y++)
- {
- for (int x = 0; x < Index.cols; x++)
- {
- if (Index.at<uchar>(y, x) == idx)
- {
- Cont.at<uchar>(y, x) = 1;
- sum_m = sum_m + (pow(x, p)*pow(y, q));
- }
- }
- }
- return sum_m;
- }
- float Task7::Centmoment(Mat Index, Mat Cont, int idx, int p, int q, float xT, float yT)
- {
- int sum_p = 0;
- for (int y = 1; y < Index.rows - 1; y++)
- {
- for (int x = 1; x < Index.cols - 1; x++)
- {
- if (Index.at<uchar>(y, x) == idx)
- {
- sum_p = sum_p + (pow((x - xT), p)*pow((y - yT), q));
- }
- }
- }
- return sum_p;
- }
- int Task7::Perimm(Mat Index, int idx)
- {
- int perimeter = 0;
- for (int y = 0; y < Index.rows; y++)
- {
- for (int x = 0; x < Index.cols; x++)
- {
- if (Index.at<uchar>(y, x) == idx)
- {
- if ((Index.at<uchar>(y, x - 1) != idx) || (Index.at<uchar>(y, x + 1) != idx) || (Index.at<uchar>(y - 1, x) != idx) || (Index.at<uchar>(y + 1, x) != idx))
- {
- perimeter++;
- }
- }
- }
- }
- return perimeter;
- }
- Mat Task7::Erase(Mat Cont)
- {
- for (int y = 0; y < Cont.rows; y++)
- {
- for (int x = 0; x < Cont.cols; x++)
- {
- Cont.at<uchar>(y, x) = 0;
- }
- }
- return Cont;
- }
- Task7::Task7()
- {
- cv::Mat src_img;
- src_img = cv::imread("train.png", 0);
- if (!src_img.data)
- {
- printf("No image loaded.\n");
- return;
- }
- cv::Mat g_img(src_img.rows, src_img.cols, CV_8U);
- cv::Mat index(src_img.rows, src_img.cols, CV_8U);
- cv::Mat cont(src_img.rows, src_img.cols, CV_8U);
- uchar t = 128;
- //________________________________________tresholding
- for (int y = 0; y < src_img.rows; y++)
- {
- for (int x = 0; x < src_img.cols; x++)
- {
- if (src_img.at<uchar>(y, x) > t)
- {
- g_img.at<uchar>(y, x) = 255;
- }
- else if (src_img.at<uchar>(y, x) <= t)
- {
- g_img.at<uchar>(y, x) = 0;
- }
- }
- }
- index = Erase(index);
- cont = Erase(cont);
- int idx = 25;
- int count = 0;
- for (int y = 0; y < src_img.rows; y++)
- {
- for (int x = 0; x < src_img.cols; x++)
- {
- if ((g_img.at<uchar>(y, x) == 255) && (index.at<uchar>(y, x) == 0))
- {
- Index_f(g_img, index, y, x, idx);
- idx += 15;
- count++;
- }
- }
- }
- printf("\n\nPocet detekovanych objektu %d\n", count);
- //float eth_F1[3] = { 0.0, 0.0, 0.0 };
- //float eth_F2[3] = { 0.0, 0.0, 0.0 };
- int* m10 = NULL;
- int* m01 = NULL;
- float* x_T = NULL;
- float* y_T = NULL;
- float* my02 = NULL;
- float* my20 = NULL;
- float* my00 = NULL;
- float* my11 = NULL;
- int* perimeter = NULL;
- float* F1 = NULL;
- float* F2 = NULL;
- int* type = NULL;
- int* Type = NULL;
- m10 = new int[count];
- m01 = new int[count];
- x_T = new float[count];
- y_T = new float[count];
- my02 = new float[count];
- my20 = new float[count];
- my11 = new float[count];
- my00 = new float[count];
- perimeter = new int[count];
- F1 = new float[count];
- F2 = new float[count];
- type = new int[count];
- Type = new int[count];
- for (int j = 0; j < count; j++){
- m10[j] = 0;
- m01[j] = 0;
- x_T[j] = 0.0;
- y_T[j] = 0.0;
- my02[j] = 0.0;
- my20[j] = 0.0;
- my11[j] = 0.0;
- my00[j] = 0.0;
- perimeter[j] = 0;
- F1[j] = 0.0;
- F2[j] = 0.0;
- }
- int i = 0;
- for (int y = 0; y < index.rows; y++)
- {
- for (int x = 0; x < index.cols; x++)
- {
- if (index.at<uchar>(y, x) != 0 && cont.at<uchar>(y, x) == 0)
- {
- int area;
- float my_min = 0.0;
- float my_max = 0.0;
- area = Moment(index, cont, index.at<uchar>(y, x), 0, 0);
- //printf("Area of the object %d is %d\n",i+1, area);
- m10[i] = Moment(index, cont, index.at<uchar>(y, x), 1, 0);
- m01[i] = Moment(index, cont, index.at<uchar>(y, x), 0, 1);
- x_T[i] = (float)m10[i] / (float)area;
- y_T[i] = (float)m01[i] / (float)area;
- //printf("\nThe center of object %d is in [%f, %f]\n", i + 1, x_T[i], y_T[i]);
- my20[i] = Centmoment(index, cont, index.at<uchar>(y, x), 2, 0, x_T[i], y_T[i]);
- my02[i] = Centmoment(index, cont, index.at<uchar>(y, x), 0, 2, x_T[i], y_T[i]);
- my11[i] = Centmoment(index, cont, index.at<uchar>(y, x), 1, 1, x_T[i], y_T[i]);
- my00[i] = Centmoment(index, cont, index.at<uchar>(y, x), 0, 0, x_T[i], y_T[i]);
- perimeter[i] = Perimm(index, index.at<uchar>(y, x));
- //printf("The perimeter of an object %d is %d \n",i+1, perimeter[i]);
- //printf("Area of the object %d is my00 = %f\n", i + 1, my00[i]);
- F1[i] = pow(perimeter[i], 2) / (100 * my00[i]);
- my_max = ((my20[i] + my02[i]) / 2) + ((sqrtf(abs((4 * my11[i]) + powf((my20[i] - my02[i]), 2)))) / 2);
- my_min = ((my20[i] + my02[i]) / 2) - ((sqrtf(abs((4 * my11[i]) + powf((my20[i] - my02[i]), 2)))) / 2);
- //printf(" my20 = %f, my02 = %f, my11 = %f, my00 = %f, my_min = %f and my_max = %f \n", my20[i], my02[i], my11[i], my00[i], my_min, my_max);
- F2[i] = my_min / my_max;
- //printf("The features of an object %d are F1 = %f and F2 = %f\n", i + 1, F1[i], F2[i]);
- i++;
- }
- }
- }
- //K-MEANS
- float centr_1[3] = { 0.0, 0.1, 0.2 };
- float centr_2[3] = { 0.85, 0.8, 0.5 };
- float old_centr_1[3] = { 0.0, 0.0, 0.0 };
- float old_centr_2[3] = { 0.0, 0.0, 0.0 };
- float* one_1 = NULL;
- one_1 = new float[count];
- float* two_1 = NULL;
- two_1 = new float[count];
- float* three_1 = NULL;
- three_1 = new float[count];
- float* one_2 = NULL;
- one_2 = new float[count];
- float* two_2 = NULL;
- two_2 = new float[count];
- float* three_2 = NULL;
- three_2 = new float[count];
- for (int k = 0; k < 10; k++)
- {
- int Counter[3] = { 0, 0, 0 };
- float Suma_1[3] = { 0.0, 0.0, 0.0 };
- float Suma_2[3] = { 0.0, 0.0, 0.0 };
- for (int j = 0; j < count; j++)
- {
- one_1[j] = 0.0;
- one_2[j] = 0.0;
- two_1[j] = 0.0;
- two_2[j] = 0.0;
- three_1[j] = 0.0;
- three_2[j] = 0.0;
- float dist_1 = sqrtf(pow((F1[j] - centr_1[0]), 2) + pow((F2[j] - centr_2[0]), 2));
- float dist_2 = sqrtf(pow((F1[j] - centr_1[1]), 2) + pow((F2[j] - centr_2[1]), 2));
- float dist_3 = sqrtf(pow((F1[j] - centr_1[2]), 2) + pow((F2[j] - centr_2[2]), 2));
- if ((dist_1 < dist_2) && (dist_1 < dist_3))
- {
- Type[j] = 1;
- }
- else if ((dist_2 < dist_1) && (dist_2 < dist_3))
- {
- Type[j] = 2;
- }
- else if ((dist_3 < dist_1) && (dist_3 < dist_2))
- {
- Type[j] = 3;
- }
- switch (Type[j])
- {
- case 1:
- one_1[j] = F1[j];
- one_2[j] = F2[j];
- break;
- case 2:
- two_1[j] = F1[j];
- two_2[j] = F2[j];
- break;
- case 3:
- three_1[j] = F1[j];
- three_2[j] = F2[j];
- break;
- }
- }
- for (int j = 0; j < count; j++)
- {
- if ((one_1[j] != 0.0) || (one_2[j] != 0.0))
- {
- Counter[0] = Counter[0] + 1;
- Suma_1[0] = Suma_1[0] + one_1[j];
- Suma_2[0] = Suma_2[0] + one_2[j];
- }
- else if ((two_1[j] != 0.0) || (two_2[j] != 0.0))
- {
- Counter[1] = Counter[1] + 1;
- Suma_1[1] = Suma_1[1] + two_1[j];
- Suma_2[1] = Suma_2[1] + two_2[j];
- }
- else if ((three_1[j] != 0.0) || (three_2[j] != 0.0))
- {
- Counter[2] = Counter[2] + 1;
- Suma_1[2] = Suma_1[2] + three_1[j];
- Suma_2[2] = Suma_2[2] + three_2[j];
- }
- }
- old_centr_1[0] = centr_1[0];
- old_centr_1[1] = centr_1[1];
- old_centr_1[2] = centr_1[2];
- old_centr_2[0] = centr_2[0];
- old_centr_2[1] = centr_2[1];
- old_centr_2[2] = centr_2[2];
- centr_1[0] = Suma_1[0] / (float)Counter[0];
- centr_1[1] = Suma_1[1] / (float)Counter[1];
- centr_1[2] = Suma_1[2] / (float)Counter[2];
- centr_2[0] = Suma_2[0] / (float)Counter[0];
- centr_2[1] = Suma_2[1] / (float)Counter[1];
- centr_2[2] = Suma_2[2] / (float)Counter[2];
- float differ_0 = sqrtf(pow((old_centr_1[0] - centr_1[0]), 2) + pow((old_centr_2[0] - centr_2[0]), 2));
- float differ_1 = sqrtf(pow((old_centr_1[1] - centr_1[1]), 2) + pow((old_centr_2[1] - centr_2[1]), 2));
- float differ_2 = sqrtf(pow((old_centr_1[2] - centr_1[2]), 2) + pow((old_centr_2[2] - centr_2[2]), 2));
- if ((differ_0 < 0.0001) && (differ_1 < 0.0001) && (differ_2 < 0.0001))
- {
- goto end;
- }
- printf("\n\nThe Centroid_1 = %f and %f and %f\n", centr_1[0], centr_1[1], centr_1[2]);
- printf("The Centroid_2 = %f and %f and %f\n\n", centr_2[0], centr_2[1], centr_2[2]);
- }
- end:
- //Detekce objektų pomoci K-MEANS
- for (int j = 0; j < count; j++)
- {
- float vzdal_1 = sqrtf(pow((F1[j] - centr_1[0]), 2) + pow((F2[j] - centr_2[0]), 2));
- float vzdal_2 = sqrtf(pow((F1[j] - centr_1[1]), 2) + pow((F2[j] - centr_2[1]), 2));
- float vzdal_3 = sqrtf(pow((F1[j] - centr_1[2]), 2) + pow((F2[j] - centr_2[2]), 2));
- //printf("Vzdal 1 = %f, Vzdal 2 = %f, Vzdal 3 = %f \n\n", vzdal_1, vzdal_2, vzdal_3);
- if ((vzdal_1 < vzdal_2) && (vzdal_1 < vzdal_3))
- {
- type[j] = 1;
- }
- else if ((vzdal_2 < vzdal_1) && (vzdal_2 < vzdal_3))
- {
- type[j] = 2;
- }
- else if ((vzdal_3 < vzdal_1) && (vzdal_3 < vzdal_2))
- {
- type[j] = 3;
- }
- switch (type[j])
- {
- case 1:
- printf("The object %d with the center in [%f, %f] is SQUARE \n\n", j + 1, x_T[j], y_T[j]);
- cv::putText(index, "S", cv::Point(x_T[j], y_T[j]), FONT_ITALIC, 0.6, Scalar(150, 0, 1), false);
- break;
- case 2:
- printf("The object %d with the center in [%f, %f] is STAR \n\n", j + 1, x_T[j], y_T[j]);
- cv::putText(index, "*", cv::Point(x_T[j], y_T[j]), FONT_ITALIC, 0.6, Scalar(150, 0, 1), false);
- break;
- case 3:
- printf("The object %d with the center in [%f, %f] is RECTANGLE \n\n", j + 1, x_T[j], y_T[j]);
- cv::putText(index, "R", cv::Point(x_T[j], y_T[j]), FONT_ITALIC, 0.6, Scalar(150, 0, 1), false);
- break;
- }
- }
- cv::imshow("SRC", src_img);
- //cv::imshow("G", g_img);
- cv::imshow("KMEAN", index);
- delete[] m10;
- m10 = NULL;
- delete[] m01;
- m01 = NULL;
- delete[] x_T;
- x_T = NULL;
- delete[] y_T;
- y_T = NULL;
- delete[] my02;
- my02 = NULL;
- delete[] my20;
- my20 = NULL;
- delete[] my00;
- my00 = NULL;
- delete[] my11;
- my11 = NULL;
- delete[] perimeter;
- perimeter = NULL;
- delete[] F1;
- F1 = NULL;
- delete[] F2;
- F2 = NULL;
- delete[] type;
- type = NULL;
- delete[] one_1;
- one_1 = NULL;
- delete[] two_1;
- two_1 = NULL;
- delete[] three_1;
- three_1 = NULL;
- delete[] one_2;
- one_2 = NULL;
- delete[] two_2;
- two_2 = NULL;
- delete[] three_2;
- three_2 = NULL;
- delete[] Type;
- Type = NULL;
- cv::waitKey(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment