Ask Your Question
0

How to get the corners of rectangle shapes?

asked 2015-09-20 09:36:21 -0600

Robot_Lee gravatar image

image description Recently I received a task. Getting the distance and the angle of rotain between camera and object. I think I can put a rectangle card which is easily to be find on it and caculate the information. However,I have difficulty in it, what should I do?

edit retag flag offensive close merge delete

Comments

1

May be you can use approxPolyDP

LBerger gravatar imageLBerger ( 2015-09-20 09:52:18 -0600 )edit

Thankyou,but i failed to find the corners using this function,maybe the way i used is wrong.what should i do?

Robot_Lee gravatar imageRobot_Lee ( 2015-09-20 10:07:56 -0600 )edit

take a look squares.cpp

sturkmen gravatar imagesturkmen ( 2015-09-20 10:11:13 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-09-20 11:19:29 -0600

LBerger gravatar image

I have got this image : image description

with this program :

#include <opencv2/opencv.hpp> 
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

int main(int argc, char **argv)
{
Mat mThresh;
Mat m=imread("C:/Users/Laurent.PC-LAURENT-VISI/Downloads/rectangle.png",CV_LOAD_IMAGE_GRAYSCALE);
Mat mc;
threshold(m,mThresh,80,255,THRESH_BINARY);
findContours(mThresh,contours,hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_NONE, cv::Point(0,0));
imshow("Image",m);
mc = Mat::zeros(m.size(),CV_8UC3);
drawContours(mc,contours,0,Scalar(255,0,0),1);
vector<Point> approx;
double d=0;
do
{
    d=d+1;
    approxPolyDP(contours[0],approx,d,true);
    cout << approx.size() << " " <<d<<endl;
}
while (approx.size()>4);
contours.push_back(approx);
drawContours(mc,contours,contours.size()-1,Scalar(0,0,255),1);
imshow("Ctr",mc);
waitKey();

return 0;
}

result is

image description

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-09-20 09:36:21 -0600

Seen: 9,932 times

Last updated: Sep 20 '15