日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

opencv检测相交点_在网络摄像头feed opencv中检测2条线之间的交点

發(fā)布時(shí)間:2023/12/14 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 opencv检测相交点_在网络摄像头feed opencv中检测2条线之间的交点 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我正在嘗試檢測(cè)攝像頭Feed中兩條線之間的交點(diǎn).這是我已經(jīng)擁有的屏幕截圖

我嘗試找到紅線和綠線之間的交點(diǎn).

這是我已經(jīng)擁有的代碼

#include "stdafx.h"

#include

#include

#include

#include

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<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>1000){

// calculate the position of the ball

int posX = moment10/area;

int posY = moment01/area;

if(lastX>=0 && lastY>=0 && posX>=0 && posY>=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

");

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(&imgHSV);

cvReleaseImage(&imgThresh);

cvReleaseImage(&frame);

//Wait 10mS

int c = cvWaitKey(10);

//If 'ESC' is pressed, break the loop

if((char)c==27 ) break;

}

cvDestroyAllWindows() ;

cvReleaseImage(&imgTracking);

cvReleaseCapture(&capture);

return 0;

}

謝謝您的關(guān)注,我等待您的任何回復(fù)

更新:

多虧塞巴斯蒂安·施密茨(Sebastian Schmitz),我才解決了.這是我的代碼

void checkIntersection(int line, int lastY, int y)

{

if(lastY << line && y >= line || lastY > line && y <= 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<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>1000){

// calculate the position of the ball

int posX = moment10/area;

int posY = moment01/area;

if(lastX>=0 && lastY>=0 && posX>=0 && posY>=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);

}

我把對(duì)checkintersection函數(shù)的調(diào)用放在trackobject函數(shù)中,所以我不必將變量posY更改為global,這會(huì)導(dǎo)致很多錯(cuò)誤.

謝謝大家的回應(yīng)

總結(jié)

以上是生活随笔為你收集整理的opencv检测相交点_在网络摄像头feed opencv中检测2条线之间的交点的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。