# Inscribed and circumscribed circle of a contour

**URL:** <https://forum.opencv.org/t/inscribed-and-circumscribed-circle-of-a-contour/11945>\
**Category:** Uncategorized\
**Created:** [February 11, 2023, 3:44pm UTC](https://forum.opencv.org/t/inscribed-and-circumscribed-circle-of-a-contour/11945 "2023-02-11T15:44:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sylvain\_Ard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sylvain_ard/32/114_2.png) [@Sylvain\_Ard](https://forum.opencv.org/u/Sylvain_Ard)\
**Post date:** [February 11, 2023, 3:44pm UTC](https://forum.opencv.org/t/inscribed-and-circumscribed-circle-of-a-contour/11945/1 "2023-02-11T15:44:39Z")

</div>

Hello,  
I want to calculate the inscribed and circumscribed circle of a contour. The contour is those of this image of plant stem section :  
 ![leaflet.jpg_8](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/b/b72c9de81cdce3c92bdb67153baa6065b90f2d63.jpeg)

by circumscribed circle I mean smallest circle outside the contour and by inscribed circle I mean largest circle inside the contour  
Thank you  
Bets regards

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [February 12, 2023, 9:53am UTC](https://forum.opencv.org/t/inscribed-and-circumscribed-circle-of-a-contour/11945/2 "2023-02-12T09:53:09Z")

</div>

the outer one is [minEnclosingCircle()](https://docs.opencv.org/4.x/d3/dc0/group __imgproc__ shape.html#ga8ce13c24081bbc7151e9326f412190f1) , for the inner one you might try to [fit a circle](https://docs.opencv.org/4.x/d3/dc0/group __imgproc__ shape.html#gaf259efaad93098103d6c27b9e4900ffa) to the convexity defect points

---

<div class="post-metadata">

**Author:** ![Sylvain\_Ard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sylvain_ard/32/114_2.png) [@Sylvain\_Ard](https://forum.opencv.org/u/Sylvain_Ard)\
**Post date:** [February 12, 2023, 10:57am UTC](https://forum.opencv.org/t/inscribed-and-circumscribed-circle-of-a-contour/11945/3 "2023-02-12T10:57:14Z")

</div>

thank you very much !

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 12, 2023, 12:17pm UTC](https://forum.opencv.org/t/inscribed-and-circumscribed-circle-of-a-contour/11945/4 "2023-02-12T12:17:36Z")

</div>

inner: peak of distance transform

I’d also just go with circle fit through convexity detects. cheaper to compute and should be mathematically the same or within tolerance of noise.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Ard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sylvain_ard/32/114_2.png) [@Sylvain\_Ard](https://forum.opencv.org/u/Sylvain_Ard)\
**Post date:** [February 12, 2023, 12:30pm UTC](https://forum.opencv.org/t/inscribed-and-circumscribed-circle-of-a-contour/11945/5 "2023-02-12T12:30:22Z")

</div>

I don’t understand, for circumscribed circle I succeeded but for inscribed circle not, and I don’t think convexity detects. could works

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 12, 2023, 4:52pm UTC](https://forum.opencv.org/t/inscribed-and-circumscribed-circle-of-a-contour/11945/6 "2023-02-12T16:52:05Z")

</div>

then use the peak of the distance transform

---

<div class="post-metadata">

**Author:** ![Sylvain\_Ard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sylvain_ard/32/114_2.png) [@Sylvain\_Ard](https://forum.opencv.org/u/Sylvain_Ard)\
**Post date:** [February 12, 2023, 5:13pm UTC](https://forum.opencv.org/t/inscribed-and-circumscribed-circle-of-a-contour/11945/7 "2023-02-12T17:13:18Z")

</div>

I don’t know what is it, have you a piece of code or tutorial on this please ?

---

<div class="post-metadata">

**Author:** ![Sylvain\_Ard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sylvain_ard/32/114_2.png) [@Sylvain\_Ard](https://forum.opencv.org/u/Sylvain_Ard)\
**Post date:** [February 12, 2023, 8:30pm UTC](https://forum.opencv.org/t/inscribed-and-circumscribed-circle-of-a-contour/11945/8 "2023-02-12T20:30:02Z")

</div>

I finally succeeded by this code :  
Circle cercleInscrit(std::vectorcv::Point contour,Size imgSize) {  
Mat dist\_map;  
double\* minVal=new double;  
double\* maxVal=new double;  
Point\* minLoc=new Point;  
Point\* maxLoc=new Point;  
//création d’une image binaire avec le contour rempli de blanc  
Mat mask = Mat::zeros(imgSize, CV\_8UC1);  
//remplissage  
vector\<vector\> polygones;  
polygones.push\_back(contour);  
fillPoly(mask, polygones, Scalar(255));  
//imwrite(“mask.jpg”, mask);  
distanceTransform(mask, dist\_map, DIST\_L2, DIST\_MASK\_PRECISE);  
minMaxLoc(dist\_map, minVal, maxVal, minLoc, maxLoc);  
Circle res(\*maxVal, \*maxLoc);  
free(maxVal);  
free(maxLoc);  
free(minVal);  
free(minLoc);  
return res;  
}

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 17, 2023, 11:25am UTC](https://forum.opencv.org/t/inscribed-and-circumscribed-circle-of-a-contour/11945/9 "2023-02-17T11:25:30Z")

</div>

Split this topic on February 17, 2023, 11:25am UTC

2 posts were split to a new topic: [Can not create contours using numpy array #23230](https://forum.opencv.org/t/can-not-create-contours-using-numpy-array-23230/12023)
