2014-07-15 69 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);