YUV转灰度
转载自:http://blog.csdn.net/sxjk1987/article/details/7470545
标准的V4L2 API
http://v4l.videotechnology.com/dwg/v4l2.pdf
在例程/home/dvevm_1_20/demos/ImageGray中,涉及到图像采集及显示的一些概念
主要的几个文件
capture.c
display.c
video.c
在demo里面采集用到的格式是UYVY
V4L2_PIX_FMT_UYVY ('UYVY')
Name
V4L2_PIX_FMT_UYVY -- Variationof V4L2_PIX_FMT_YUYV with different order of samples in memory
Description
In this format each four bytes is twopixels. Each four bytes is two Y's, a Cb and a Cr. Each Y goes to one of thepixels, and the Cb and Cr belong to both pixels. As you can see, the Cr and Cbcomponents have half the horizontal resolution of the Y component.
Example 2-1. V4L2_PIX_FMT_UYVY4 × 4 pixel image
Byte Order. Each cell is one byte.
|
start + 0: |
Cb00 |
Y'00 |
Cr00 |
Y'01 |
Cb01 |
Y'02 |
Cr01 |
Y'03 |
|
start + 8: |
Cb10 |
Y'10 |
Cr10 |
Y'11 |
Cb11 |
Y'12 |
Cr11 |
Y'13 |
|
start + 16: |
Cb20 |
Y'20 |
Cr20 |
Y'21 |
Cb21 |
Y'22 |
Cr21 |
Y'23 |
|
start + 24: |
Cb30 |
Y'30 |
Cr30 |
Y'31 |
Cb31 |
Y'32 |
Cr31 |
Y'33 |
Color Sample Location.
|
|
0 |
|
1 |
|
2 |
|
3 |
|
0 |
Y |
C |
Y |
|
Y |
C |
Y |
|
1 |
Y |
C |
Y |
|
Y |
C |
Y |
|
2 |
Y |
C |
Y |
|
Y |
C |
Y |
|
3 |
Y |
C |
Y |
|
Y |
C |
Y |
在imagegray里面把图片变成灰度是在filecopy_dec.c这个函数里面有这样的代码
static void PictureGray(void *pInbuf,void*pOutbuf,unsigned int len)
{
unsigned int i;
unsigned int * pIn = (unsigned int*)pInbuf;
unsigned int *pOut= (unsigned int*)pOutbuf;
len >>= 2;
for(i=0;i<len;i++)
{
*pOut= *pIn & 0xff00ff00 | 0x00800080;
pIn++;
pOut++;
}
}
从上面的UYVY的格式解释里面可以看到,变成灰度图像只是把图像四个字节跟0xff00ff00相与就可以了,一直没明白为什么还要在后面加上一个0x00800080相或,我原来一直以为是不是UYVY的格式解释不一样,找了半天各种格式之间区别的不少,讲到这个点子上的倒没看到,最后在TI的网站上有这样的回复
i want to transfer the input image ofyuv422 format to a gray image
if you want a grayscale output in the YCbCrcolorspace of your input you can just set the Cb and Cr values to 0x80and leave the Y as is, as Y is the brightness/luminance and is essentially thegreyscale version of the image so if you set the color values to a constantmedian value of 0x80 you end up with a grayscale image.
For working with the encodedecode demo, ifall you want to do is make the output look grayscale than the small functionmentioned above (and here) should still work for you. In the encodedecode demoyou are actually compressing and decompressing the video so you could performthis operation between capture and compress or between decompress and display,either way should work as the frame buffer will be in the proper YCbCR 4:2:2format at both points. In YCbCr 4:2:2 you have a stream of bytes in the formCbYCrYCbYCrYCbYCrY... and so on, where the Y values for luminance arefor each pixel and each 2 pixels shares a Cb and Cr for chrominance. Since agreyscale image is an image with only brightness/luminance and nochrominance/color all you have to do is remove the chrominance values,
howeversince the display is expecting an image formatted in color we cannot take themout completely,
but rather we can make the chrominancevalues constant, and for greyscale/B&W you want them to all be mid pointvalues of 0x80 (out of 0xFF).
The code below does just this, if yougive it a pointer to the YCbCr 4:2:2 frame buffer and the size of the buffer itwill step through and make all of the Cb and Cr values 0x80 so that the imageappears greyscale on the output.
Bernie Thompson:
//make the image greyscale by setting allchrominance to 0x80
void process_imagebw( void* currentFrame, int yRows, int xPixels)
{
int xx = 0;
for( xx = 0; xx < (yRows * xPixels)*2; xx+=2)//just operating on the chroma
{
*( ( (unsigned char*)currentFrame ) + xx ) = 0x80; //set all chromato midpoint 0x80
}
} // End process_imagebw()
对TVP5158采集到的YUV422分析,它是以UYVY格式存放的,因为U,V是正交化的由于本课题用到的图像处理基于黑白图像,所以只需对Y分量进行处理,那么第一步就是对YUV422提取出Y,如果要保留Y分量,那么需将UV置为0x80,如需置白/黑色,Y分量设为FF/00,提取出亮度信号后,即可作简单的二值化.
UV是色差分量,UV为0就会全是绿色,全为0x80的时候才能看到灰度图。
YUV转灰度的更多相关文章
- 记一次YUV图像分析(二)
当你有一帧图像的原始(Raw)数据,不知道是RGB像素图还YUV格式时,可以利用YUV的灰度图成块状能量的特点(这也是为什么YUV格式可以被压缩编码的原因),进行简单的分辨. 当你用hexdump一类 ...
- 图像处理之基础---彩色转灰度算法优化rgb to yuv
File: StudyRGB2Gray.txtName: 彩色转灰度算法彻底学习Author: zyl910Version: V1.0Updata: 2006-5- ...
- YUV格式介绍
原文链接:http://www.cnblogs.com/azraelly/archive/2013/01/01/2841269.html YUV格式有两大类:planar和packed.对于plana ...
- 【Android】直播必备之YUV使用总结 —— Android常用的几种格式:NV21/NV12/YV12/YUV420P的区别
说明 因工作方面接触到图像处理这一块,需要对手机摄像头采集的原始帧做Rotate或者Scale,但无奈对此的了解少之又少,于是网上搜了一顿,完事后将最近所学总结一下,以方便之后的人别踩太多坑. 首先想 ...
- java实现图像灰度化
/*在研究Java实现将一张图片转成字符画的时候,发现将图像转化字符串是根据照片的灰度采用不同的字符画出来,形成一个灰度表.于是就研究了下关于灰度值这个东西,于是跳了一个大坑...因为鄙人用的ubun ...
- 关于yuv格式
首先,内存分布 1:YUV420 (1):I420: YYYYYYYY UU VV =>YUV420P (2): ...
- 【转】图像灰度化方法总结及其VC实现
转载自: http://blog.csdn.net/likezhaobin/article/details/6915754 最近一段时间作者开始进行运动目标识别定位系统设计,本文以及后续的几篇文章都 ...
- Atitit 图像处理 灰度图片 灰度化的原理与实现
Atitit 图像处理 灰度图片 灰度化的原理与实现 24位彩色图与8位灰度图 首先要先介绍一下24位彩色图像,在一个24位彩色图像中,每个像素由三个字节表示,通常表示为RGB.通常,许多24位彩色图 ...
- 【Android】YUV使用总结 —— Android常用的几种格式:NV21/NV12/YV12/YUV420P的区别
工作问题接触到图像这一块,需要对手机摄像头采集的原始帧做Rotate或者scale,但无奈对此的了解少之又少,于是网上搜了一顿,完事后将最近所学总结一下,以方便之后的人别踩太多坑. 首先想 ...
随机推荐
- 4.MVC框架开发(母版页的应用、按钮导致的Action处理、从界面向控制器传数据和HtmlHelper控件的实现(注册的实现))
1.在视图里如何引入母版页 1)在视图里母版页都是放在View目录下面的Shared文件夹下面 2)母版页里的RenderBody()类似于ASP.NET里面的ContentPalceHolder占位 ...
- 自定义MVC路由配置
首先我用MVC4新增一个订单查看的功能 1.创建控制器OrderController namespace MvcApplication3.Controllers { public class Orde ...
- UILocalNotification本地通知
// 执行通知一定要退出应用或挂起应用(进入后台)才能收到通知. 1.在iOS8及其以后版本中使用本地消息需要先获得用户的许可,否则无法成功注册本地消息.因此,我们将询问用户许可的代码片段添加到了ap ...
- TypeScript学习指南第二章--接口(Interface)
接口(Interface) TypeScript的核心机制之一在于它的类型检查系统(type-checker)只关注一个变量的"模型(shape)" 稍后我们去了解这个所谓的形状是 ...
- Lucene基础(四)-- 结合数据库使用
需求 很多时候我们在用数据库的需要使用模糊查询,我们一般会使用like语句来做,然而这样的做的效率不是很多(很抱歉我们亲自去测,很多都这么说的),那么使用Lucene来检索的话,效率会高很多. luc ...
- UIViewCotroller 的生命周期函数
Viewcontroller 的所有生命周期函数 重写时 一定要先写 父类 方法 就是(super +生命周期函数) LoadView ViewDidLoad ViewDidUnload: 在iOS ...
- poj 3373 Changing Digits (DFS + 记忆化剪枝+鸽巢原理思想)
http://poj.org/problem?id=3373 Changing Digits Time Limit: 3000MS Memory Limit: 65536K Total Submi ...
- 映像备份与恢复管理工具Easy Image X使用说明
Easy Image X(简称EIX)是一个支持Ghost映像(.gho)和ImageX映像(.wim)的映像管理工具,具有友好的图形界面,仅需几步简单操作即可完成映像备份与恢复工作.维护时使用最多的 ...
- C++ dll调用-动态(显式)
C++ dll调用-动态(显式) 废话不说上代码, dll 头文件 j_test.h #pragma once extern "C"_declspec(dllexport) voi ...
- POJ2220+DFS
题意:给出n个宝物,m个寻宝人. 每个寻宝人对n个宝物都有一个估价. 从这些估价中挑出某些,是的价值差最小. dfs..没什么好说的,暴力枚举! /* 搜索+枚举 */ #include<std ...