OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Wed, 26 Jun 2019 04:35:55 -0500Draw lines from centroid of contour at given angle till edge of contourhttp://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/I have a contour named 'cnt' obtained from the image below:
![image description](/upfiles/14249482552397042.png)
for which I am able to find the centroid like this:
M = cv2.moments(cnt)
centroid_x = int(M['m10']/M['m00'])
centroid_y = int(M['m01']/M['m00'])
I now want to draw N number of lines, each 360/N degrees apart, starting from the centroid and passing through all possible points of intersection with the contour. The cv2.line() function requires start point and end point but I don't have the end point.
If I had drawn a line passing through centroid with a slope of Tan(360/N) I would have found the intersection of the line with the contour using bitwise_and but I'm not able to figure out a way to draw that line.
Any help would be much appreciated.
Tue, 24 Feb 2015 20:25:12 -0600http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/Answer by berak for <p>I have a contour named 'cnt' obtained from the image below:</p>
<p><img alt="image description" src="/upfiles/14249482552397042.png"/></p>
<p>for which I am able to find the centroid like this:</p>
<pre><code>M = cv2.moments(cnt)
centroid_x = int(M['m10']/M['m00'])
centroid_y = int(M['m01']/M['m00'])
</code></pre>
<p>I now want to draw N number of lines, each 360/N degrees apart, starting from the centroid and passing through all possible points of intersection with the contour. The cv2.line() function requires start point and end point but I don't have the end point.</p>
<p>If I had drawn a line passing through centroid with a slope of Tan(360/N) I would have found the intersection of the line with the contour using bitwise_and but I'm not able to figure out a way to draw that line. </p>
<p>Any help would be much appreciated. </p>
http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?answer=56254#post-id-56254it's a bit of a shame, that you can't access the LineIterator from python, as it saves you from finding the intersection point manually, you'd just walk on with your line, until you reach a border pixel (or the image bounds) :
Mat ocv = imread("k4eIlCQ.png");
Mat gray; cvtColor(ocv,gray,COLOR_BGR2GRAY);
threshold(gray,gray,100,255,0);
Moments M = moments(gray);
Point cen( int(M.m10/M.m00), int(M.m01/M.m00) );
for (int i=0; i<360; i+=20)
{
double s = sin(i*CV_PI/180);
double c = cos(i*CV_PI/180);
Point p2(cen.x+s*150, cen.y+c*150); // any radius will do, we just want the direction
LineIterator it(ocv, cen, p2, 8);
Rect bounds(0, 0, ocv.cols, ocv.rows);
while(bounds.contains(it.pos()))
{
Vec3b & pixel = ocv.at<Vec3b>(it.pos());
// if you stare really hard, you'll see the cheat ;)
if (pixel[0] > 50) // non dark(it's not really black in the image!)
pixel[1] = pixel[2] = 0; // set g and b to 0, leaves blue line
else
break;
it++;
}
}
imshow("lines", ocv);
![image description](/upfiles/14249508316138649.png)Thu, 26 Feb 2015 05:41:23 -0600http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?answer=56254#post-id-56254Comment by StevenPuttemans for <p>it's a bit of a shame, that you can't access the LineIterator from python, as it saves you from finding the intersection point manually, you'd just walk on with your line, until you reach a border pixel (or the image bounds) :</p>
<pre><code>Mat ocv = imread("k4eIlCQ.png");
Mat gray; cvtColor(ocv,gray,COLOR_BGR2GRAY);
threshold(gray,gray,100,255,0);
Moments M = moments(gray);
Point cen( int(M.m10/M.m00), int(M.m01/M.m00) );
for (int i=0; i<360; i+=20)
{
double s = sin(i*CV_PI/180);
double c = cos(i*CV_PI/180);
Point p2(cen.x+s*150, cen.y+c*150); // any radius will do, we just want the direction
LineIterator it(ocv, cen, p2, 8);
Rect bounds(0, 0, ocv.cols, ocv.rows);
while(bounds.contains(it.pos()))
{
Vec3b & pixel = ocv.at<Vec3b>(it.pos());
// if you stare really hard, you'll see the cheat ;)
if (pixel[0] > 50) // non dark(it's not really black in the image!)
pixel[1] = pixel[2] = 0; // set g and b to 0, leaves blue line
else
break;
it++;
}
}
imshow("lines", ocv);
</code></pre>
<p><img alt="image description" src="/upfiles/14249508316138649.png"/></p>
http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=214769#post-id-214769It stands for the cosinus and sinus signals.Wed, 26 Jun 2019 04:35:55 -0500http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=214769#post-id-214769Comment by adrianaioanei for <p>it's a bit of a shame, that you can't access the LineIterator from python, as it saves you from finding the intersection point manually, you'd just walk on with your line, until you reach a border pixel (or the image bounds) :</p>
<pre><code>Mat ocv = imread("k4eIlCQ.png");
Mat gray; cvtColor(ocv,gray,COLOR_BGR2GRAY);
threshold(gray,gray,100,255,0);
Moments M = moments(gray);
Point cen( int(M.m10/M.m00), int(M.m01/M.m00) );
for (int i=0; i<360; i+=20)
{
double s = sin(i*CV_PI/180);
double c = cos(i*CV_PI/180);
Point p2(cen.x+s*150, cen.y+c*150); // any radius will do, we just want the direction
LineIterator it(ocv, cen, p2, 8);
Rect bounds(0, 0, ocv.cols, ocv.rows);
while(bounds.contains(it.pos()))
{
Vec3b & pixel = ocv.at<Vec3b>(it.pos());
// if you stare really hard, you'll see the cheat ;)
if (pixel[0] > 50) // non dark(it's not really black in the image!)
pixel[1] = pixel[2] = 0; // set g and b to 0, leaves blue line
else
break;
it++;
}
}
imshow("lines", ocv);
</code></pre>
<p><img alt="image description" src="/upfiles/14249508316138649.png"/></p>
http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=213747#post-id-213747Hello. What does it means C and S variaibles?Fri, 31 May 2019 09:19:04 -0500http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=213747#post-id-213747Comment by sturkmen for <p>it's a bit of a shame, that you can't access the LineIterator from python, as it saves you from finding the intersection point manually, you'd just walk on with your line, until you reach a border pixel (or the image bounds) :</p>
<pre><code>Mat ocv = imread("k4eIlCQ.png");
Mat gray; cvtColor(ocv,gray,COLOR_BGR2GRAY);
threshold(gray,gray,100,255,0);
Moments M = moments(gray);
Point cen( int(M.m10/M.m00), int(M.m01/M.m00) );
for (int i=0; i<360; i+=20)
{
double s = sin(i*CV_PI/180);
double c = cos(i*CV_PI/180);
Point p2(cen.x+s*150, cen.y+c*150); // any radius will do, we just want the direction
LineIterator it(ocv, cen, p2, 8);
Rect bounds(0, 0, ocv.cols, ocv.rows);
while(bounds.contains(it.pos()))
{
Vec3b & pixel = ocv.at<Vec3b>(it.pos());
// if you stare really hard, you'll see the cheat ;)
if (pixel[0] > 50) // non dark(it's not really black in the image!)
pixel[1] = pixel[2] = 0; // set g and b to 0, leaves blue line
else
break;
it++;
}
}
imshow("lines", ocv);
</code></pre>
<p><img alt="image description" src="/upfiles/14249508316138649.png"/></p>
http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=69171#post-id-69171i think there must be a marker (maybe a new special tag like "Perfect_Solution" ) to mark all questions like this one. by this way newcomers easily find such good answers. @StevenPuttemans, @berak what is your opinion.Sat, 22 Aug 2015 03:26:45 -0500http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=69171#post-id-69171Comment by theodore for <p>it's a bit of a shame, that you can't access the LineIterator from python, as it saves you from finding the intersection point manually, you'd just walk on with your line, until you reach a border pixel (or the image bounds) :</p>
<pre><code>Mat ocv = imread("k4eIlCQ.png");
Mat gray; cvtColor(ocv,gray,COLOR_BGR2GRAY);
threshold(gray,gray,100,255,0);
Moments M = moments(gray);
Point cen( int(M.m10/M.m00), int(M.m01/M.m00) );
for (int i=0; i<360; i+=20)
{
double s = sin(i*CV_PI/180);
double c = cos(i*CV_PI/180);
Point p2(cen.x+s*150, cen.y+c*150); // any radius will do, we just want the direction
LineIterator it(ocv, cen, p2, 8);
Rect bounds(0, 0, ocv.cols, ocv.rows);
while(bounds.contains(it.pos()))
{
Vec3b & pixel = ocv.at<Vec3b>(it.pos());
// if you stare really hard, you'll see the cheat ;)
if (pixel[0] > 50) // non dark(it's not really black in the image!)
pixel[1] = pixel[2] = 0; // set g and b to 0, leaves blue line
else
break;
it++;
}
}
imshow("lines", ocv);
</code></pre>
<p><img alt="image description" src="/upfiles/14249508316138649.png"/></p>
http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=69241#post-id-69241actually regarding to what we were talking about making some changes in the form of the forum it would be nice answers with high voting rate to be marked as sticky in addition with some other threads like faq, rules, maybe job openings, etc...Sun, 23 Aug 2015 15:19:19 -0500http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=69241#post-id-69241Comment by theodore for <p>it's a bit of a shame, that you can't access the LineIterator from python, as it saves you from finding the intersection point manually, you'd just walk on with your line, until you reach a border pixel (or the image bounds) :</p>
<pre><code>Mat ocv = imread("k4eIlCQ.png");
Mat gray; cvtColor(ocv,gray,COLOR_BGR2GRAY);
threshold(gray,gray,100,255,0);
Moments M = moments(gray);
Point cen( int(M.m10/M.m00), int(M.m01/M.m00) );
for (int i=0; i<360; i+=20)
{
double s = sin(i*CV_PI/180);
double c = cos(i*CV_PI/180);
Point p2(cen.x+s*150, cen.y+c*150); // any radius will do, we just want the direction
LineIterator it(ocv, cen, p2, 8);
Rect bounds(0, 0, ocv.cols, ocv.rows);
while(bounds.contains(it.pos()))
{
Vec3b & pixel = ocv.at<Vec3b>(it.pos());
// if you stare really hard, you'll see the cheat ;)
if (pixel[0] > 50) // non dark(it's not really black in the image!)
pixel[1] = pixel[2] = 0; // set g and b to 0, leaves blue line
else
break;
it++;
}
}
imshow("lines", ocv);
</code></pre>
<p><img alt="image description" src="/upfiles/14249508316138649.png"/></p>
http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=56261#post-id-56261perfect!!!! I like it ;-).Thu, 26 Feb 2015 06:12:57 -0600http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=56261#post-id-56261Comment by StevenPuttemans for <p>it's a bit of a shame, that you can't access the LineIterator from python, as it saves you from finding the intersection point manually, you'd just walk on with your line, until you reach a border pixel (or the image bounds) :</p>
<pre><code>Mat ocv = imread("k4eIlCQ.png");
Mat gray; cvtColor(ocv,gray,COLOR_BGR2GRAY);
threshold(gray,gray,100,255,0);
Moments M = moments(gray);
Point cen( int(M.m10/M.m00), int(M.m01/M.m00) );
for (int i=0; i<360; i+=20)
{
double s = sin(i*CV_PI/180);
double c = cos(i*CV_PI/180);
Point p2(cen.x+s*150, cen.y+c*150); // any radius will do, we just want the direction
LineIterator it(ocv, cen, p2, 8);
Rect bounds(0, 0, ocv.cols, ocv.rows);
while(bounds.contains(it.pos()))
{
Vec3b & pixel = ocv.at<Vec3b>(it.pos());
// if you stare really hard, you'll see the cheat ;)
if (pixel[0] > 50) // non dark(it's not really black in the image!)
pixel[1] = pixel[2] = 0; // set g and b to 0, leaves blue line
else
break;
it++;
}
}
imshow("lines", ocv);
</code></pre>
<p><img alt="image description" src="/upfiles/14249508316138649.png"/></p>
http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=69762#post-id-69762@both be patient. I passed on this concerns to the dev team and they told me they would get back with answers mid september.Mon, 31 Aug 2015 08:17:26 -0500http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=69762#post-id-69762Answer by StevenPuttemans for <p>I have a contour named 'cnt' obtained from the image below:</p>
<p><img alt="image description" src="/upfiles/14249482552397042.png"/></p>
<p>for which I am able to find the centroid like this:</p>
<pre><code>M = cv2.moments(cnt)
centroid_x = int(M['m10']/M['m00'])
centroid_y = int(M['m01']/M['m00'])
</code></pre>
<p>I now want to draw N number of lines, each 360/N degrees apart, starting from the centroid and passing through all possible points of intersection with the contour. The cv2.line() function requires start point and end point but I don't have the end point.</p>
<p>If I had drawn a line passing through centroid with a slope of Tan(360/N) I would have found the intersection of the line with the contour using bitwise_and but I'm not able to figure out a way to draw that line. </p>
<p>Any help would be much appreciated. </p>
http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?answer=56148#post-id-56148Altough the approach by @Balaji will probably work without any problems, I do think this can be done much easier. From an angle and a starting point one can use mathematics to derive the function that represents the line. Once you have done that the following steps are quite easy.
- Define for each border (edge of mat) the value of the function. For example if your top border equals the equation `x = 0`, then you can define the y value of your line equation and see if that still falls within the range of your top edge.
- For each line you want to draw there will be only 2 points possible on your matrix. This also allows you only to draw lines for 180 degrees and get all 360 degree segments. Then use those two matching points to draw your line.
Necessary equation: http://stackoverflow.com/questions/1571294/line-equation-with-angle
Wed, 25 Feb 2015 04:07:36 -0600http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?answer=56148#post-id-56148Comment by Balaji R for <p>Altough the approach by <a href="/users/6041/balaji/">@Balaji</a> will probably work without any problems, I do think this can be done much easier. From an angle and a starting point one can use mathematics to derive the function that represents the line. Once you have done that the following steps are quite easy.</p>
<ul>
<li>Define for each border (edge of mat) the value of the function. For example if your top border equals the equation <code>x = 0</code>, then you can define the y value of your line equation and see if that still falls within the range of your top edge. </li>
<li>For each line you want to draw there will be only 2 points possible on your matrix. This also allows you only to draw lines for 180 degrees and get all 360 degree segments. Then use those two matching points to draw your line.</li>
</ul>
<p>Necessary equation: <a href="http://stackoverflow.com/questions/1571294/line-equation-with-angle">http://stackoverflow.com/questions/15...</a></p>
http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=56169#post-id-56169Yes that is correct! Step 1 is unnecessary.Wed, 25 Feb 2015 05:48:10 -0600http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=56169#post-id-56169Comment by berak for <p>Altough the approach by <a href="/users/6041/balaji/">@Balaji</a> will probably work without any problems, I do think this can be done much easier. From an angle and a starting point one can use mathematics to derive the function that represents the line. Once you have done that the following steps are quite easy.</p>
<ul>
<li>Define for each border (edge of mat) the value of the function. For example if your top border equals the equation <code>x = 0</code>, then you can define the y value of your line equation and see if that still falls within the range of your top edge. </li>
<li>For each line you want to draw there will be only 2 points possible on your matrix. This also allows you only to draw lines for 180 degrees and get all 360 degree segments. Then use those two matching points to draw your line.</li>
</ul>
<p>Necessary equation: <a href="http://stackoverflow.com/questions/1571294/line-equation-with-angle">http://stackoverflow.com/questions/15...</a></p>
http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=56262#post-id-56262that assumes, you *have* a function for the outline. if not, all you probably got there is a contour, a vector of points, and you'd have to do an intersection check for each consecutive pairs/linesThu, 26 Feb 2015 06:23:32 -0600http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=56262#post-id-56262Comment by StevenPuttemans for <p>Altough the approach by <a href="/users/6041/balaji/">@Balaji</a> will probably work without any problems, I do think this can be done much easier. From an angle and a starting point one can use mathematics to derive the function that represents the line. Once you have done that the following steps are quite easy.</p>
<ul>
<li>Define for each border (edge of mat) the value of the function. For example if your top border equals the equation <code>x = 0</code>, then you can define the y value of your line equation and see if that still falls within the range of your top edge. </li>
<li>For each line you want to draw there will be only 2 points possible on your matrix. This also allows you only to draw lines for 180 degrees and get all 360 degree segments. Then use those two matching points to draw your line.</li>
</ul>
<p>Necessary equation: <a href="http://stackoverflow.com/questions/1571294/line-equation-with-angle">http://stackoverflow.com/questions/15...</a></p>
http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=56264#post-id-56264Well I was going to split the image segments over the complete image :D Not only the contour :PThu, 26 Feb 2015 06:28:25 -0600http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?comment=56264#post-id-56264Answer by Balaji R for <p>I have a contour named 'cnt' obtained from the image below:</p>
<p><img alt="image description" src="/upfiles/14249482552397042.png"/></p>
<p>for which I am able to find the centroid like this:</p>
<pre><code>M = cv2.moments(cnt)
centroid_x = int(M['m10']/M['m00'])
centroid_y = int(M['m01']/M['m00'])
</code></pre>
<p>I now want to draw N number of lines, each 360/N degrees apart, starting from the centroid and passing through all possible points of intersection with the contour. The cv2.line() function requires start point and end point but I don't have the end point.</p>
<p>If I had drawn a line passing through centroid with a slope of Tan(360/N) I would have found the intersection of the line with the contour using bitwise_and but I'm not able to figure out a way to draw that line. </p>
<p>Any help would be much appreciated. </p>
http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?answer=56101#post-id-56101Hi @samkhan !
I think you are trying to find contour points that are at certain angle from centroid of that contour. Please follow the above steps:
1. Find [minenclosing circle](http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#minenclosingcircle) of that contour.
2. Find Point on the circle on a given angle. You can refer [this Answer](http://answers.opencv.org/question/36704/finding-a-point-on-a-circle/#36706)
3. Find all the Points on the Line segment [using Line Iterator](http://docs.opencv.org/modules/core/doc/drawing_functions.html#lineiterator).
4. Loop through all the contour points & find the Distance between two points(Contour Point & Point on the Line).
5. Find the Point with minimum distance which is the Intersection Point P.
6. Draw a Line from Centroid C to Point P.
Hope you understand this simple explanation & I'll leave the code part for you!
Tue, 24 Feb 2015 23:57:17 -0600http://answers.opencv.org/question/56095/draw-lines-from-centroid-of-contour-at-given-angle-till-edge-of-contour/?answer=56101#post-id-56101