Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Vec3b mycolor = img(x,y);
- Vec3b green = Vec3b(0, 255, 0);
- Vec3b red = Vec3b(0, 0, 255);
- Mat_<Vec3b> cloneArea = Mat(img.rows,img.cols,CV_8UC3);
- Mat_<Vec3b> clonePerimeterThiness = Mat(img.rows, img.cols, CV_8UC3);
- Mat_<Vec3b> cloneCenterOfMass = Mat(img.rows, img.cols, CV_8UC3);
- Mat_<Vec3b> cloneAxisOfElongation = Mat(img.rows, img.cols, CV_8UC3);
- Mat_<Vec3b> cloneAspectRatio = Mat(img.rows, img.cols, CV_8UC3);
- Mat_<Vec3b> cloneProjectionHor = Mat(img.rows, img.cols, CV_8UC3);
- Mat_<Vec3b> cloneProjectionVert = Mat(img.rows, img.cols, CV_8UC3);
- cloneArea = img.clone();
- clonePerimeterThiness = img.clone();
- cloneAspectRatio = img.clone();
- cloneAxisOfElongation = img.clone();
- cloneCenterOfMass = img.clone();
- int objectLabel = 0;
- Point2i centerOfMass;
- int objectRows = 0;
- int objectCols = 0;
- int objectArea = 0;
- int denominator = 0;
- int numerator1 = 0;
- int numerator2 = 0;
- int cmax = 0;
- int cMin = INT_MAX;
- int rmax = 0;
- int rmin = INT_MAX;
- for (int i = 0; i < img.rows; i++) {
- for (int j = 0; j < img.cols; j++) {
- if (img(i,j) == mycolor) {
- cloneArea(i, j) = green;
- objectArea++;
- objectRows += i;
- objectCols += j;
- if(cmax < j){
- cmax = j;
- }
- if (cMin > j) {
- cMin = j;
- }
- if (rmax < i) {
- rmax = i;
- }
- if (rmin > i) {
- rmin = i;
- }
- }
- }
- }
- imshow("Show area", cloneArea);
- waitKey();
- int cmRow = objectRows / objectArea;
- int cmCols = objectCols / objectArea;
- //colorCenterOfMass
- cloneCenterOfMass(cmRow,cmCols) = green;
- cloneCenterOfMass(cmRow - 1, cmCols) = green;
- cloneCenterOfMass(cmRow + 1, cmCols) = green;
- cloneCenterOfMass(cmRow, cmCols - 1) = green;
- cloneCenterOfMass(cmRow, cmCols + 1) = green;
- imshow("Center of Mass", cloneCenterOfMass);
- waitKey();
- for (int i = 0; i < img.rows; i++) {
- for (int j = 0; j < img.cols; j++) {
- if (img(i,j) == mycolor) {
- cloneProjectionHor(i, j) = red;
- cloneProjectionVert(i, j) = red;
- denominator += (i - cmRow)*(j - cmCols);
- numerator1 += (j - cmCols)*(j - cmCols);
- numerator2 += (i - cmRow)*(i - cmRow);
- }
- }
- }
- float arcTanElong = atan2(2.0 * (float)denominator, (float)numerator1 - (float)numerator2) / 2;
- float distance = 100;
- Point2i newPointCenter;
- newPointCenter.y = cmRow + distance * sin(arcTanElong);
- newPointCenter.x = cmCols + distance * cos(arcTanElong);
- Point2i newCenter;
- newCenter.x = cmCols;
- newCenter.y = cmRow;
- line(cloneAxisOfElongation,newCenter, newPointCenter, green, 2);
- imshow("Axis of Elongation", cloneAxisOfElongation);
- waitKey();
- int di8[8] = { -1, 0, 1, 0, 1, -1, 1 , -1 };
- int dj8[8] = { 0, -1, 0, 1, 1, 1 , -1 , -1 };
- int neighboursi;
- int neighboursj;
- int perimeter = 0;
- int check = 0;
- for (int i = 0; i < img.rows; i++) {
- for (int j = 0; j < img.cols; j++) {
- if (img(i,j) == mycolor) {
- check = 0;
- for (int k = 0; k < 8; k++) {
- neighboursi = i + di8[k];
- neighboursj = j + dj8[k];
- if (isInside(img,neighboursi,neighboursj) && img(neighboursi, neighboursj) != mycolor) {
- check = 1;
- clonePerimeterThiness(i,j) = green;
- }
- }
- perimeter += check;
- }
- }
- }
- imshow("Perimeter", clonePerimeterThiness);
- waitKey();
- float thinessRatio = 0.0;
- thinessRatio = 4.0 * PI*((float)objectArea / ((float)perimeter * (float)perimeter));
- float aspectRatio = 0.0;
- aspectRatio = ((float)cmax - (float)cMin + 1.0) / ((float)rmax - (float)rmin + 1.0);
- Point2i leftTop;
- leftTop.x = cMin;
- leftTop.y = rmin;
- Point2i leftBott;
- leftBott.x = cMin;
- leftBott.y = rmax;
- Point2i rightTop;
- rightTop.x = cmax;
- rightTop.y = rmin;
- Point2i rightBott;
- rightBott.x = cmax;
- rightBott.y = rmax;
- line(cloneAspectRatio, leftTop, rightTop, green, 1);
- line(cloneAspectRatio, leftTop, leftBott, green, 1);
- line(cloneAspectRatio, rightTop, rightBott, green, 1);
- line(cloneAspectRatio, leftBott, rightBott, green, 1);
- imshow("AspectRatio", cloneAspectRatio);
- waitKey();
- std::vector<int> pixelOnRows(img.rows);
- std::vector<int> pixelOnCols(img.cols);
- for (int i = 0; i < img.rows; i++) {
- pixelOnRows[i] = 0;
- }
- for (int j = 0; j < img.cols; j++) {
- pixelOnCols[j] = 0;
- }
- for (int i = 0; i < img.rows; i++) {
- int pixelCount = 0;
- for (int j = 0; j < img.cols; j++) {
- if (img(i,j) == mycolor) {
- pixelCount++;
- }
- }
- pixelOnRows[i] = pixelCount;
- }
- for (int i = 0; i < img.cols; i++) {
- int pixelCount = 0;
- for (int j = 0; j < img.rows; j++) {
- if (img(j,i) == mycolor) {
- pixelCount++;
- }
- }
- pixelOnCols[i] = pixelCount;
- }
- for (int i = 0; i < img.rows; i++) {
- Point2i finalPoint;
- finalPoint.y = i;
- finalPoint.x = img.cols;
- Point2i startPoint;
- startPoint.y = i;
- startPoint.x = img.cols - pixelOnRows[i];
- if (pixelOnRows[i] != 0) {
- line(cloneProjectionVert, startPoint, finalPoint, green, 2);
- }
- }
- for (int i = 0; i < img.cols; i++) {
- Point2i finalPoint;
- finalPoint.y = img.rows;
- finalPoint.x = i;
- Point2i startPoint;
- startPoint.y = img.rows - pixelOnCols[i];
- startPoint.x = i;
- if (pixelOnCols[i] != 0) {
- line(cloneProjectionHor, startPoint, finalPoint, green, 2);
- }
- }
- imshow("ProjectionVert", cloneProjectionVert);
- waitKey();
- imshow("ProjectionHor", cloneProjectionHor);
- waitKey();
- std::cout << "Object area: " << objectArea << std::endl;
- std::cout << "Center of mass: " << centerOfMass.y << " " << centerOfMass.x << std::endl;
- std::cout << "Axis of Elongation " << arcTanElong << std::endl;
- std::cout << "Perimeter: " << perimeter << std::endl;
- std::cout << "Thiness ration: " << thinessRatio << std::endl;
- std::cout << "Aspect Ratio: " << aspectRatio << std::endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement