C中的OpenCv:管道出口处的图像结构为空
我试着用我的网络摄像头拍摄,然后用相框把它们送入管道。首先,我试着只拍一张照片并发送出去。 我把结构图的指针送入管道 但该结构的尖头在管道入口处已满,但在出口处为空 我一直在寻找关于管道中发送结构的解决方案,我读到我必须在读取出口之前用malloc初始化结构图片的指针。我尝试了一下,但这不起作用 这是我在opencv中使用的图像结构的文档: 我用这条命令编译:C中的OpenCv:管道出口处的图像结构为空,c,opencv,C,Opencv,我试着用我的网络摄像头拍摄,然后用相框把它们送入管道。首先,我试着只拍一张照片并发送出去。 我把结构图的指针送入管道 但该结构的尖头在管道入口处已满,但在出口处为空 我一直在寻找关于管道中发送结构的解决方案,我读到我必须在读取出口之前用malloc初始化结构图片的指针。我尝试了一下,但这不起作用 这是我在opencv中使用的图像结构的文档: 我用这条命令编译: gcc -ggdb `pkg-config --cflags opencv` -o `basename opencv_v3.c .c`
gcc -ggdb `pkg-config --cflags opencv` -o `basename opencv_v3.c .c` opencv_v3.c `pkg-config --libs opencv`
我有一个结果:
open webcam
take picture
size struct : 144
size picture : 921600
send picture
receive picture
size struct : -1386173560
size picture : 0
这是我的代码:
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int take_picture(CvCapture *capture,int fd[]){
IplImage* img = cvQueryFrame(capture);
printf("take picture\n\n");
printf("size struct : %d\n",img->nSize);
printf("size picture : %d\n\n",img->imageSize);
close(fd[0]);
write(fd[1], img,img->nSize);
close(fd[1]);
printf("send picture\n");
return img->nSize;
}
void reception(int fd[],int size){
IplImage* img = malloc(size);
close(fd[1]);
read(fd[0],img,size);
close(fd[0]);
printf("receive picture\n\n");
printf("size struct : %d\n",img->nSize);
printf("size picture : %d\n\n",img->imageSize);
free(img);
}
int main(int argc, char *argv[]){
int fd[2];
int size;
int pid;
if (pipe(fd) == -1){
printf("pipe failed\n");
return -1;
}
if((pid = fork()) < 0){
printf("fork failed\n");
return -1;
}
if (pid == 0){
/* child */
printf("open webcam \n");
CvCapture *capture=cvCreateCameraCapture(0);
size = take_picture(capture,fd);
}
else{
/* parent */
waitpid(pid,0,WUNTRACED);
reception(fd,size);
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
int-take_-picture(CvCapture*capture,int-fd[]){
IplImage*img=cvQueryFrame(捕获);
printf(“拍照\n\n”);
printf(“大小结构:%d\n”,img->nSize);
printf(“大小图片:%d\n\n”,img->imageSize);
关闭(fd[0]);
写入(fd[1],img,img->nSize);
关闭(fd[1]);
printf(“发送图片”);
返回img->nSize;
}
无效接收(内部fd[],内部大小){
IplImage*img=malloc(尺寸);
关闭(fd[1]);
读取(fd[0],img,大小);
关闭(fd[0]);
printf(“接收图片\n\n”);
printf(“大小结构:%d\n”,img->nSize);
printf(“大小图片:%d\n\n”,img->imageSize);
免费(img);
}
int main(int argc,char*argv[]){
int-fd[2];
整数大小;
int-pid;
如果(管道(fd)=-1){
printf(“管道失败\n”);
返回-1;
}
如果((pid=fork())<0){
printf(“fork失败\n”);
返回-1;
}
如果(pid==0){
/*孩子*/
printf(“打开网络摄像头”);
CvCapture*capture=cvCreateCameraCapture(0);
尺寸=拍摄照片(拍摄,fd);
}
否则{
/*母公司*/
waitpid(pid,0,WUNTRACED);
接收(fd,大小);
}
返回0;
}