我需要将位图转换为二进制图像以完成我的作业。你知道这方面的任何知识吗?
我需要将位图转换为二进制图像以完成我的作业。你知道这方面的任何知识吗?
你是否正在寻找一个算法来执行转换?
最简单的方法是将每个像素值与固定阈值进行比较:如果像素值小于阈值,则相应的输出像素为黑色(0),否则为白色(1)。
如果您希望自动确定阈值,您可以实现Otsu的方法。当您不能对图像中的像素分布做出太多假设时,该方法总体上能够正确地完成工作。
http://en.wikipedia.org/wiki/Otsu%27s_Method
作为参考,以下是在Mathematica中的样子:Binarize[image, threshold]
,对于Otsu方法使用Binarize[img]
。
Bitmap bitmapObtained =//get your bitmap
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmapObtained.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
也许这是你的代码
imageID = cursor.getString(columnIndex);
// uri = Uri.withAppendedPath(Media.EXTERNAL_CONTENT_URI, "" + imageID);
Log.v("dklfdlk",imageID);
bitmap = BitmapFactory.decodeFile(imageID);
if (bitmap != null) {
newBitmap = Bitmap.createScaledBitmap(bitmap, 78, 78, true);
bitmap.recycle();
if (newBitmap != null) {
publishProgress(new LoadedImage(newBitmap));
}
试试这个