AndroidDev 板


LINE

各位大大好 小弟目前利用 mamaya3 大大的 照相机预览的程式 在 Nexus 7 上抓到了底层回传的 Camera preview raw data(NV21格式) 并解码成RGB格式 我的问题是 想保留画面上的红色部分 其余部分转成灰阶 类似像这张图 http://ppt.cc/j53Q 可是 "影像处理完後,全变成蓝色滤镜的效果" 像这样 http://ppt.cc/9NfV >"< ******************************************************************* 我的做法是除了满足 1. pixel RGB 的 R 分量是否等於 255 2. pixel RGB 的 G 分量等於 0 3. pixel RGB 的 B 分量等於 0 的像素点不做修改 其余的像素点通通改成灰阶 也就是红色的点保留不除理,剩余的通通改成灰阶 ******************************************************************* 以下是我在客制化View中 (就是不断更新预览画面的View) 覆写的 onDraw 方法的程式码,总 若是有需要其余的java档,小弟可在上传 感谢板上高手 ^.^ =================================================================== @Override protected void onDraw(Canvas canvas){ Log.i(tag, "onDraw() "); //image 可以是framework 传到 Application 层的 preview raw data int size = imgWidth * imgHeight; int[] rgb = new int[imgWidth * imgHeight]; if(isCameraSet){ rgb = convertYUV420_NV21toARGB8888(image, imgWidth, imgHeight); for (int k = 0; k < size; k++) { if(Color.red(rgb[k]) == 255 && Color.green(rgb[k]) == 0 && Color.blue(rgb[k]) == 0){} else{ rgb[k] = (int) ((0.2126 * Color.red(rgb[k])) + (0.7152 * Color.green(rgb[k])) + (0.0722 * Color.blue(rgb[k]))); } } Log.i("tag", "rgb length = " + rgb.length); overlayBitmap = Bitmap.createBitmap(rgb, 0, imgWidth, imgWidth,imgHeight, Bitmap.Config.RGB_565); canvas.drawBitmap(overlayBitmap, matrix, null); overlayBitmap.recycle(); } } ======================================================================== 完整的 ViewToDraw 程式码 package com.example.macampreviewdemo; public class ViewToDraw extends View{ public String tag = "tag"; public byte[] image; public boolean isCameraSet = false; public int imgWidth, imgHeight; Bitmap overlayBitmap; Matrix matrix; public ViewToDraw(Context context, AttributeSet attrs) { super(context, attrs); matrix = new Matrix(); } public void cameraSet(){ isCameraSet = true; } public void putImage(byte[] img){ image = img; } @Override protected void onDraw(Canvas canvas){ Log.i(tag, "onDraw() "); //image 可以是framework 传到 Application 层的 preview raw data int size = imgWidth * imgHeight; int[] rgb = new int[imgWidth * imgHeight]; if(isCameraSet){ rgb = convertYUV420_NV21toARGB8888(image, imgWidth, imgHeight); for (int k = 0; k < size; k++) { if(Color.red(rgb[k]) == 255 && Color.green(rgb[k]) == 0 && Color.blue(rgb[k]) == 0){} else{ rgb[k] = (int) ((0.2126 * Color.red(rgb[k])) + (0.7152 * Color.green(rgb[k])) + (0.0722 * Color.blue(rgb[k]))); } } Log.i("tag", "rgb length = " + rgb.length); overlayBitmap = Bitmap.createBitmap(rgb, 0, imgWidth, imgWidth, imgHeight, Bitmap.Config.RGB_565); canvas.drawBitmap(overlayBitmap, matrix, null); overlayBitmap.recycle(); } } /** * Converts YUV420 NV21 to ARGB8888 * * @param data byte array on YUV420 NV21 format. * @param width pixels width * @param height pixels height * @return a ARGB8888 pixels int array. Where each int is a pixels ARGB. */ public static int[] convertYUV420_NV21toARGB8888(byte[] data, int width, int height) { int size = width*height; int offset = size; int[] pixels = new int[size]; int u, v, y1, y2, y3, y4; // i along Y and the final pixels // k along pixels U and V for(int i=0, k=0; i < size; i+=2, k+=1) { y1 = data[i ]&0xff; y2 = data[i+1]&0xff; y3 = data[width+i ]&0xff; y4 = data[width+i+1]&0xff; v = data[offset+k ]&0xff; u = data[offset+k+1]&0xff; v = v-128; u = u-128; pixels[i ] = convertYUVtoARGB(y1, u, v); pixels[i+1] = convertYUVtoARGB(y2, u, v); pixels[width+i ] = convertYUVtoARGB(y3, u, v); pixels[width+i+1] = convertYUVtoARGB(y4, u, v); if (i!=0 && (i+2)%width==0) i+=width; } return pixels; } private static int convertYUVtoARGB(int y, int u, int v) { int r,g,b; r = y + (int)(1.402f*u); g = y - (int)(0.344f*v + 0.714f*u); b = y + (int)(1.772f*v); r = r>255? 255 : r<0 ? 0 : r; g = g>255? 255 : g<0 ? 0 : g; b = b>255? 255 : b<0 ? 0 : b; return 0xff000000 | (r<<16) | (g<<8) | b; } } --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.124.161.210
1F:→ wuliou:我没看你的code 不过红色的threshold用255不会太大吗 03/11 21:06
2F:→ sheep922420:"保留红色部分" 的条件会再做调整,感谢大大意见 ^^ 03/11 21:25
3F:→ Griffith:不太懂影像处理,但有个小疑问~您转成灰阶的公式…转出来 03/11 22:57
4F:→ Griffith:不是Y(0~255)吗?但您是用RGB的格式去产生新图@@",这样 03/11 23:00
5F:→ Griffith:是ok的吗O_Q? 03/11 23:00
6F:→ Griffith:转灰阶的那一行改成:rgb[k] = rgb[k]<<16 | rgb[k]<<8 | 03/11 23:13
7F:→ Griffith:rgb[k]试试~~O_Q" 03/11 23:13
8F:推 sdyy:你红色取值太极端了 做完可能都当成杂讯了... 03/11 23:19
9F:→ Griffith:对了,而且如果用你转灰阶的方法就会变成... 03/11 23:30
10F:→ Griffith:value = 0x0000xx(R=00, G=00, B=xx, xx=[0, 255])这样可 03/11 23:31
11F:→ Griffith:能就可以解释为什麽会被遮上一层蓝色的东西了~~((小弟一 03/11 23:32
12F:→ Griffith:点拙见~如有不对地方请指正…((小声说~我也觉得threshold 03/11 23:33
13F:→ Griffith:可以再取的适切一些@@"~~~~~ 03/11 23:34
14F:→ sheep922420:感谢S大跟G大的意见,我再把保留红色的条件放宽~ 03/12 01:32
15F:→ sheep922420:我先看一下G大的解释, 感谢各位高手 !!! 03/12 01:34
16F:→ sheep922420:请教G大, value = 0x0000xx(R=00..xx=[0,255]) 这行的 03/12 01:37
17F:→ sheep922420:原因我不太明白 03/12 01:38
18F:→ Griffith:请问原PO大,您试过我所说的转换方式後,有正常吗@@???? 03/12 09:20
19F:→ Griffith:如果不正常~~~我讲的解释可能都会变成嘴炮XDDDDDDDD" 03/12 09:21
20F:→ Griffith:毕竟上面那些转换方式,只是小弟的猜测O_Q"~~~~~ 03/12 09:22
21F:推 lovelycateye:https://github.com/CyberAgent/android-gpuimage 03/12 10:34
22F:→ lovelycateye:参考看看? 03/12 10:34







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Gossiping站内搜寻

TOP