![](/img/trans.png)
[英]Grayscale image classification with neural network on Android with OpenCV
[英]Android with opencv - image to grayscale
我正在寻找Android中简单的应用示例,该示例使用opencv将彩色图像转换为灰度图像。 我正在尝试使用thic代码,但此行后应用程序崩溃Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U);
。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img=(ImageView)findViewById(R.id.imageView1);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U);
Utils.bitmapToMat(bmp, tmp);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY,4);
Utils.matToBitmap(tmp, bmp);
img.setImageBitmap(bmp);
}
真正快捷的方法。 没有opencv。
private Button button;
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
image = (ImageView)findViewById(R.id.imageView1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
image.setColorFilter(filter);
}
});
我只是尝试过,为我工作。
编辑:在此处查看官方文档链接。 http://developer.android.com/reference/android/graphics/ColorMatrix.html#setSaturation(float)
当您处于onCreate()
,尚未布置您的布局,这就是为什么bmp.getWidth()
和bmp.getHeight()
返回0的原因。
一种方法是重写onWindowFocusChanged()
private ImageView img;
private boolean isFullyInitialized = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.imageView1);
}
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
if (hasFocus && !isFullyInitialized)
{
isFullyInitialized = true;
openCvCode();
}
}
private void openCvCode()
{
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Mat tmp = new Mat(bmp.getWidth(), bmp.getHeight(), CvType.CV_8U);
Utils.bitmapToMat(bmp, tmp);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY);
Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2GRAY,4);
Utils.matToBitmap(tmp, bmp);
img.setImageBitmap(bmp);
}
问题解决了:
public class MainActivity extends Activity {
ImageView img;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.imageView1);
bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.lena);
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
{
Log.e("TEST", "Cannot connect to OpenCV Manager");
}
}
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Mat tmp = new Mat(bitmap.getWidth(), bitmap.getHeight(),CvType.CV_8UC1);
// Convert
Utils.bitmapToMat(bitmap, tmp);
Mat gray = new Mat(bitmap.getWidth(), bitmap.getHeight(),CvType.CV_8UC1);
// Conver the color
Imgproc.cvtColor(tmp, gray, Imgproc.COLOR_RGB2GRAY);
// Convert back to bitmap
Mat destination = new Mat(gray.rows(),gray.cols(),gray.type());
Imgproc.adaptiveThreshold(gray, destination, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 15, 4);
Utils.matToBitmap(destination, bitmap);
img.setImageBitmap(bitmap);
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.