如何在opencv检测到圆圈后执行一些shell脚本
How to execute some shell script after opencv detect circle
如何在opencv检测到圆圈后执行一些shell脚本(例如1.sh)?
我用过exec,它可以工作,但opencv程序在检测到圆圈后关闭,我想要的是程序直到我按下"q"键才关闭。
这是我的代码:
#include<cv.h>
#include<highgui.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int main( int argc, char **argv )
{
CvCapture *capture = 0;
IplImage *img = 0;
int key = 0;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_PLAIN,1.0,1.0,0,1,CV_AA);
capture = cvCaptureFromCAM( 0 );
if ( !capture ) {
fprintf( stderr, "Cannot open initialize webcam!n" );
return 1;
}
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
img = cvQueryFrame( capture );
if (!img)
exit(1);
IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
CvMemStorage* storage = cvCreateMemStorage(0);
while( key != 'q' ) {
img = cvQueryFrame( capture );
if( !img ) break;
cvCvtColor( img, gray, CV_BGR2GRAY );
cvSmooth( gray, gray, CV_GAUSSIAN, 5, 5 );
CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, >gray->height/40, 200, 100/*, 20, 100*/ );
int i;
for( i = 0; i < circles->total; i++ )
{
float* p = (float*)cvGetSeqElem( circles, i );
cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), >CV_RGB(50,255,30), 5, 8, 0 );
cvPutText(img, "CIRCLE",cvPoint(cvRound(p[0]+45),cvRound(p[1]+45)), &font, >CV_RGB(50,10,255));
if ( circles ) {
execl("./1.sh", (char *)0);
}
}
cvShowImage( "result", img );
cvShowImage("gray", gray);
key = cvWaitKey( 1 );
}
// cvReleaseMemStorage(storage);
// cvReleaseImage(gray);
cvDestroyAllWindows();
cvDestroyWindow( "result" );
cvReleaseCapture( &capture );
return 0;
}
我在ubuntu上使用了代码块。
exec*之后,将无法访问(该过程中的)任何代码。如果希望程序在不等待脚本完成的情况下继续运行,则可以fork、exec,否则添加等待。或者,您可以使用system或popen。
示例:
派生命令并等待的示例函数:
#include <unistd.h>
/*as a macro*/
#define FORK_EXEC_WAIT(a) ({int s,p;if((p=fork())==0)
{execvp(a[0],a);}else{while(wait(&s)!= p);}})
/*as a function*/
void fork_exec_wait(char** a) {
int s,p;
if((p=fork())==0){
execvp(a[0],a);
}else{
while(wait(&s)!= p);
}
}
发出命令并继续
#include <unistd.h>
/*as a macro*/
#define FORK_EXEC(a) ({if((fork())==0) execvp(a[0],a);})
/*as a function*/
void fork_exec(char** a) {
int s,p;
if((p=fork())==0)
execvp(a[0],a);
}
系统命令是~fork-exec-wait的"sh-c命令args"
#include <stdlib.h>
system("command args");
popen命令在没有sh-c的情况下是类似的,它将以流的形式(比如管道、fifo等)向您提供输出
#include <stdio.h>
FILE *fp;
fp = popen("command args", "r");
...
pclose(fp);
相关文章:
- 使用 execv 从C++代码执行的 Linux 脚本失败
- C++ Python 并不总是执行 Python 脚本
- 在使用PyInstaller冻结Python脚本时包含一个C++可执行文件
- 如何在 ubuntu 上的 php 脚本中获取程序(c,c++,java,python,php)的执行时间和内存使用量?
- 将 nodejs 脚本作为子进程执行(而不是从其他脚本执行)
- 无法从Linux,C ++执行"my_script" python脚本"main(filename)"功能;pModule = PyImport_Import(pName);返回空值
- VIM:执行复杂文件类型的脚本和显示结果
- 在C 功能执行后,使用GCHANDLE CRALSES将大型结构从C#Unity脚本传递到C DLL
- 如何使用gnu_parallal以运行多个可执行文件和/或bash脚本
- 使用C++脚本自动执行 CMake 生成
- 可视化 将 c++ 可执行文件图标添加到预制件 5 构建脚本
- C++执行执行的 bash 脚本随机停止
- 将输入值从BASH脚本传递到C 可执行文件
- 在C 中执行SQL脚本
- 循环浏览文件夹中的所有文件以继续执行脚本
- Subprocessces.call 执行 c++ 脚本失败
- 在嵌入式八度C++中如何执行脚本文件?( "error: invalid call to script" )
- 找不到C++执行脚本Aprog命令
- 等待执行不可执行脚本的子进程
- 使用c++执行脚本