Normal and tangent to curve in openCV - opencv

Is there a robust method for finding the tangent and the normal to a drawn curve in openCV? In the image, I drawed some normal vectors to the curve. The input is just some curves drawed.

Related

PCA for ellipse fitting

I have a depth video stream (grayscale) of a pen and pen-like objects. Since they are mostly of an ellipse shape, I want to fit an ellipse using PCA and get real-time output of ellipse angles.
The final goal is to estimate object(ellipse) orientation with respect x-y coordinate system.
Here are the image samples:
Depth Image
I have read this paper on using PCA for ellipse fitting but I am not sure how to apply it.

How can I project a rectangular texture into a region of curved shape?

I want to map a texture image of rectangular shape into a curved area. The curved area has a axis defined by bezier curve and fixed width.
I can map the points on the axis to the texture by percentile, and get a stripe of pixels to fill the region. But this way the left side of the region is "stretched", and I get unfilled gaps.
How can I map the texture to the curved area "smoothly"? Is there an algorithm for such a task?
To answer my own question:
My own naive solution is to fill the gaps(trianglar area in the image) with pixel values by interpolating between the adjacent points on normal vectors.
Later I found a more mathematical solution to this problem in a paper:
http://www.stat.ucla.edu/~sczhu/papers/Conf_2011/portrait-painting-preprint.pdf
It map the grids of the rectanglar texture to the spline-shaped area with a method called thin-plate spline (TPS) transformation:
we compute a thin-plate spline (TPS) transformation [Barrodale
et al. 1993] between the pairs of source and target dot positions
(e.g., between the corresponding backbone control points in Figs.4a
and 4b), and apply the transformation to the vertices of a quadrilateral
mesh covering the source brush stroke to get the deformed
mesh. Finally, we compute a texture mapping using the mesh, with
a bilinear interpolation inside each quadrilateral.
I am thinking maybe the same transformation can be done with bezier curves.
Hope this is helpful.

OpenCV circle distortion detection

OpenCV has capapabilities to compensate for distortion in patterns, such as a this board, for example:
Every example I ever saw for this process does it with grids or squares. I would like to know if something similar exists for a single circle. My practical case is that I detect an ellipse, and I need to calculate the angle between the plane of this ellipse and the projection plane where the ellipse is projected as a circle. I managed to achieve that in my own code, but I would like to know if there is something built into the library to that purpose.
Use the ellipse axes to your advantage
I don't know of any "circular projection" as you name it, but I'm thinking that you can rephrase your problem into having the solution already.
Images make any answer SO cool.
Forget the ellipse, take the axes
A circle can be thought of as 2 vectors with unit norm defining a plane.
The projected circle's axes you estimate are the projection of the unit referential into the 3D plane
Then for projecting back and forth is just an affair of applying the transformation described by the estimated axes vectors

Getting corners from convex points

I have written algorithm to extract the points shown in the image. They form convex shape and I know order of them. How do I extract corners (top 3 and bottom 3) from such points?
I'm using opencv.
if you already have the convex hull of the object, and that hull includes the corner points, then all you need to to do is simplify the hull until it only has 6 points.
There are many ways to simplify polygons, for example you could just use this simple algorithm used in this answer: How to find corner coordinates of a rectangle in an image
do
for each point P on the convex hull:
measure its distance to the line AB _
between the point A before P and the point B after P,
remove the point with the smallest distance
repeat until 6 points left
If you do not know the exact number of points, then you could remove points until the minimum distance rises above a certain threshold
you could also do Ramer-Douglas-Peucker to simplify the polygon, openCV already has that implemented in cv::approxPolyDP.
Just modify the openCV squares sample to use 6 points instead of 4
Instead of trying to directly determine which of your feature points correspond to corners, how about applying an corner detection algorithm on the entire image then looking for which of your feature points appear close to peaks in the corner detector?
I'd suggest starting with a Harris corner detector. The OpenCV implementation is cv::cornerHarris.
Essentially, the Harris algorithm applies both a horizontal and a vertical Sobel filter to the image (or some other approximation of the partial derivatives of the image in the x and y directions).
It then constructs a 2 by 2 structure matrix at each image pixel, looks at the eigenvalues of that matrix, and calls points corners if both eigenvalues are above some threshold.

Contours matching - finding contours displacement

I found contours on two images with same object and I want to find displacement and rotation of this object. I've tried with rotated bounding boxes of this contours and then its angles and center points but rotations of bounding boxes don't tell about contour rotation correctly because it's the same for angles a+0, a+90, a+180 etc. degrees.
Is it any other good way to find rotation and displacement of contours? Maybe some use of convex hull, convexity defects? I've read in Learning OpenCv about matching contours but it hasn't helped. Could someone give some example?
//edit:
Maybe there is some way to use something similar to freeman chains to this? But I can't figure out algorithm at the moment. Making chain with angles between sequence point and then checking sequence match isn't working good...
If the object has convexity defects then you could choose one defect, make a vector from the centroid of the first contour to the centroid of this defect.
Then you could check the defects in the second contour and match the one that you used before.Again a vector from the centroid of the contour to the centroid of the matched defect.
From this you get 2 segments (vectors) from which you could obtain a displacement and a rotation.

Resources