Android的OpenCV的转换垫灰度使用matToBitmap / bitmapToMat [英] android-opencv converting mat to grayscale with using matToBitmap/bitmapToMat
本文介绍了Android的OpenCV的转换垫灰度使用matToBitmap / bitmapToMat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用willowgarage更新的OpenCV库的日食。我想转换一个垫子变量转换成灰度,我尝试了一切,我发现在网络上,但他们没有为我工作。
I am using newer opencv library of willowgarage in eclipse. And I want to convert a mat variable into grayscale, I've tried everything I found on the net but they didnt work for me.
下面是我的code
package com.deneme.deneme;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img=(ImageView) findViewById(R.id.pic);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.p26);
Mat imgToProcess=Utils.bitmapToMat(bmp);
//******
//right here I need to convert this imgToProcess to grayscale for future opencv processes
//******
Bitmap bmpOut = Bitmap.createBitmap(imgToProcess.cols(), imgToProcess.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(imgToProcess, bmpOut);
img.setImageBitmap(bmpOut);
}
}
推荐答案
添加以下code在code座:
Add the following code in your code block:
Imgproc.cvtColor(imgToProcess, imgToProcess, Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(imgToProcess, imgToProcess, Imgproc.COLOR_GRAY2RGBA, 4);
或者,你也可以自己访问像素:
Or you can access pixels by yourself:
for(int i=0;i<imgToProcess.height();i++){
for(int j=0;j<imgToProcess.width();j++){
double y = 0.3 * imgToProcess.get(i, j)[0] + 0.59 * imgToProcess.get(i, j)[1] + 0.11 * imgToProcess.get(i, j)[2];
imgToProcess.put(i, j, new double[]{y, y, y, 255});
}
}
这篇关于Android的OpenCV的转换垫灰度使用matToBitmap / bitmapToMat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文