2014-07-15 73 views
0

我是一个嵌入式编程人员,使用多线程应用程序,它将通过串行线接收像素数据并将其显示在窗口中。我使用openCV的cvSetData()方法来复制通过串行线接收到的数据并将其填充到openCV数组中。还使用cvShowImage()函数,我正在显示不断更新的像素数据(显示视频的概念)。OpenCV多线程XInitThreads错误

这里是我的代码片段:

//-------------------------------------Start of code------------------------------------// 
#include <stdio.h> 
#include <stdlib.h> 

#include <fcntl.h> 
#include <unistd.h> 
#include <termios.h> 
#include <sys/select.h> 
#include "serial_comm_defines.h" 

#include <opencv2/highgui/highgui.hpp> 

#include <signal.h> 
#include <time.h> 
#include <sys/time.h> 

#include <pthread.h> 



extern unsigned char array[COUNT_LIM]; 
IplImage *newimage; 

img_disp_method(void) 
{ 

cvSetData((CvArr*)newimage, (void*)array, 1556); 
cvNamedWindow("Mywindow",CV_WINDOW_FREERATIO); 
cvResizeWindow("Mywindow", 1556, 360); 
cvShowImage("Mywindow",(CvArr*)newimage); 
cvWaitKey(1); 

} 


void *serial_thread_method(void* my_fd) 
{ 
clock_t start = 0, end = 0; 
double time_taken = 0; 

if ((int)my_fd<0) 
printf("\nError opening device file\n"); 
else 
{ 
printf("\nDevice file opened successfully\n"); 
if (serial_config((int)my_fd) < 0) 
printf("\nUnable to configure serial port\n"); 
else 
{ 
printf("\nSerial port configured successfully\n"); 

for(;;) 
{ 
    start = clock(); 

serial_read((int)my_fd); 

    end = clock(); 

    printf("\nTime taken:%f seconds\n", (double)((end-start)/CLOCKS_PER_SEC)); 
} 

} 
} 

close ((int)my_fd); 

return NULL; 
} 


int main(int argc, char **argv) 
{ 
pthread_t serial_read_thread; 
int my_fd=0, i=0, temp=0, serial_thread_ret=0; 
newimage = cvCreateImageHeader(cvSize(HEIGHT, WIDTH), IPL_DEPTH_8U, 0x01); 
struct timeval my_value={0,10000}; 
struct timeval my_interval={0,10000}; 
struct itimerval my_timer={my_interval,my_value}; 

setitimer(ITIMER_REAL, &my_timer, 0); 
signal(SIGALRM, img_disp_method); 

my_fd = open_device_file((char**)argv); 


if ((serial_thread_ret = pthread_create(&serial_read_thread, NULL, serial_thread_method, (void*)my_fd) == 0)) 
    fprintf(stdout, "\nSerial read thread created successfully\n"); 
else 
    perror("\nError creating serial read thread\n"); 


pthread_join(&serial_read_thread, NULL); 

cvReleaseImageHeader(&newimage); 

return NULL; 
} 
//----------------------------------------End of code--------------------------------------// 

的代码编译的罚款。但是当我执行二进制文件时,会引发以下错误。我还观察到,如果将计时器的值(my_value和my_interval的值)更改为大于30ms(30000)的任何位置,代码就可以正常工作。请解释发生了什么。

+0

请避免的OpenCV的过时C-API。他们已经在2010年转向了C++,所以你应该。 – berak

回答

0

尝试使用虚拟计时器而不是实时计时器。

像这样的事情

setitimer(SIGVTALRM, &my_timer, 0); 

signal(SIGVTALRM, img_disp_method);