Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to do alignment of image?

I need to do the following : 1.Load largest_contour image 2. Calculate moments of the ROI 3. Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00} 4. Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image 5. Save the aligned image temporarily 6. Display the aligned image

I'm unable to find the COG and align the contour

my c++ code for finding contour :

include <iostream>

include "opencv2/core/core.hpp"

include "opencv2/imgproc/imgproc.hpp"

include "opencv2/highgui/highgui.hpp"

include <iostream>

using namespace cv; using namespace std; int main() { int largest_area = 0; int largest_contour_index = 0; Rect bounding_rect;

Mat src = imread("Morp.jpg"); //Load source image
Mat thr(src.rows, src.cols, CV_8UC1);
Mat dst(src.rows, src.cols, CV_8UC1, Scalar::all(0));
//cvtColor(src, thr, CV_BGR2GRAY); //Convert to gray
//threshold(thr, thr, 25, 255, THRESH_BINARY); //Threshold the gray

vector<vector<Point>> contours; // Vector for storing contour
vector<Vec4i> hierarchy;

findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image

for (int i = 0; i< contours.size(); i++) // iterate through each contour. 
{
    double a = contourArea(contours[i], false);  //  Find the area of contour
    if (a>largest_area) {
        largest_area = a;
        largest_contour_index = i;                //Store the index of largest contour
        bounding_rect = boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
    }

}

Scalar color(255, 255, 255);
drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy); // Draw the largest contour using previously stored index.
rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);
imshow("src", src);
imshow("Largest Contour", dst);
imwrite("Contour.jpg", dst);
waitKey(0);

}

How to do alignment of image?

I need to do the following : 1.Load largest_contour image 2. Calculate moments of the ROI 3. Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00} 4. Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image 5. Save the aligned image temporarily 6. Display the aligned image

I'm unable to find the COG and align the contour

my c++ code for finding contour :

include <iostream>

include "opencv2/core/core.hpp"

include "opencv2/imgproc/imgproc.hpp"

include "opencv2/highgui/highgui.hpp"

include <iostream>

using namespace cv; using namespace std;

int main() { int largest_area = 0; int largest_contour_index = 0; Rect bounding_rect;

Mat src = imread("Morp.jpg"); //Load source image
Mat thr(src.rows, src.cols, CV_8UC1);
Mat dst(src.rows, src.cols, CV_8UC1, Scalar::all(0));
//cvtColor(src, thr, CV_BGR2GRAY); //Convert to gray
//threshold(thr, thr, 25, 255, THRESH_BINARY); //Threshold the gray

vector<vector<Point>> contours; // Vector for storing contour
vector<Vec4i> hierarchy;

findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image

for (int i = 0; i< contours.size(); i++) // iterate through each contour. 
{
    double a = contourArea(contours[i], false);  //  Find the area of contour
    if (a>largest_area) {
        largest_area = a;
        largest_contour_index = i;                //Store the index of largest contour
        bounding_rect = boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
    }

}

Scalar color(255, 255, 255);
drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy); // Draw the largest contour using previously stored index.
rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);
imshow("src", src);
imshow("Largest Contour", dst);
imwrite("Contour.jpg", dst);
waitKey(0);

}

How to do alignment of image?

I need to do the following : 1.Load largest_contour image 2. Calculate moments of the ROI 3. Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00} 4. Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image 5. Save the aligned image temporarily 6. Display the aligned image

I'm unable to find the COG and align the contour

my c++ code for finding contour :

int main() { int largest_area = 0; int largest_contour_index = 0; Rect bounding_rect;

Mat src = imread("Morp.jpg"); //Load source image
Mat thr(src.rows, src.cols, CV_8UC1);
Mat dst(src.rows, src.cols, CV_8UC1, Scalar::all(0));
//cvtColor(src, thr, CV_BGR2GRAY); //Convert to gray
//threshold(thr, thr, 25, 255, THRESH_BINARY); //Threshold the gray

vector<vector<Point>> contours; // Vector for storing contour
vector<Vec4i> hierarchy;

findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image

for (int i = 0; i< contours.size(); i++) // iterate through each contour. 
{
    double a = contourArea(contours[i], false);  //  Find the area of contour
    if (a>largest_area) {
        largest_area = a;
        largest_contour_index = i;                //Store the index of largest contour
        bounding_rect = boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
    }

}

Scalar color(255, 255, 255);
drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy); // Draw the largest contour using previously stored index.
rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);
imshow("src", src);
imshow("Largest Contour", dst);
imwrite("Contour.jpg", dst);
waitKey(0);

}

