将图像转换为黑白(非灰度)
Transforming an image to black and white (not grayscale)
有没有办法对 Xamarin.Forms 或 Xamarin.Android 中的图像进行阈值处理?
我想对 android 中用相机拍摄的每张照片进行阈值处理(一次一张),目前我正在使用 xlabs 进行相机拍摄。
我进行了搜索,但没有找到任何有用的信息。也许你们可以帮助我。 :)
因此,您可以使用 OpenCV Library for Android。
- 下载from here(例如)版本 3.0.0
- 现在您需要添加 Android Java Binding Library into your Xamarin.Android project(also you can read this great article)
- 当Android Java Binding Library添加到你的项目时,你需要添加.jar ,这是从 OpenCV SDK 通过生成的 .jar 的 documentation(but i've made this for you, here is link 生成的)。
- 在你的 Android Java 绑定库 -> 文件夹(Jars) 添加现有文件(来自步骤 3 的 .jar),还需要将 共享对象 添加到 Jar 文件夹(来自 YourPathWhereIsStoreSDK/OpenCV-android-sdk/sdk/native/libs/armeabi...等).
结果必须与此类似:
- 现在,在您的 Xamarin.Android 项目中,您必须引用 Android Java 绑定库 项目。
- 请参阅下面的代码,了解如何使用此库 :)。
在您的启动画面 activity(或 MainActivity)中添加:
//beware,that we need to INIT Only once,so better to put in SplashActivity
public MainActivity()
{
if (!OpenCVLoader.InitDebug())
{
System.Console.WriteLine("Failed to INIT \n OpenCV Failure");
}
else
{
System.Console.WriteLine("OpenCV INIT Succes");
}
}
现在我们可以将图像转换为 b/w:
void ConvertImageToBW()
{
Mat MatToBW = new Mat();
using (Bitmap SourceImg = Bitmap.CreateBitmap(BitmapFactory.DecodeFile(App._file.Path))) //App._file.Path - path of image that we have made by camera
{
if (SourceImg != null)
{
Utils.BitmapToMat(SourceImg,MatToBW);
//first we convert to grayscale
Imgproc.CvtColor(MatToBW, MatToBW, Imgproc.ColorRgb2gray);
//now converting to b/w
Imgproc.Threshold(MatToBW, MatToBW, 0.5 * 255, 255, Imgproc.ThreshBinary);
using(Bitmap newBitmap = Bitmap.CreateBitmap(MatToBW.Cols(),MatToBW.Rows(),Bitmap.Config.Argb8888))
{
Utils.MatToBitmap(MatToBW, newBitmap);
//put result in your imageView, B/W Image
imageView.SetImageBitmap(newBitmap);
}
}
}
}
现在只需将此方法用于您的 OnActivityResult。
享受吧!
有没有办法对 Xamarin.Forms 或 Xamarin.Android 中的图像进行阈值处理?
我想对 android 中用相机拍摄的每张照片进行阈值处理(一次一张),目前我正在使用 xlabs 进行相机拍摄。
我进行了搜索,但没有找到任何有用的信息。也许你们可以帮助我。 :)
因此,您可以使用 OpenCV Library for Android。
- 下载from here(例如)版本 3.0.0
- 现在您需要添加 Android Java Binding Library into your Xamarin.Android project(also you can read this great article)
- 当Android Java Binding Library添加到你的项目时,你需要添加.jar ,这是从 OpenCV SDK 通过生成的 .jar 的 documentation(but i've made this for you, here is link 生成的)。
- 在你的 Android Java 绑定库 -> 文件夹(Jars) 添加现有文件(来自步骤 3 的 .jar),还需要将 共享对象 添加到 Jar 文件夹(来自 YourPathWhereIsStoreSDK/OpenCV-android-sdk/sdk/native/libs/armeabi...等).
结果必须与此类似:
- 现在,在您的 Xamarin.Android 项目中,您必须引用 Android Java 绑定库 项目。
- 请参阅下面的代码,了解如何使用此库 :)。
在您的启动画面 activity(或 MainActivity)中添加:
//beware,that we need to INIT Only once,so better to put in SplashActivity
public MainActivity()
{
if (!OpenCVLoader.InitDebug())
{
System.Console.WriteLine("Failed to INIT \n OpenCV Failure");
}
else
{
System.Console.WriteLine("OpenCV INIT Succes");
}
}
现在我们可以将图像转换为 b/w:
void ConvertImageToBW()
{
Mat MatToBW = new Mat();
using (Bitmap SourceImg = Bitmap.CreateBitmap(BitmapFactory.DecodeFile(App._file.Path))) //App._file.Path - path of image that we have made by camera
{
if (SourceImg != null)
{
Utils.BitmapToMat(SourceImg,MatToBW);
//first we convert to grayscale
Imgproc.CvtColor(MatToBW, MatToBW, Imgproc.ColorRgb2gray);
//now converting to b/w
Imgproc.Threshold(MatToBW, MatToBW, 0.5 * 255, 255, Imgproc.ThreshBinary);
using(Bitmap newBitmap = Bitmap.CreateBitmap(MatToBW.Cols(),MatToBW.Rows(),Bitmap.Config.Argb8888))
{
Utils.MatToBitmap(MatToBW, newBitmap);
//put result in your imageView, B/W Image
imageView.SetImageBitmap(newBitmap);
}
}
}
}
现在只需将此方法用于您的 OnActivityResult。
享受吧!