Note that there are some explanatory texts on larger screens.

plurals
  1. PODetetcting intersection between 2 line in webcam feed opencv
    primarykey
    data
    text
    <p>i'm trying to detect the intersection between two line in webcam feed. Here's the screenshot of what i already have <img src="https://i.stack.imgur.com/RpnwU.png" alt="enter image description here"></p> <p>I try to find the intersection between my red and green line.</p> <p>And here's the code of what i already have</p> <pre><code>#include "stdafx.h" #include &lt;cv.h&gt; #include &lt;opencv2/core/core.hpp&gt; #include &lt;opencv2/highgui/highgui.hpp&gt; #include &lt;highgui.h&gt; IplImage* imgTracking; int lastX = -1; int lastY = -1; //This function threshold the HSV image and create a binary image IplImage* GetThresholdedImage(IplImage* imgHSV){ IplImage* imgThresh=cvCreateImage(cvGetSize(imgHSV),IPL_DEPTH_8U, 1); cvInRangeS(imgHSV, cvScalar(170,160,60), cvScalar(180,2556,256), imgThresh); return imgThresh; } void trackObject(IplImage* imgThresh){ // Calculate the moments of 'imgThresh' CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments)); cvMoments(imgThresh, moments, 1); double moment10 = cvGetSpatialMoment(moments, 1, 0); double moment01 = cvGetSpatialMoment(moments, 0, 1); double area = cvGetCentralMoment(moments, 0, 0); // if the area&lt;1000, I consider that the there are no object in the image and it's because of the noise, the area is not zero if(area&gt;1000){ // calculate the position of the ball int posX = moment10/area; int posY = moment01/area; if(lastX&gt;=0 &amp;&amp; lastY&gt;=0 &amp;&amp; posX&gt;=0 &amp;&amp; posY&gt;=0) { // Draw a yellow line from the previous point to the current point cvLine(imgTracking, cvPoint(posX, posY), cvPoint(lastX, lastY), cvScalar(0,0,255), 4); } lastX = posX; lastY = posY; } cvLine(imgTracking,cv::Point(100,300) , cv::Point(600,300),cv::Scalar(0,200,0),2,8); free(moments); } bool intersection(cv::Point lastX, cv::Point lastY, cv::Point , cv::Point()) { } /* void imaginaryline() { cv::Mat img = cv::Mat::zeros(500, 500, CV_8UC3); cv::line(img, cv::Point(100,200) , cv::Point(400,100),cv::Scalar(0,200,0),2,8); }*/ int main(){ CvCapture* capture =0; capture = cvCaptureFromCAM(0); if(!capture){ printf("Capture failure\n"); return -1; } IplImage* frame=0; frame = cvQueryFrame(capture); if(!frame) return -1; //create a blank image and assigned to 'imgTracking' which has the same size of original video imgTracking=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U, 3); cvZero(imgTracking); //covert the image, 'imgTracking' to black cvNamedWindow("Video"); cvNamedWindow("Ball"); //iterate through each frames of the video while(true){ frame = cvQueryFrame(capture); if(!frame) break; frame=cvCloneImage(frame); cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV IplImage* imgThresh = GetThresholdedImage(imgHSV); cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel //track the possition of the ball trackObject(imgThresh); printf("Pos X = %d", lastX); printf("Pos Y = %d", lastY); // Add the tracking image and the frame cvAdd(frame, imgTracking, frame); cvShowImage("Ball", imgThresh); cvShowImage("Video", frame); //Clean up used images cvReleaseImage(&amp;imgHSV); cvReleaseImage(&amp;imgThresh); cvReleaseImage(&amp;frame); //Wait 10mS int c = cvWaitKey(10); //If 'ESC' is pressed, break the loop if((char)c==27 ) break; } cvDestroyAllWindows() ; cvReleaseImage(&amp;imgTracking); cvReleaseCapture(&amp;capture); return 0; } </code></pre> <p>Thank you for your attention guys, i waited for any of your response</p> <p>UPDATE : Thanks to Sebastian Schmitz, i sollved it. Here's my code</p> <pre><code>void checkIntersection(int line, int lastY, int y) { if(lastY &lt;&lt; line &amp;&amp; y &gt;= line || lastY &gt; line &amp;&amp; y &lt;= line) { printf("intersection detected"); } } void trackObject(IplImage* imgThresh){ // Calculate the moments of 'imgThresh' CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments)); cvMoments(imgThresh, moments, 1); double moment10 = cvGetSpatialMoment(moments, 1, 0); double moment01 = cvGetSpatialMoment(moments, 0, 1); double area = cvGetCentralMoment(moments, 0, 0); // if the area&lt;1000, I consider that the there are no object in the image and it's because of the noise, the area is not zero if(area&gt;1000){ // calculate the position of the ball int posX = moment10/area; int posY = moment01/area; if(lastX&gt;=0 &amp;&amp; lastY&gt;=0 &amp;&amp; posX&gt;=0 &amp;&amp; posY&gt;=0) { // Draw a yellow line from the previous point to the current point cvLine(imgTracking, cvPoint(posX, posY), cvPoint(lastX, lastY), cvScalar(0,0,255), 4); } checkIntersection(300, lastY, posY); lastX = posX; lastY = posY; } cvLine(imgTracking,cv::Point(100,300) , cv::Point(600,300),cv::Scalar(0,200,0),2,8); cvRectangle(imgTracking,cv::Point(400,400), cv::Point(450,450),cv::Scalar(0,200,0),2,8); free(moments); } </code></pre> <p>i put the call for checkintersection function inside trackobject function so i dont have to change variable posY into global which lead to many error. </p> <p>Thank you all for your response</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload