UVC摄像头开发(三)

        本次主要记录QT以及QML显示UVC摄像头采集图像以及期间可靠性崩溃和刷新频率问题

        在开发UVC摄像头过程中,开始直接将图像显示在FrameBuffer上面,该方式很多猿兄已经写过了方式方法,大家搜一下就可以看到,具体不展开细说。
该方式如果和触摸屏同步使用,打开相机后显示图像会和桌面程序抢占最上层界面进行显示,此种情况下就多了在QT或者QML上直接显示图像。
此时程序设计发生变化如下:

        显然,将UVC摄像头采集到的图像数据显示在QML上,考虑到QT自带QImage就是处理图像的模块,自然该需求变化未QML显示QImage的需求。同样有很多大佬写了关于QML如何显示QImage的案例,典型的方式是重写QQuickImageProvider给QML提供QImage。该方式大家搜一下,有很多实现方式,就不多说。

        这里说一下转换过程,由于UVC摄像头采集到的原始图像数据为YUV数据,如何转至RGB格式进行显示。
        YUV422数据格式:大致为每一组Y分量用一组UV分量,由此可以算出每一个像素占用字节数为:1(Y)+1/2(U)+1/2(V)=2bytes,所以计算大小时图像单帧字节数为:ImgWidth*ImgHeight*2,大致可以理解为每一个Y分量公用两个UV分量,其中YUYV大致排布如下图。


根据YUV转换成RGB模型有如下公式:

R = 1.164*(Y-16) + 1.159*(V-128); 
G = 1.164*(Y-16) - 0.380*(U-128)+ 0.813*(V-128); 
B = 1.164*(Y-16) + 2.018*(U-128)); 

        则可以设计YUV转换成RGB转换函数,由于YUV模型为YUV422(YUYV)模型,这里根据转换模型计算出的RGB分量需要两次分发,保证每一组Y分量公用UV分量。具体函数如下:

int yuv2rgb(int y, int u, int v)
{
     unsigned int pixel32 = 0;
     unsigned char *pixel = (unsigned char *)&pixel32;
     int r, g, b;
     static long int ruv, guv, buv;

     if(1 == flag)
     {
         flag = 0;
         ruv = 1159*(v-128);
         guv = -380*(u-128) + 813*(v-128);
         buv = 2018*(u-128);
     }

     r = (1164*(y-16) + ruv) / 1000;
     g = (1164*(y-16) - guv) / 1000;
     b = (1164*(y-16) + buv) / 1000;

     if(r > 255) r = 255;
     if(g > 255) g = 255;
     if(b > 255) b = 255;
     if(r < 0) r = 0;
     if(g < 0) g = 0;
     if(b < 0) b = 0;

     pixel[0] = r;
     pixel[1] = g;
     pixel[2] = b;

     return pixel32;
}

则每一帧图像转换成RGB如下函数:

int yuyv2rgb32(unsigned char *yuv, 
			   unsigned char *rgb, 
			   unsigned int width,
			   unsigned int height)
{
     unsigned int in, out;
     int y0, u, y1, v;
     unsigned int pixel32;
     unsigned char *pixel = (unsigned char *)&pixel32;
     //分辨率描述像素点个数,而yuv2个像素点占有4个字符,所以这里计算总的字符个数,需要乘2
     unsigned int size = width*height*2; 

     for(in = 0, out = 0; in < size; in += 4, out += 8)
     {
          y0 = yuv[in+0];
          u  = yuv[in+1];
          y1 = yuv[in+2];
          v  = yuv[in+3];

          flag = 1;
          pixel32 = yuvtorgb(y0, u, v);
          rgb[out+0] = pixel[0];   
          rgb[out+1] = pixel[1];
          rgb[out+2] = pixel[2];
          rgb[out+3] = 0;  //32位rgb多了一个保留位

          pixel32 = yuvtorgb(y1, u, v);
          rgb[out+4] = pixel[0];
          rgb[out+5] = pixel[1];
          rgb[out+6] = pixel[2];
          rgb[out+7] = 0;

     }
     return 0;
}

        所以,最终UVC摄像头采集到的数据变成了一个unsigned char * buffer;怎么将UVC图像用QML显示转成成,将一个无符号的buffer转换成QImage进行QML显示。
        注意到,QImage有如下构造函数:

QImage(const QSize &size, Format format);
    QImage(int width, int height, Format format);
    QImage(uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = Q_NULLPTR, void *cleanupInfo = Q_NULLPTR);
    QImage(const uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = Q_NULLPTR, void *cleanupInfo = Q_NULLPTR);
    QImage(uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = Q_NULLPTR, void *cleanupInfo = Q_NULLPTR);
    QImage(const uchar *data, int width, int height, int bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = Q_NULLPTR, void *cleanupInfo = Q_NULLPTR);

