# Contour Approximation OpenCV

I am using opencv for python. For a bad shape, we use approxpolyDP().For this, I created a bad rectangle(added to the post)

While using this, I only get 2 dots and not a proper rectangle.

Can anyone help me why this is happening?

import cv2
import numpy as np

img = im
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(img,100,200)

(_,cnts,_) = cv2.findContours(canny,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

cnt = cnts[0]

epsilon = 0.1*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)

cv2.drawContours(im,approx,-1,(0,255,0),3)

cv2.imshow("img",im)
cv2.waitKey(0)
cv2.destroyAllWindows()


This is how the output comes out as

This is my desired result

edit retag close merge delete

Sort by ยป oldest newest most voted

approxPolyDP can't do what you are looking for becase you have to find right epsilon that cuts just "defects", check the doc or Douglas-Peucker algorithm

You can use convexHull

vector<cv::Point> hull;
cv::convexHull(contours[i], hull);
//draw it
cv::polylines(im, hull, true, cv::Scalar(255, 0, 0));


or approx it a bit more

double len = arcLength(hull, true);
double epsilon = 0.02*len;
vector<cv::Point> approx;
cv::approxPolyDP(hull, approx, epsilon, true);
cv::polylines(im, approx, true, cv::Scalar(0, 255, 0));

cv::RotatedRect rr;
rr = cv::minAreaRect(contours[i]);
//draw the rectangle
cv::Point2f rect_points[4];
rr.points(rect_points);
for (int j = 0; j < 4; j++)
cv::line(im, rect_points[j], rect_points[(j + 1) % 4], cv::Scalar(0, 0, 255), 2, 8);


OR look at find square example: https://github.com/Itseez/opencv/blob...

more

I still dont get a line around the rectangle

import cv2
import numpy as np

img = im
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(img,127,255,cv2.THRESH_BINARY)

(_,cnts,_) = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

cnt = cnts[1]
hull = cv2.convexHull(cnt)
cv2.drawContours(im,hull,-1,(0,255,0),3)

cv2.imshow("img",im)
cv2.waitKey(0)
cv2.destroyAllWindows()


Here -> This is how it looks http://postimg.org/image/o9zuq40hl/23...

If you could help me, that would be great since i am stuck here for a very long time

( 2015-11-28 00:25:56 -0500 )edit
1

@arp1561_ans please you are using 2 answer for same issue. Use just one !

ConvexHull solve your problem check your code with suggested tutorial. Drawing Functions in OpenCV, Contours - 1: Getting Started , Contours - 2 : Brotherhood, Contours in OpenCV

( 2015-11-28 03:08:23 -0500 )edit

Official site

GitHub

Wiki

Documentation