How to determine the size of a Point2f variable? - c++

parallel_for_(Range(0,npoints),LKTrackerInvoker(prevPyr[level*lvlstep1],deriveI,nextPyr[level*lvlstep2],prevPts,nextPts,status,err,winsize,criteria,level,maxlevel,flags(float)minEigThreshold);
Above is a function in OpenCV, which is used to calculate optical flow for previous pyramid images(const Mat& prevPyr) and next pyramid images(Mat& nextPyr).
Now I want to process nextPts(Point2f* nextPts), I need the size of nextPts.
I tried to look up OpenCV documentation but could not find any member function to get the size, Point2f cannot be converted to Mat, either. So, how can I get the size for a Point type variable and when should I use Point2f instead of Mat or InputArray, I'm not clear what the advantages of class Point are.
Any relevant reply would be appreciate a lot.

Related

Search contours on the image

I'm trying solve the recognition problem with a help OpenCV library for C++.
I have a some text(below) and i want to separate each symbol in this text using by cvFindContours(...) function. After, I want to send each separated symbol on the input of neural network for recognition it. It's all ok. I will can get all contours in my image and i can drawn it on my image with a help cvDrawContours(...) function(below). But cvFindContours(...) returns unordered sequence(pointer on the first contour in this sequence) where contains all the found contours. For my task order is very important.
CVAPI(int) cvFindContours( CvArr* image, CvMemStorage* storage, CvSeq** first_contour,
int header_size CV_DEFAULT(sizeof(CvContour)),
int mode CV_DEFAULT(CV_RETR_LIST),
int method CV_DEFAULT(CV_CHAIN_APPROX_SIMPLE),
CvPoint offset CV_DEFAULT(cvPoint(0,0)));
-image- source image
-storage- for storing where contains contours
-first_contour- pointer to the first contour in the storage
-mode- mode of search (I use the CV_RETR_EXTERNAL for search external contours)
-method- method of approximation (I'm using the CV_CHAIN_APPROX_SIMPLE by default)
How can I make the cvFindContours(...) function that returns the contours in the order in which they in the picture? Is it possible?
Thanks!
You can't directly force findContours to yield contours in a certain order (I mean there is no parameter to tune this in the function call).
To sort your contours in a "read text" order, you could do a loop which goes through all your contours and retrieves for each contour the top-leftest point, either by directly going through all points in each contour object, or by using a boundingbox (see minAreaRect for example).
Once you have all these points, sort them from left to right and bottom to top (some adjustments will probably have to be made, like detecting all contours starting within a range of heights to be all part of the same text line)
You have found bounding rectangles for all the contours present in your image. Instead of going about with the left-most point approach, you can sort your contours based on the centroid of each contour, which is more robust since your approach is being for text.
THIS ANSWER from the OpenCV community might help provide a start

Can I create a transformation matrix from rotation/translation vectors?

I'm trying to deskew an image that has an element of known size. Given this image:
I can use aruco:: estimatePoseBoard which returns rotation and translation vectors. Is there a way to use that information to deskew everything that's in the same plane as the marker board? (Unfortunately my linear algebra is rudimentary at best.)
Clarification
I know how to deskew the marker board. What I want to be able to do is deskew the other things (in this case, the cloud-shaped object) in the same plane as the marker board. I'm trying to determine whether or not that's possible and, if so, how to do it. I can already put four markers around the object I want to deskew and use the detected corners as input to getPerspectiveTransform along with the known distance between them. But for our real-world application it may be difficult for the user to place markers exactly. It would be much easier if they could place a single marker board in the frame and have the software deskew the other objects.
Since you tagged OpenCV:
From the image I can see that you have detected the corners of all the black box. So just get the most border for points in a way or another:
Then it is like this:
std::vector<cv::Point2f> src_points={/*Fill your 4 corners here*/};
std::vector<cv::Point2f> dst_points={cv:Point2f(0,0), cv::Point2f(width,0), cv::Point2f(width,height),cv::Point2f(0,height)};
auto H=v::getPerspectiveTransform(src_points,dst_points);
cv::Mat copped_image;
cv::warpPerspective(full_image,copped_image,H,cv::Size(width,height));
I was stuck on the assumption that the destination points in the call to getPerspectiveTransform had to be the corners of the output image (as they are in Humam's suggestion). Once it dawned on me that the destination points could be somewhere within the output image I had my answer.
float boardX = 1240;
float boardY = 1570;
float boardWidth = 1730;
float boardHeight = 1400;
vector<Point2f> destinationCorners;
destinationCorners(Point2f(boardX+boardWidth, boardY));
destinationCorners(Point2f(boardX+boardWidth, boardY+boardHeight));
destinationCorners(Point2f(boardX, boardY+boardHeight));
destinationCorners(Point2f(boardX, boardY));
Mat h = getPerspectiveTransform(detectedCorners, destinationCorners);
Mat bigImage(image.size() * 3, image.type(), Scalar(0, 50, 50));
warpPerspective(image, bigImage, h, bigImage.size());
This fixed the perspective of the board and everything in its plane. (The waviness of the board is due to the fact that the paper wasn't lying flat in the original photo.)

How to convert CImg float image type to unchar image(0-255) type

I am using CImg library and implementing non maximum suppression for Harris Corner Detector. Because calculation of numbers like determinants requires float type image, I declared images as float type. But when exracting local maximum on the harris response float image, I found it hard to set the threshold because I don't know what exactly the values of the pixels on a float image are, and I got some very strange points extracted which actually weren't the ones I wanted. For example I printed out the values of the points, then I found pixel values like 6e+9 or 1.8e+5, and I don't know what they mean. Is there a way to convert a float image into unchar image so that I can set an integer threshold for local maximum extraction? Thanks in advance!
You just have to normalize your image values, like this :
CImg<unsigned char> img_normalized = img.get_normalize(0,255);
then work on the values of 'img_normalized' instead.

warpPerspective

I tried to get the inverse perspective to get a frame captured in real-time to the camera plane using the following code:
Mat dst;
dst=dst.zeros(frame.cols,frame.rows,frame.type());
if(Found){
Mat mmat;
mmat.create(3,3,CV_32FC1);
mmat=getPerspectiveTransform(templPoints,imgPoints);
cout<< mmat<<endl<<endl;
warpPerspective(frame,dst,Homo,dst.size(),INTER_LINEAR );
imshow("out",dst);
}
the problem is that the dst image is totally black , what's wrong with my code?
The image you are seeing is usually the result of sending the source points into getPerspectiveTransform in the wrong order. This means that the points are crossing each other and triangular shapes will appear. Check the order of the points and make sure they match the order of the destination points.
You need to provide some more details.
Why are you calling both findHomography and getPerspectiveTransform? Since you are calling both, I assume that both templPoints and imgPoints are of size 4, in which case the call to findHomography is redundant (and RANSAC does nothing at all for you).
Have you looked (e.g. using matlab or octave, or by hand) at the values of mmat * templPoints? They should be equal to imgPoints, and all inside the [0, dst.width] x [0, dst.height]

OpenCV Transform using Chessboard

I have only just started experimenting with OpenCV a little bit. I have a setup of an LCD with a static position, and I'd like to extract what is being displayed on the screen from the image. I've seen the chessboard pattern used for calibrating a camera, but it seems like that is used to undistort the image, which isn't totally what I want to do.
I was thinking I'd display the chessboard on the LCD and then figure out the transformations needed to convert the image of the LCD into the ideal view of the chessboard directly overhead and cropped. Then I would store the transformations, change what the LCD is displaying, take a picture, perform the same transformations, and get the ideal view of what was now being displayed.
I'm wondering if that sounds like a good idea? Is there a simpler way to achieve what I'm trying to do? And any tips on the functions I should be using to figure out the transformations, perform them, store them (maybe just keep the transform matrices in memory or write them to file), etc?
I'm not sure I understood correctly everything you are trying to do, but bear with me.
Some cameras have lenses that cause a little distortion to the image, and for this purpose OpenCV offers methods to aid in the camera calibration process.
Practically speaking, if you want to write an application that will automatically correct the distortion in the image, first, you need to discover what are the magical values that need to be used to undo this effect. These values come from a proper calibration procedure.
The chessboard image is used together with an application to calibrate the camera. So, after you have an image of the chessboard taken by the camera device, pass this image to the calibration app. The app will identify the corners of the squares and compute the values of the distortion and return the magical values you need to use to counter the distortion effect. At this point, you are interested in 2 variables returned by calibrateCamera(): they are cameraMatrix and distCoeffs. Print them, and write the data on a piece of paper.
At the end, the system you are developing needs to have a function/method to undistort the image, where these 2 variables will be hard coded inside the function, followed by a call to cv::undistort() (if you are using the C++ API of OpenCV):
cv::Mat undistorted;
cv::undistort(image, undistorted, cameraMatrix, distCoeffs);
and that's it.
Detecting rotation automatically might be a bit tricky, but the first thing to do is find the coordinates of the object you are interested in. But if the camera is in a fixed position, this is going to be easy.
For more info on perspective change and rotation with OpenCV, I suggest taking a look at these other questions:
Executing cv::warpPerspective for a fake deskewing on a set of cv::Point
Affine Transform, Simple Rotation and Scaling or something else entirely?
Rotate cv::Mat using cv::warpAffine offsets destination image
findhomography() is not bad choice, but skew,distortion(camera lens) is real problem..
C++: Mat findHomography(InputArray srcPoints, InputArray dstPoints,
int method=0, double ransacReprojThreshold=3, OutputArray
mask=noArray() )
Python: cv2.findHomography(srcPoints, dstPoints[, method[,
ransacReprojThreshold[, mask]]]) → retval, mask
C: void cvFindHomography(const CvMat* srcPoints, const CvMat*
dstPoints, CvMat* H, int method=0, double ransacReprojThreshold=3,
CvMat* status=NULL)
http://opencv.itseez.com/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#findhomography