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燈, 水草

請輸入看板名稱,例如:e-shopping站內搜尋

TOP