Quantcast
Channel: OpenCV Q&A Forum - Latest question feed
Viewing all articles
Browse latest Browse all 600

How to centralise the contour of the object?

$
0
0
I have been able to find its COG, but how to centralise the contour of the hand? Here is my code: #include "opencv2\opencv.hpp" using namespace cv; int main() { Mat gray = imread("largest_contour.jpg", IMREAD_GRAYSCALE); Moments mu = moments(gray, true); //Point center; Point COG;//center of gravity //COG = Point(center.x, center.y); COG = Point(mu.m10 / mu.m00, mu.m01 / mu.m00); //find the COG of ROI Mat drawing = Mat::zeros(gray.size(), CV_8UC1); Point center(drawing.cols / 2, drawing.rows / 2); Mat res; cvtColor(gray, res, CV_GRAY2BGR); Mat alignedImage = Mat::zeros(gray.size(), CV_8UC1); //align the contour Mat roi = gray(Rect(150, 50, 150, 250)); circle(res, center, 2, Scalar(0, 0, 255)); circle(res, COG, 2, Scalar(0, 0, 255)); //Translate the ROI to the COG of the image 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; } imshow("COG", res); imwrite("AlignedImage.jpg",res); waitKey(); return 0; }

Viewing all articles
Browse latest Browse all 600

Trending Articles