Java 灰度到二进制?
我正在尝试将灰度图像转换为二进制图像。我的代码将位图转换为灰度,但我不知道如何将该图像转换为二进制。这是我目前拥有的代码。请帮帮我!!!谢谢Java 灰度到二进制?,java,android,eclipse,binary,grayscale,Java,Android,Eclipse,Binary,Grayscale,我正在尝试将灰度图像转换为二进制图像。我的代码将位图转换为灰度,但我不知道如何将该图像转换为二进制。这是我目前拥有的代码。请帮帮我!!!谢谢 package com.example.vanderbilt; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android
package com.example.vanderbilt;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
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;
公共类二进制扩展活动{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.binary);
ImageView img = (ImageView) findViewById(R.id.ivBinary);
/*
* Bitmap bmp = BitmapFactory.decodeResource(getResources(),
* R.drawable.sample4); Bitmap bmpGray = toGrayscale(bmp);
* img.setImageBitmap(bmpGray);
*/
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.sample4);
Mat imgToProcess = Utils.bitmapToMat(bmp);
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 });
}
}
int widthM, heightM, rowsM, colsM;
colsM = imgToProcess.cols();
rowsM = imgToProcess.rows();
heightM = imgToProcess.height();
widthM = imgToProcess.width();
Bitmap bmpOut = Bitmap.createBitmap(imgToProcess.cols(),
imgToProcess.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(imgToProcess, bmpOut);
img.setImageBitmap(bmpOut);
}
public Bitmap toGrayscale(Bitmap bmpOriginal) {
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,
Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.binary);
ImageView img=(ImageView)findViewById(R.id.ivBinary);
/*
*位图bmp=BitmapFactory.decodeResource(getResources(),
*R.drawable.sample4);位图bmpGray=toGrayscale(bmp);
*img.setImageBitmap(bmpGray);
*/
位图bmp=BitmapFactory.decodeResource(getResources(),
R.可抽取样本4);
Mat imgToProcess=Utils.bitmapToMat(bmp);
对于(int i=0;i
}我以前用android做过一些图像过滤,对于ThresholdingFilter,我使用了以下代码:
public class ThresholdingFilter {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ METHODS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* Methods that apply a Thresholding Effect on image
*
* @param imageIn the input image
* @param threshold Integer - value (0-255) prefered value (threshold = 125)
* @return
*/
public static AndroidImage process(AndroidImage imageIn, int threshold) {
// The Resulting image
AndroidImage imageOut;
// Initiate the Output image
imageOut = new AndroidImage(imageIn.getImage());
// Do Threshold process
for(int y=0; y<imageIn.getHeight(); y++){
for(int x=0; x<imageIn.getWidth(); x++){
if(imageOut.getRComponent(x,y) < threshold){
imageOut.setPixelColor(x, y, 0,0,0);
}
else{
imageOut.setPixelColor(x, y, 255,255,255);
}
}
}
// Return final image
return imageOut;
}
}
公共类阈值过滤器{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~方法~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
*对图像应用阈值效果的方法
*
*输入图像中的@param image
*@param threshold Integer-值(0-255)首选值(阈值=125)
*@返回
*/
公共静态AndroidImage进程(AndroidImage imageIn,int阈值){
//生成的图像
AndroidImageOut;
//启动输出映像
imageOut=newandroidimage(imageIn.getImage());
//做阈值处理
对于(int y=0;y 8)&0xff;
b=彩色数组[索引]&0xff;
colorArray[索引]=0xff000000|(r>8;
}
/**
*方法获取指定像素的蓝色
*
*@param x
*@param y
*@返回B组件的颜色
*/
公共整型getb组件(整型x,整型y){
返回(getColorArray()[((y*width+x))]&0x000000FF);
}
/**
*方法将图像旋转指定的度数
*
*@param rotateDegrees
*/
公共空心旋转(整数旋转格律){
矩阵mtx=新矩阵();
mtx.后旋转(旋转度);
image=Bitmap.createBitmap(图像,0,0,宽度,高度,mtx,true);
宽度=image.getWidth();
高度=image.getHeight();
updateColorary();
}
//二传手和接球手
/**
*@返回图像
*/
公共位图getImage(){
返回图像;
}
/**
*@param image要设置的图像
*/
公共void setImage(位图图像){
这个图像=图像;
}
/**
*@返回格式化名称
*/
公共字符串getFormatName(){
返回格式化名称;
}
/**
*@param formatName要设置的formatName
*/
public void setFormatName(字符串formatName){
this.formatName=formatName;
}
/**
*@返回宽度
*/
公共int getWidth(){
返回宽度;
}
/**
*@param width要设置的宽度
*/
公共void setWidth(int-width){
这个。宽度=宽度;
}
/**
*@返回高度
*/
公共整数getHeight(){
返回高度;
}
/**
*@param height要设置的高度
*/
公共空间设置高度(内部高度){
高度=高度;
}
/**
*@返回颜色数组
*/
public int[]getColorArray(){
返回彩色数组;
}
/**
*@param colorArray要设置的colorArray
*/
公共void setColorArray(int[]colorArray){
this.colorArray=colorArray;
}
}
希望这将对您有所帮助您的意思是需要阈值算法吗?请参阅中的文章。是的,我需要对灰度图像进行阈值设置。您知道我可以使用哪些代码吗?我将使用这些代码选择最佳阈值。还有一个优化,您可以将图片分成4个部分,直到每个部分的阈值质量都很高够了。我使用了以下代码:Imgproc.threshold(imgToProcess,imgToProcess,2000255,Imgproc.THRESH_二进制);但结果只显示了一个黑色页面。发生了什么?互联网上有足够多的示例。例如,应该这样做。我将第一个代码放在binary.java中,对于第二个代码,我创建了一个新类并插入了它。但是我不断收到AndroidImage的错误。我该怎么办?@Young HunKim你得到了什么错误,使用两个类as单独的类,并在您的活动上调用它们。使用AsynchronousOutTask进行筛选,因为这是一个很长的操作!!如果您现在遇到问题,请告诉我,我使用的是Imgproc.threshold(imgToProcess,imgToProcess,100,2
public class AndroidImage {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ATTRIBUTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Original bitmap image
private Bitmap image;
// Format of the image (jpg/png)
private String formatName;
/** Dimensions of image */
// Width of the image
private int width;
// Height of the image
private int height;
// RGB Array Color
protected int[] colorArray;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ METHODS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* Constructor
*
* @param img the Bitmap from which we create our AndroidImage Object
*/
public AndroidImage(Bitmap img){
this.image = img;
this.formatName = "jpg";
this.width = img.getWidth();
this.height = img.getHeight();
updateColorArray();
}
/**
* Method to reset the image to a solid color
*
* @param color - color to reset the entire image to
*/
public void clearImage(int color){
for(int y=0; y<height; y++){
for(int x=0; x<width; x++){
image.setPixel(x, y, color);
}
}
}
/**
* Method to set color array for image - called on initialization by constructor
*
*
*/
private void updateColorArray(){
colorArray = new int[width * height];
image.getPixels(colorArray, 0, width, 0, 0, width, height);
int r, g, b;
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
int index = y * width + x;
r = (colorArray[index] >> 16) & 0xff;
g = (colorArray[index] >> 8) & 0xff;
b = colorArray[index] & 0xff;
colorArray[index] = 0xff000000 | (r << 16) | (g << 8) | b;
}
}
}
/**
* Method to set the color of a specific pixel
*
* @param x
* @param y
* @param color
*/
public void setPixelColor(int x, int y, int color){
colorArray[((y*image.getWidth()+x))] = color;
image.setPixel(x, y, color);
}
/**
* Method to get the color of a specified pixel
*
* @param x
* @param y
* @return color
*/
public int getPixelColor(int x, int y){
return colorArray[y*width+x];
}
/**
* Method to set the color of a specified pixel from an RGB combination
*
* @param x
* @param y
* @param c0
* @param c1
* @param c2
*/
public void setPixelColor(int x, int y, int c0, int c1, int c2){
colorArray[((y*image.getWidth()+x))] = (255 << 24) + (c0 << 16) + (c1 << 8) + c2;
image.setPixel(x, y, colorArray[((y*image.getWidth()+x))]);
}
/**
* Method to get the RED color of a specified pixel
*
* @param x
* @param y
* @return color of R component
*/
public int getRComponent(int x, int y){
return (getColorArray()[((y*width+x))]& 0x00FF0000) >>> 16;
}
/**
* Method to get the GREEN color of a specified pixel
*
* @param x
* @param y
* @return color of G component
*/
public int getGComponent(int x, int y){
return (getColorArray()[((y*width+x))]& 0x0000FF00) >>> 8;
}
/**
* Method to get the BLUE color of a specified pixel
*
* @param x
* @param y
* @return color of B component
*/
public int getBComponent(int x, int y){
return (getColorArray()[((y*width+x))] & 0x000000FF);
}
/**
* Method to rotate an image by the specified number of degrees
*
* @param rotateDegrees
*/
public void rotate (int rotateDegrees){
Matrix mtx = new Matrix();
mtx.postRotate(rotateDegrees);
image = Bitmap.createBitmap(image, 0, 0, width, height, mtx, true);
width = image.getWidth();
height = image.getHeight();
updateColorArray();
}
// Setters and Getters
/**
* @return the image
*/
public Bitmap getImage() {
return image;
}
/**
* @param image the image to set
*/
public void setImage(Bitmap image) {
this.image = image;
}
/**
* @return the formatName
*/
public String getFormatName() {
return formatName;
}
/**
* @param formatName the formatName to set
*/
public void setFormatName(String formatName) {
this.formatName = formatName;
}
/**
* @return the width
*/
public int getWidth() {
return width;
}
/**
* @param width the width to set
*/
public void setWidth(int width) {
this.width = width;
}
/**
* @return the height
*/
public int getHeight() {
return height;
}
/**
* @param height the height to set
*/
public void setHeight(int height) {
this.height = height;
}
/**
* @return the colorArray
*/
public int[] getColorArray() {
return colorArray;
}
/**
* @param colorArray the colorArray to set
*/
public void setColorArray(int[] colorArray) {
this.colorArray = colorArray;
}
}