# Revision history [back]

### 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>

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>

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);


}

 4 No.4 Revision Tetragramm 7058 ●11 ●35

### 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 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);
}
} 


 5 No.5 Revision updated 2016-12-17 11:53:53 -0500 Tetragramm 7058 ●11 ●35 How to do alignment of image? I need to do the following : 1.Load : Load largest_contour image 2. image Calculate moments of the ROI 3. ROI Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00} 4. M01/M00} Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image 5. image Save the aligned image temporarily 6. temporarily 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); } 6 No.6 Revision updated 2016-12-19 07:39:38 -0500 How to do alignment of image? I need to do the following : Load largest_contour image Calculate moments of the ROI Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00} Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image Save the aligned image temporarily 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); } 7 No.7 Revision updated 2016-12-19 07:43:31 -0500 How to do alignment of image? I need to do the following : Load largest_contour image Calculate moments of the ROI Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00} Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image Save the aligned image temporarily 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); } 8 No.8 Revision updated 2016-12-19 07:57:07 -0500 How to do alignment of image? I need to do the following : Load largest_contour image Calculate moments of the ROI Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00} Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image Save the aligned image temporarily 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); } 9 No.9 Revision updated 2016-12-19 08:01:56 -0500 berak 26924 ●4 ●73 ●282 How to do alignment of image? I need to do the following : Load largest_contour image Calculate moments of the ROI Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00} Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image Save the aligned image temporarily 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); } } 10 No.10 Revision updated 2016-12-19 08:06:37 -0500 How to do alignment of image? I need to do the following : Load largest_contour image Calculate moments of the ROI Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00} Align the contour ,that is ,to find the COG of the image and translate the ROI to the COG of the image Save the aligned image temporarily 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); } 11 No.11 Revision updated 2017-01-07 07:38:54 -0500 How to do alignment of image? I need to do the following : Load largest_contour image Calculate 1.Calculate moments of the ROI ROI 2. Find the COG of the ROI using equation :{x,y} = {M10/M00, M01/M00} 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 Save the aligned image temporarily 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); } 12 No.12 Revision updated 2017-01-21 13:15:00 -0500 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); } 13 No.13 Revision updated 2017-01-22 02:12:18 -0500 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. 


 Copyright OpenCV foundation, 2012-2018. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license. about | faq | help | privacy policy | terms of service Powered by Askbot version 0.10.2 Please note: OpenCV answers requires javascript to work properly, please enable javascript in your browser, here is how //IE fix to hide the red margin var noscript = document.getElementsByTagName('noscript')[0]; noscript.style.padding = '0px'; noscript.style.backgroundColor = 'transparent'; askbot['urls']['mark_read_message'] = '/s/messages/markread/'; askbot['urls']['get_tags_by_wildcard'] = '/s/get-tags-by-wildcard/'; askbot['urls']['get_tag_list'] = '/s/get-tag-list/'; askbot['urls']['follow_user'] = '/followit/follow/user/{{userId}}/'; askbot['urls']['unfollow_user'] = '/followit/unfollow/user/{{userId}}/'; askbot['urls']['user_signin'] = '/account/signin/'; askbot['urls']['getEditor'] = '/s/get-editor/'; askbot['urls']['apiGetQuestions'] = '/s/api/get_questions/'; askbot['urls']['ask'] = '/questions/ask/'; askbot['urls']['questions'] = '/questions/'; askbot['settings']['groupsEnabled'] = false; askbot['settings']['static_url'] = '/m/'; askbot['settings']['minSearchWordLength'] = 4; askbot['settings']['mathjaxEnabled'] = false; askbot['settings']['sharingSuffixText'] = ''; askbot['settings']['errorPlacement'] = 'after-label'; askbot['data']['maxCommentLength'] = 800; askbot['settings']['editorType'] = 'markdown'; askbot['settings']['commentsEditorType'] = 'rich\u002Dtext'; askbot['messages']['askYourQuestion'] = 'Ask Your Question'; askbot['messages']['questionSingular'] = 'question'; askbot['messages']['answerSingular'] = 'answer'; askbot['messages']['acceptOwnAnswer'] = 'accept or unaccept your own answer'; askbot['messages']['followQuestions'] = 'follow questions'; askbot['settings']['allowedUploadFileTypes'] = [ "jpg", "jpeg", "gif", "bmp", "png", "tiff" ]; askbot['data']['haveFlashNotifications'] = true; askbot['data']['activeTab'] = 'questions'; askbot['settings']['csrfCookieName'] = 'csrftoken'; askbot['data']['searchUrl'] = ''; /*<![CDATA[*/ $('.mceStatusbar').remove();//a hack to remove the tinyMCE status bar$(document).ready(function(){ // focus input on the search bar endcomment var activeTab = askbot['data']['activeTab']; if (inArray(activeTab, ['users', 'questions', 'tags', 'badges'])) { var searchInput = $('#keywords'); } else if (activeTab === 'ask') { var searchInput =$('#id_title'); } else { var searchInput = undefined; animateHashes(); } if (searchInput) { searchInput.focus(); putCursorAtEnd(searchInput); } var haveFullTextSearchTab = inArray(activeTab, ['questions', 'badges', 'ask']); var haveUserProfilePage = $('body').hasClass('user-profile-page'); if ((haveUserProfilePage || haveFullTextSearchTab) && searchInput && searchInput.length) { var search = new FullTextSearch(); askbot['controllers'] = askbot['controllers'] || {}; askbot['controllers']['fullTextSearch'] = search; search.setSearchUrl(askbot['data']['searchUrl']); if (activeTab === 'ask') { search.setAskButtonEnabled(false); } search.decorate(searchInput); } else if (activeTab === 'tags') { var search = new TagSearch(); search.decorate(searchInput); } if (askbot['data']['userIsAdminOrMod']) {$('body').addClass('admin'); } if (askbot['settings']['groupsEnabled']) { askbot['urls']['add_group'] = "/s/add-group/"; var group_dropdown = new GroupDropdown(); $('.groups-dropdown').append(group_dropdown.getElement()); } var userRep =$('#userToolsNav .reputation'); if (userRep.length) { var showPermsTrigger = new ShowPermsTrigger(); showPermsTrigger.decorate(userRep); } }); if (askbot['data']['haveFlashNotifications']) { $('#validate_email_alert').click(function(){notify.close(true)}) notify.show(); } var langNav =$('.lang-nav'); if (langNav.length) { var nav = new LangNav(); nav.decorate(langNav); } /*]]>*/ //todo - take this out into .js file $(document).ready(function(){$('div.revision div[id^=rev-header-]').bind('click', function(){ var revId = this.id.substr(11); toggleRev(revId); }); lanai.highlightSyntax(); }); function toggleRev(id) { var arrow = $("#rev-arrow-" + id); var visible = arrow.attr("src").indexOf("hide") > -1; if (visible) { var image_path = '/m/default/media/images/expander-arrow-show.gif?v=6'; } else { var image_path = '/m/default/media/images/expander-arrow-hide.gif?v=6'; } image_path = image_path + "?v=6"; arrow.attr("src", image_path);$("#rev-body-" + id).slideToggle("fast"); } for (url_name in askbot['urls']){ askbot['urls'][url_name] = cleanUrl(askbot['urls'][url_name]); }