click to hide/show revision 4
No.4 Revision

How to do alignment of image?

I need to do the following : 1.Load largest_contour image 2. Calculate moments of the ROI 3. Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00} 4. Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image 5. Save the aligned image temporarily 6. Display the aligned image

I'm unable to find the COG and align the contour

my c++ code for finding contour :

int main()
{
int largest_area = 0;
int largest_contour_index = 0;
Rect bounding_rect;

bounding_rect;
Mat src = imread("Morp.jpg"); //Load source image
 Mat thr(src.rows, src.cols, CV_8UC1);
 Mat dst(src.rows, src.cols, CV_8UC1, Scalar::all(0));
 //cvtColor(src, thr, CV_BGR2GRAY); //Convert to gray
 //threshold(thr, thr, 25, 255, THRESH_BINARY); //Threshold the gray
 vector<vector<Point>> contours; // Vector for storing contour
 vector<Vec4i> hierarchy;
 findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image
 for (int i = 0; i< contours.size(); i++) // iterate through each contour.
 {
  double a = contourArea(contours[i], false); // Find the area of contour
  if (a>largest_area) {
 largest_area = a;
  largest_contour_index = i; //Store the index of largest contour
  bounding_rect = boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
 }
 }
 Scalar color(255, 255, 255);
 drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy); // Draw the largest contour using previously stored index.
 rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);
 imshow("src", src);
 imshow("Largest Contour", dst);
 imwrite("Contour.jpg", dst);
 waitKey(0);
}

}

click to hide/show revision 5
No.5 Revision

How to do alignment of image?

I need to do the following : 1.Load :

  1. Load largest_contour image 2. image
  2. Calculate moments of the ROI 3. ROI
  3. Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00} 4. M01/M00}
  4. Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image 5. image
  5. Save the aligned image temporarily 6. temporarily
  6. Display the aligned image

I'm unable to find the COG and align the contour

my c++ code for finding contour :

int main()
{
    int largest_area = 0;
    int largest_contour_index = 0;
    Rect bounding_rect;

    Mat src = imread("Morp.jpg"); //Load source image
    Mat thr(src.rows, src.cols, CV_8UC1);
    Mat dst(src.rows, src.cols, CV_8UC1, Scalar::all(0));
    //cvtColor(src, thr, CV_BGR2GRAY); //Convert to gray
    //threshold(thr, thr, 25, 255, THRESH_BINARY); //Threshold the gray

    vector<vector<Point>> contours; // Vector for storing contour
    vector<Vec4i> hierarchy;

    findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image

    for (int i = 0; i< contours.size(); i++) // iterate through each contour. 
    {
        double a = contourArea(contours[i], false);  //  Find the area of contour
        if (a>largest_area) {
            largest_area = a;
            largest_contour_index = i;                //Store the index of largest contour
            bounding_rect = boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
        }

    }

    Scalar color(255, 255, 255);
    drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy); // Draw the largest contour using previously stored index.
    rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);
    imshow("src", src);
    imshow("Largest Contour", dst);
    imwrite("Contour.jpg", dst);
    waitKey(0);
}

How to do alignment of image?

I need to do the following :

  1. Load largest_contour image
  2. Calculate moments of the ROI
  3. Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00}
  4. Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image
  5. Save the aligned image temporarily
  6. Display the aligned image

I'm unable to find the COG and align the contour

my c++ code for finding contour :

