作者sheep922420 ()
看板AndroidDev
标题[问题] 关於影像处理
时间Tue Mar 11 20:13:17 2014
各位大大好
小弟目前利用 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
22F:→ lovelycateye:参考看看? 03/12 10:34