Ask Your Question

Revision history [back]

OpenCV has a useful tool called PCA (principal component analysis) https://robospace.wordpress.com/2013/10/09/object-orientation-principal-component-analysis-opencv/ Here is how I used it:

  • resized the image by 1/4 as we don't really need such high resolution here
  • extract the red channel so that the blue part of the pen is darker than the surroundings
  • apply a threshold using binary_inverse with treshold value about 90
  • do some morphology to clean up the threshold image: one iteration of erosion with 3x3 rect and 5 iterations of dilation with 3x3 rect
  • do some contour filtering to only include the contour whose center is nearest the center of the image
  • now do PCA on this remaining contour to give 2 eigenvectors that tell us the orientation

From here, you can apply some test to tell if it is in some desired orientation.

image description