int main()
{
    int largest_area void alignContour(vector<Point>& contour, int x, int y)

{ for (size_t i = 0; i<contour.size(); i++)="" {="" contour[i].x="" +="x;" contour[i].y="" +="y;" }="" }<="" p="">

int findBiggestContour(vector<vector<cv::point> > contours) { int indexOfBiggestContour = -1; int sizeOfBiggestContour = 0; int largest_contour_index = 0; Rect bounding_rect; Mat src = imread("Morp.jpg"); //Load source image Mat thr(src.rows, src.cols, CV_8UC1); Mat dst(src.rows, src.cols, CV_8UC1, Scalar::all(0)); //cvtColor(src, thr, CV_BGR2GRAY); //Convert to gray //threshold(thr, thr, 25, 255, THRESH_BINARY); //Threshold the gray vector<vector<Point>> contours; // Vector for storing contour vector<Vec4i> hierarchy; findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image for (int i = 0; i< i < contours.size(); i++) // iterate through each contour. { double a = contourArea(contours[i], false); // Find the area of contour { if (a>largest_area) { largest_area = a; largest_contour_index = i; //Store the index of largest contour bounding_rect = boundingRect(contours[i]); // Find the bounding rectangle for biggest contour (contours[i].size() > sizeOfBiggestContour) { sizeOfBiggestContour = contours[i].size(); indexOfBiggestContour = i; } } Scalar color(255, 255, 255); drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy); // Draw the return indexOfBiggestContour; } int main() { Mat src = imread("largest_contour.jpg"); //Load largest contour using previously stored index. rectangle(src, bounding_rect, Scalar(0, 255, image vector<vector<point> > contours; vector<vec4i> hierarchy; Mat drawing = Mat::zeros(src.size(), CV_8UC1);

//int indexOfContour;
int indexOfContour = findBiggestContour(contours);


if (indexOfContour > -1)
{

    vector<Point> ROI;
    ROI = contours[indexOfContour];

    Moments M;
    M = moments(ROI);

    Point COG;
    COG = Point(M.m10 / M.m00, M.m01 / M.m00);
    Point center(drawing.cols / 2, drawing.rows / 2);
    Mat alignedImage = Mat::zeros(src.size(), CV_8UC1);

    circle(drawing, COG, 2, Scalar(125, 100, 0), 1, 3, 8);
    circle(drawing, center, 2, Scalar(125, 100, 0), 3, 8);

    int x;
    if (COG.x > center.x)
    {
        x = COG.x - center.x;
        x = -x;
    }
    else
    {
        x = (COG.x - center.x)*-1;
    }

    int y;

    if (COG.y < center.y)
    {
        y = center.y - COG.y;
    }
    else
    {
        y = center.y - COG.y;
    }

    alignContour(contours[indexOfContour], x, y);


    drawContours(alignedImage, contours, indexOfContour, Scalar(255), -1, 8, 0);
    imshow("src", src);
    imshow("Largest Contour", dst);
    imwrite("Contour.jpg", dst);
hierarchy);


    //displaying the image
    imshow("aligned Image ", alignedImage);
    waitKey(0);
}


//saving the new image
imwrite("alignedImage.jpg", alignedImage);

}

How to do alignment of image?

I need to do the following :

  1. Load largest_contour image
  2. Calculate moments of the ROI
  3. Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00}
  4. Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image
  5. Save the aligned image temporarily
  6. Display the aligned image

I'm unable to find the COG and align the contour

my c++ code :

void alignContour(vector<Point>& contour, int x, int y)

{ for (size_t i = 0; i<contour.size(); i++)="" {="" contour[i].x="" +="x;" contour[i].y="" +="y;" }="" }<="" p="">

int findBiggestContour(vector<vector<cv::point> > contours) { int indexOfBiggestContour = -1; int sizeOfBiggestContour = 0; for (int i = 0; i < contours.size(); i++) { if (contours[i].size() > sizeOfBiggestContour) { sizeOfBiggestContour = contours[i].size(); indexOfBiggestContour = i; } } return indexOfBiggestContour; } int main() { Mat src = imread("largest_contour.jpg"); //Load largest contour image vector<vector<point> > contours; vector<vec4i> hierarchy; Mat drawing = Mat::zeros(src.size(), CV_8UC1);

//int indexOfContour;
int indexOfContour = findBiggestContour(contours);


if (indexOfContour > -1)
{

    vector<Point> ROI;
    ROI = contours[indexOfContour];

    Moments M;
    M = moments(ROI);

    Point COG;
    COG = Point(M.m10 / M.m00, M.m01 / M.m00);
    Point center(drawing.cols / 2, drawing.rows / 2);
    Mat alignedImage = Mat::zeros(src.size(), CV_8UC1);

    circle(drawing, COG, 2, Scalar(125, 100, 0), 3, 8);
    circle(drawing, center, 2, Scalar(125, 100, 0), 3, 8);

    int x;
    if (COG.x > center.x)
    {
        x = COG.x - center.x;
        x = -x;
    }
    else
    {
        x = (COG.x - center.x)*-1;
    }

    int y;

    if (COG.y < center.y)
    {
        y = center.y - COG.y;
    }
    else
    {
        y = center.y - COG.y;
    }

    alignContour(contours[indexOfContour], x, y);


    drawContours(alignedImage, contours, indexOfContour, Scalar(255), -1, 8, hierarchy);


    //displaying the image
    imshow("aligned Image ", alignedImage);
    waitKey(0);


//saving the new image
imwrite("alignedImage.jpg", alignedImage);

}

How to do alignment of image?

I need to do the following :

  1. Load largest_contour image
  2. Calculate moments of the ROI
  3. Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00}
  4. Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image
  5. Save the aligned image temporarily
  6. Display the aligned image