所以图像转换可以如下:

QImage img((const unsigned char *)(pImgBufffer), uiImgWidth,uiImgHeight, uiImgWidth*4, QImage::Format_RGB32);
QImage ret = img.rgbSwapped();
ShowImage::instance()->sendimage(ret.convertToFormat(QImage::Format_RGB888));

ShowImage::instance()->sendimage(ret.convertToFormat(QImage::Format_RGB888));//QQuickImageProvider中接口进行信号更新,这一块可以搜一下猿兄们的博客,很多,不细讲。

在可靠性测试期间容易崩溃,检测信号如下:
    Program received signal SIGSEGV, Segmentation fault.

查询Linux信号两说明,这里给出Linux信号值 

/* Signals.  */
#define	SIGHUP		1	/* Hangup (POSIX).  */
#define	SIGINT		2	/* Interrupt (ANSI).  */
#define	SIGQUIT		3	/* Quit (POSIX).  */
#define	SIGILL		4	/* Illegal instruction (ANSI).  */
#define	SIGTRAP		5	/* Trace trap (POSIX).  */
#define	SIGABRT		6	/* Abort (ANSI).  */
#define	SIGIOT		6	/* IOT trap (4.2 BSD).  */
#define	SIGBUS		7	/* BUS error (4.2 BSD).  */
#define	SIGFPE		8	/* Floating-point exception (ANSI).  */
#define	SIGKILL		9	/* Kill, unblockable (POSIX).  */
#define	SIGUSR1		10	/* User-defined signal 1 (POSIX).  */
#define	SIGSEGV		11	/* Segmentation violation (ANSI).  */
#define	SIGUSR2		12	/* User-defined signal 2 (POSIX).  */
#define	SIGPIPE		13	/* Broken pipe (POSIX).  */
#define	SIGALRM		14	/* Alarm clock (POSIX).  */
#define	SIGTERM		15	/* Termination (ANSI).  */
#define	SIGSTKFLT	16	/* Stack fault.  */
#define	SIGCLD		SIGCHLD	/* Same as SIGCHLD (System V).  */
#define	SIGCHLD		17	/* Child status has changed (POSIX).  */
#define	SIGCONT		18	/* Continue (POSIX).  */
#define	SIGSTOP		19	/* Stop, unblockable (POSIX).  */
#define	SIGTSTP		20	/* Keyboard stop (POSIX).  */
#define	SIGTTIN		21	/* Background read from tty (POSIX).  */
#define	SIGTTOU		22	/* Background write to tty (POSIX).  */
#define	SIGURG		23	/* Urgent condition on socket (4.2 BSD).  */
#define	SIGXCPU		24	/* CPU limit exceeded (4.2 BSD).  */
#define	SIGXFSZ		25	/* File size limit exceeded (4.2 BSD).  */
#define	SIGVTALRM	26	/* Virtual alarm clock (4.2 BSD).  */
#define	SIGPROF		27	/* Profiling alarm clock (4.2 BSD).  */
#define	SIGWINCH	28	/* Window size change (4.3 BSD, Sun).  */
#define	SIGPOLL		SIGIO	/* Pollable event occurred (System V).  */
#define	SIGIO		29	/* I/O now possible (4.2 BSD).  */
#define	SIGPWR		30	/* Power failure restart (System V).  */
#define SIGSYS		31	/* Bad system call.  */
#define SIGUNUSED	31

        SIGSEGV是是当一个进程执行了一个无效的内存引用,或发生段错误时发送给它的信号。意思是程序接受一个无效的指针地址,Segmentation fault即是提示我们去注意定义指针的符号。显然,问题已经很明显,即程序内部使用了一个无效指针。考虑到除了主线程,采集子线程,QML也算一个信号出发线程,该错误应该是出现了现成同步问题,导致一个线程销毁了指针后另一个线程引用过程中出现了无效指针。考虑到QImage构造函数都为浅拷贝,数据传递至QML时直接传递存在风险,进行深拷贝后在数据传递解决崩溃问题。

        同时,由于FrameBuffer显示刷新速率很快,采用QML时刷新很慢,部分时候只有显示一部分图像就一直在刷新,这是由于QML显示QImage过程中时间开销大,需要一定的缓冲时间,对实时性要求高的场景不建议使用QQuickImageProvider模块类实现。

源代码分享:https://download.csdn.net/download/lrc_920212/86727460

实时显示效果:https://live.csdn.net/v/241301?spm=1001.2014.3001.5501

以上是部分总结,欢迎大家留言沟通。

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半叶清枫

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值