First time here? Check out the FAQ!

Ask Your Question
0

How to get the corners of rectangle shapes?

asked Sep 20 '15

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?

Preview: (hide)

Comments

1

May be you can use approxPolyDP

LBerger gravatar imageLBerger (Sep 20 '15)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 (Sep 20 '15)edit

take a look squares.cpp

sturkmen gravatar imagesturkmen (Sep 20 '15)edit

1 answer

Sort by » oldest newest most voted
3

answered Sep 20 '15

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

Preview: (hide)

Question Tools

1 follower

Stats

Asked: Sep 20 '15

Seen: 10,002 times

Last updated: Sep 20 '15