Ask Your Question

convexHull clockwise parameter inverted ?

asked 2016-10-14 06:53:34 -0500

HDM gravatar image

Perhaps I'm misunderstanding the 3rd parameter to convexHull ? :

clockwise - Orientation flag. If it is true, the output convex hull is oriented clockwise. Otherwise, it is oriented counter-clockwise. The assumed coordinate system has its X axis pointing to the right, and its Y axis pointing upwards.

When I run the following code:

#include <iostream>
#include <vector>

#include <opencv2/core/base.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

int main(int argc, char **argv)
   std::vector<cv::Point2i> points, hull_points;
   points.push_back(cv::Point2i(114, 22));
   points.push_back(cv::Point2i(329, 245));
   points.push_back(cv::Point2i(37, 98));
   points.push_back(cv::Point2i(43, 272));
   points.push_back(cv::Point2i(120, 150));
   cv::convexHull(points, hull_points, false);
   cv::Mat img(350, 460, CV_8UC3, cv::Scalar::all(0));   
   int no = 1;
   for (auto it = hull_points.begin(); it != hull_points.end(); ++it)
      cv::Point2i &pt = *it;
      cv::circle(img, pt, 3, cv::Scalar(0, 255, 0), 1, cv::LINE_AA);      
      std::stringstream ss;
      ss << no++ << ":(" << pt.x << "," << pt.y << ")";
      cv::putText(img, ss.str(), cv::Point2i(pt.x+1, pt.y-1), CV_FONT_HERSHEY_COMPLEX_SMALL, 1.0, cvScalar(0, 255, 255), 1, CV_AA);
      std::cout << ss.str() << " ";
   std::cout << std::endl;
   cv::imwrite("img.png", img);      

I get the following image:

image description

which appears to be clockwise unless its supposed to be traversed in descending order.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-10-14 07:16:49 -0500

updated 2016-10-14 07:44:33 -0500

Ah the good old issue of coordinate system orientation.

  • clockwise means moving from x axis towards the y axis if you consider a upstraight coordinate system, so Y running from bottom to top.
  • however OpenCV uses the top left corner as origin and has a downwards pointing y axis.
  • this means to achieve this you need to warp the movement over the x axis.
  • and thus your rotation is actually clockwise in the OpenCV coordinate system.

I know it is sometimes a bit sketchy because coordinate systems are used in different constellations inside OpenCV, but now you know why you get this result.


image description

edit flag offensive delete link more


However the documkentation explicitly states:

The assumed coordinate system has its X axis pointing to the right, and its Y axis pointing upwards.

HDM gravatar imageHDM ( 2016-10-14 07:21:33 -0500 )edit

YES for defining the concept of counter clockwise and clockwise. Probably the explanation should be more precise.

StevenPuttemans gravatar imageStevenPuttemans ( 2016-10-14 07:33:33 -0500 )edit

Yes, I assumed they meant the coordinate system used for the hull.

Thanks for the reply.

HDM gravatar imageHDM ( 2016-10-14 07:35:28 -0500 )edit

You can use this graphics too

LBerger gravatar imageLBerger ( 2016-10-14 11:57:29 -0500 )edit

Question Tools

1 follower


Asked: 2016-10-14 06:53:34 -0500

Seen: 358 times

Last updated: Oct 14 '16