I'm unable to find the COG and align the contour

my c++ code :

using namespace cv; using namespace std;

void alignContour(vector<Point>& alignContour(vector& contour, int x, int y)

{ y) { for (size_t i = 0; i<contour.size(); i++)="" {="" contour[i].x="" +="x;" contour[i].y="" +="y;" }="" }<="" p="">

int findBiggestContour(vector<vector<cv::point> i

int findBiggestContour(vector > contours) { int indexOfBiggestContour = -1; int sizeOfBiggestContour = 0; for (int i = 0; i < contours.size(); i++) { { if (contours[i].size() > sizeOfBiggestContour) { { sizeOfBiggestContour = contours[i].size(); contours[i].size(); indexOfBiggestContour = i; i; } } } return indexOfBiggestContour; } int main() { } int main() { Mat src = imread("largest_contour.jpg"); //Load largest contour image vector<vector<point> image vector > contours; vector<vec4i> hierarchy; contours; vector hierarchy; Mat drawing = Mat::zeros(src.size(), CV_8UC1);CV_8UC1); //int indexOfContour; int indexOfContour = findBiggestContour(contours);

//int indexOfContour;
int indexOfContour = findBiggestContour(contours);


if (indexOfContour > -1)
{

    vector<Point> ROI;
    ROI = contours[indexOfContour];

    //calculate moments of ROI
    Moments M;
    M = moments(ROI);

    Point COG;
COG; //center of gravity of leg
    COG = Point(M.m10 / M.m00, M.m01 / M.m00);
M.m00); //find the COG of ROI using the following equation
    Point center(drawing.cols / 2, drawing.rows / 2);
    Mat alignedImage = Mat::zeros(src.size(), CV_8UC1);
CV_8UC1); //align the contour

    circle(drawing, COG, 2, Scalar(125, 100, 0), 3, 8);
    circle(drawing, center, 2, Scalar(125, 100, 0), 3, 8);

    int x;
    if (COG.x > center.x)
    {
        x = COG.x - center.x;
        x = -x;
    }
    else
    {
        x = (COG.x - center.x)*-1;
    }

    int y;

    if (COG.y < center.y)
    {
        y = center.y - COG.y;
    }
    else
    {
        y = center.y - COG.y;
    }

    alignContour(contours[indexOfContour], x, y);


    drawContours(alignedImage, contours, indexOfContour, Scalar(255), -1, 8, hierarchy);


    //displaying the image
    imshow("aligned Image ", alignedImage);
    waitKey(0);


//saving the new image
imwrite("alignedImage.jpg", alignedImage);

}

click to hide/show revision 9
No.9 Revision

updated 2016-12-19 08:01:56 -0600

berak gravatar image

How to do alignment of image?

I need to do the following :

  1. Load largest_contour image
  2. Calculate moments of the ROI
  3. Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00}
  4. Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image
  5. Save the aligned image temporarily
  6. Display the aligned image

I'm unable to find the COG and align the contour

my c++ code :

using namespace cv;
using namespace std;

std; void alignContour(vector& contour, int x, int y) { for (size_t i = 0; i

i int findBiggestContour(vector > contours) { int indexOfBiggestContour = -1; int sizeOfBiggestContour = 0; for (int i = 0; i < contours.size(); i++) { if (contours[i].size() > sizeOfBiggestContour) { sizeOfBiggestContour = contours[i].size(); indexOfBiggestContour = i; } } return indexOfBiggestContour; } int main() { Mat src = imread("largest_contour.jpg"); //Load largest contour image vector > contours; vector hierarchy; Mat drawing = Mat::zeros(src.size(), CV_8UC1); //int indexOfContour; int indexOfContour = findBiggestContour(contours);

findBiggestContour(contours);
if (indexOfContour > -1)
 {
  vector<Point> ROI;
 ROI = contours[indexOfContour];
  //calculate moments of ROI
 Moments M;
 M = moments(ROI);
  Point COG; //center of gravity of leg
  COG = Point(M.m10 / M.m00, M.m01 / M.m00); //find the COG of ROI using the following equation
 Point center(drawing.cols / 2, drawing.rows / 2);
 Mat alignedImage = Mat::zeros(src.size(), CV_8UC1); //align the contour
  circle(drawing, COG, 2, Scalar(125, 100, 0), 3, 8);
 circle(drawing, center, 2, Scalar(125, 100, 0), 3, 8);
 int x;
  if (COG.x > center.x)
 {
  x = COG.x - center.x;
 x = -x;
 }
 else
 {
  x = (COG.x - center.x)*-1;
 }
 int y;
  if (COG.y < center.y)
 {
  y = center.y - COG.y;
 }
 else
 {
  y = center.y - COG.y;
 }
  alignContour(contours[indexOfContour], x, y);
  drawContours(alignedImage, contours, indexOfContour, Scalar(255), -1, 8, hierarchy);
 //displaying the image
  imshow("aligned Image ", alignedImage);
 waitKey(0);
 //saving the new image
 imwrite("alignedImage.jpg", alignedImage);
}

}

How to do alignment of image?

I need to do the following :

  1. Load largest_contour image
  2. Calculate moments of the ROI
  3. Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00}
  4. Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image
  5. Save the aligned image temporarily
  6. Display the aligned image

I'm unable to find the COG and align the contour

my c++ code :

#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv\cv.h"
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>

using namespace cv; 
cv;
using namespace std;

void alignContour(vector& alignContour(vector<Point>& contour, int x, int y) { y)
{
    for (size_t i = 0; i
i<contour.size(); i++)
    {
        contour[i].x += x;
        contour[i].y += y;
    }
}

int findBiggestContour(vector findBiggestContour(vector<vector<cv::Point> > contours) {
  int indexOfBiggestContour = -1;
  int sizeOfBiggestContour = 0;
  for (int i = 0; i < contours.size(); i++) { 
{
        if (contours[i].size() > sizeOfBiggestContour) { 
{
            sizeOfBiggestContour = contours[i].size(); 
contours[i].size();
            indexOfBiggestContour = i; 
} 
}
i;
        }
    }
    return indexOfBiggestContour;
 } 
}
int main() { 
main()
{
    Mat src = imread("largest_contour.jpg"); //Load largest contour 
image vector image
    vector<vector<Point> > contours; 
vector hierarchy; 
contours;
    vector<Vec4i> hierarchy;
    Mat drawing = Mat::zeros(src.size(), CV_8UC1);
    //int indexOfContour;
    int indexOfContour = findBiggestContour(contours);
     if (indexOfContour > -1)
    {

        vector<Point> ROI;
        ROI = contours[indexOfContour];

        //calculate moments of ROI
        Moments M;
        M = moments(ROI);

        Point COG; //center of gravity of leg
        COG = Point(M.m10 / M.m00, M.m01 / M.m00); //find the COG of ROI using the following equation
        Point center(drawing.cols / 2, drawing.rows / 2);
        Mat alignedImage = Mat::zeros(src.size(), CV_8UC1); //align the contour

        circle(drawing, COG, 2, Scalar(125, 100, 0), 3, 8);
        circle(drawing, center, 2, Scalar(125, 100, 0), 3, 8);

        int x;
        if (COG.x > center.x)
        {
            x = COG.x - center.x;
            x = -x;
        }
        else
        {
            x = (COG.x - center.x)*-1;
        }

        int y;

        if (COG.y < center.y)
        {
            y = center.y - COG.y;
        }
        else
        {
            y = center.y - COG.y;
        }

        alignContour(contours[indexOfContour], x, y);


        drawContours(alignedImage, contours, indexOfContour, Scalar(255), -1, 8, hierarchy);


        //displaying the image
        imshow("aligned Image ", alignedImage);
        waitKey(0);


    //saving the new image
    imwrite("alignedImage.jpg", alignedImage);
}

How to do alignment of image?

I need to do the following :

  1. Load largest_contour image
  2. Calculate

    1.Calculate moments of the ROI

  3. ROI 2. Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00}
  4. Align the contour ,that is ,to M01/M00} 3. find the COG of the image and translate the ROI to the COG of the image
  5. Save the aligned image temporarily
  6. Display the aligned image

I'm unable to find the COG and align the contourcontour.No result is being obtained.

my c++ code :

#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv\cv.h"
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>

using namespace cv;
using namespace std;

void alignContour(vector<Point>& contour, int x, int y)
{
    for (size_t i = 0; i<contour.size(); i++)
    {
        contour[i].x += x;
        contour[i].y += y;
    }
}

int findBiggestContour(vector<vector<cv::Point> > contours) {
    int indexOfBiggestContour = -1;
    int sizeOfBiggestContour = 0;
    for (int i = 0; i < contours.size(); i++) {
        if (contours[i].size() > sizeOfBiggestContour) {
            sizeOfBiggestContour = contours[i].size();
            indexOfBiggestContour = i;
        }
    }
    return indexOfBiggestContour;
}
int main()
{
    Mat src = imread("largest_contour.jpg"); //Load largest contour image
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    Mat drawing = Mat::zeros(src.size(), CV_8UC1);
    int indexOfContour = findBiggestContour(contours);
    if (indexOfContour > -1)
    {

        vector<Point> ROI;
        ROI = contours[indexOfContour];

        //calculate moments of ROI
        Moments M;
        M = moments(ROI);

        Point COG; //center of gravity of leg
         COG = Point(M.m10 / M.m00, M.m01 / M.m00); //find the COG of ROI using the following equation
        Point center(drawing.cols / 2, drawing.rows / 2);
        Mat alignedImage = Mat::zeros(src.size(), CV_8UC1); //align the contour

        circle(drawing, COG, 2, Scalar(125, 100, 0), 3, 8);
        circle(drawing, center, 2, Scalar(125, 100, 0), 3, 8);

        int x;
        if (COG.x > center.x)
        {
            x = COG.x - center.x;
            x = -x;
        }
        else
        {
            x = (COG.x - center.x)*-1;
        }

        int y;

        if (COG.y < center.y)
        {
            y = center.y - COG.y;
        }
        else
        {
            y = center.y - COG.y;
        }

        alignContour(contours[indexOfContour], x, y);


        drawContours(alignedImage, contours, indexOfContour, Scalar(255), -1, 8, hierarchy);


        //displaying the image
        imshow("aligned Image ", alignedImage);
        waitKey(0);


    //saving the new image
    imwrite("alignedImage.jpg", alignedImage);
}

How to do alignment of image?

I need to do the following :

1.Calculate moments of the ROI 2. Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00} 3. find the COG of the image and translate the ROI to the COG of the image

I'm unable to find the COG and align the contour.No result is being obtained.contour.

my c++ code :

#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv\cv.h"
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>

using namespace cv;
using namespace std;

void alignContour(vector<Point>& contour, int x, int y)
{
    for (size_t i = 0; i<contour.size(); i++)
    {
        contour[i].x += x;
        contour[i].y += y;
    }
}

int findBiggestContour(vector<vector<cv::Point> > contours) {
    int indexOfBiggestContour = -1;
    int sizeOfBiggestContour = 0;
    for (int i = 0; i < contours.size(); i++) {
        if (contours[i].size() > sizeOfBiggestContour) {
            sizeOfBiggestContour = contours[i].size();
            indexOfBiggestContour = i;
        }
    }
    return indexOfBiggestContour;
}
int main()
{
    Mat src = imread("largest_contour.jpg"); //Load largest contour image
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    Mat drawing = Mat::zeros(src.size(), CV_8UC1);
    int indexOfContour = findBiggestContour(contours);
    if (indexOfContour > -1)
    {

        vector<Point> ROI;
        ROI = contours[indexOfContour];

        //calculate moments of ROI
        Moments M;
        M = moments(ROI);

        Point COG; //center of gravity 
        COG = Point(M.m10 / M.m00, M.m01 / M.m00); //find the COG of ROI using equation
        Point center(drawing.cols / 2, drawing.rows / 2);
        Mat alignedImage = Mat::zeros(src.size(), CV_8UC1); //align the contour

        circle(drawing, COG, 2, Scalar(125, 100, 0), 3, 8);
        circle(drawing, center, 2, Scalar(125, 100, 0), 3, 8);

        int x;
        if (COG.x > center.x)
        {
            x = COG.x - center.x;
            x = -x;
        }
        else
        {
            x = (COG.x - center.x)*-1;
        }

        int y;

        if (COG.y < center.y)
        {
            y = center.y - COG.y;
        }
        else
        {
            y = center.y - COG.y;
        }

        alignContour(contours[indexOfContour], x, y);


        drawContours(alignedImage, contours, indexOfContour, Scalar(255), -1, 8, hierarchy);

}

How to do alignment of image?

I need to do the following :

1.Calculate moments of the ROI 2. Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00} 3. find the COG of the image and translate the ROI to the COG of the imageROI

I'm unable to find the COG and align the contour.