Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8167917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:21:22+00:00 2026-06-06T20:21:22+00:00

I’m trying to convert a grayscale image to a binary. The code I have

  • 0

I’m trying to convert a grayscale image to a binary. The code I have converts a bitmap to grayscale, but I’m at loss as to how to make that image into a binary. Here is the code I currently have. PLEASE HELP ME!!! Thanks!

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;

public class Binary extends Activity {

@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;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:21:23+00:00Added an answer on June 6, 2026 at 8:21 pm

    I have done some image filtering in the past with android, for the ThresholdingFilter i used this code:

    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;
        }
    
    }
    

    AndroidImage is a Custom Wrapper class for the Android Bitmap

    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;
        }
    
    }
    

    Hope this will help you

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.