opencv使用线程刷新问题
我使用两个线程:
线程 1 是从相机获取帧并处理它们的线程 线程 2 是使用 cvshowimage 显示它们的线程
在第二个线程中我使用 cvWaitKey(200); (我也尝试了其他值..)
问题是显示了第一个图像,但过了一会儿它们就不再显示了(当您尝试移动窗口时会发生相同的情况。 它冻结了,有时图像变成空白。
知道如何解决这个问题吗?
编辑: 当我在线程中显示图像时,我会丢失帧。应该是正常的吧?
编辑2: 我还尝试在线程中可视化 2 个较旧的帧而不是新的帧,但输出相同。Edit3
: 这就是我或多或少正在做的事情:
void *showImages( void *ptr )
{
bool showit = false;
while (!MainThreadHasFinished)
{
pthread_mutex_lock( &mutex1 );
if(ImageGenerated = true)
showit = true;
else
showit = false;
pthread_mutex_unlock( &mutex1 );
showit = true;
if(showit == true)
cvShowImage( "RGB Image", RGBImage);
cvWaitKey(500);
}}
IplImage *RGBImage;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
bool ImageGenerated;
int main(int argc, char** argv)
{
//init camera and other stuff
int frameCounter=0;
RGBImage = cvCreateImage( cvSize(RGB_RES_X,RGB_RES_Y),8,3);
int iret1 = pthread_create( &showImagesThread, NULL, showImages, (void*) message1);
for (;;) {
pthread_mutex_lock( &mutex1 );
ImageGenerated = false;
pthread_mutex_unlock( &mutex1 );
//get frame here in showImg
frameCounter++;
if(frameCounter == 10) frameCounter=0;
if(frameCounter == 2)
cvCopy(&showImg,RGBImage);
pthread_mutex_lock( &mutex1 );
ImageGenerated = true;
pthread_mutex_unlock( &mutex1 );
//other stuff
}
}
干杯
I'm using two threads:
thread 1 is the the one that takes the frames from the camera and process them
thread 2 is the one that displays them using cvshowimage
In the second thread I'm using cvWaitKey(200); (i tried also other values..)
The problem is that the first images are showed but after a while they are not anymore (the same situation occurs when you try to move the window.
It freezes and sometimes the image becomes blank..
Any idea of how can I solve this problem?
Edit:
when I show images in the thread I loose frames. Should it be normal?
Edit2:
I tried also to visualize in the thread 2 older frames instead of the new one but same output..
Edit3:
This is what I'm doing more or less:
void *showImages( void *ptr )
{
bool showit = false;
while (!MainThreadHasFinished)
{
pthread_mutex_lock( &mutex1 );
if(ImageGenerated = true)
showit = true;
else
showit = false;
pthread_mutex_unlock( &mutex1 );
showit = true;
if(showit == true)
cvShowImage( "RGB Image", RGBImage);
cvWaitKey(500);
}}
IplImage *RGBImage;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
bool ImageGenerated;
int main(int argc, char** argv)
{
//init camera and other stuff
int frameCounter=0;
RGBImage = cvCreateImage( cvSize(RGB_RES_X,RGB_RES_Y),8,3);
int iret1 = pthread_create( &showImagesThread, NULL, showImages, (void*) message1);
for (;;) {
pthread_mutex_lock( &mutex1 );
ImageGenerated = false;
pthread_mutex_unlock( &mutex1 );
//get frame here in showImg
frameCounter++;
if(frameCounter == 10) frameCounter=0;
if(frameCounter == 2)
cvCopy(&showImg,RGBImage);
pthread_mutex_lock( &mutex1 );
ImageGenerated = true;
pthread_mutex_unlock( &mutex1 );
//other stuff
}
}
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定尝试从另一个线程在 GUI 上显示某些内容是一个好主意。除非我误解了你的意思,否则你应该直接从 GUI 线程调用
cvshowimage
。I'm not sure that trying to show something on GUI from another thread is a very good idea. Unless I've misunderstood what you're saying, you should call
cvshowimage
from the GUI thread directly.