Totalllyrandomguy
Totalllyrandomguy

Reputation: 149

java how to get a boolean array from a black and white image

I have this image that I made in ms paint that is 106x17 and I want to turn the entire bitmap into a number. The image itself is stored as a .png and I need a way to read the image and store each pixel as a bit in a BigInteger. The way I need the image to be read is fairly specific and a little weird... The image needs to be read in lines from top to bottom going from right to left... so the upper right hand pixel should be the first bit in the number and the bottom left most pixel should be the last bit in the number.

EDIT: I should probably clarify a little bit, Since the file is stored as a .png I cant just read it as a number, I will try and export it to a bitmap image right after I post this update. Also I am storing it in a BigInteger because the number should be 106x17= 1802 bits long, so the number cannot be passed through an int or long first because it will loose most of the information. And lastly, in this context a black pixel represents a 1 and a white pixel represents a 0... sorry for the strange conventions but that is more or less what I'm working with.

Upvotes: 1

Views: 1520

Answers (4)

Shrikant Havale
Shrikant Havale

Reputation: 1290

There is Java API specifically for Image Processing ImageJ, here is the link where you can download necessary Jar, http://imagej.nih.gov/ij/download.html and documentation link http://imagej.nih.gov/ij/docs/index.html

There are several tutorials and example available to do basic operations on images using this API, I will try to write basic code for your task

    // list of points
    List<Point> pointList = new ArrayList<>();

    ImagePlus imp = IJ.openImage("/path/to/image.tif"); 
    ImageProcessor imageProcessor = imp.getProcessor(); 

    // width and height of image
    int width = imageProcessor.getWidth();
    int height = imageProcessor.getHeight();

    // iterate through width and then through height
    for (int u = 0; u < width; u++) {
        for (int v = 0; v < height; v++) {
            int valuePixel = imageProcessor.getPixel(u, v);
            if (valuePixel > 0) {
                pointList.add(new Point(u, v));
            }
        }
    }

    // convert list to array
    pointList.toArray(new Point[pointList.size()]);

here is one more link for more examples,http://albert.rierol.net/imagej_programming_tutorials.html#ImageJ

Upvotes: 0

user4668606
user4668606

Reputation:

BufferedImage bi = yourImage;

//the number of bytes required to store all bits
double size = ((double) bi.getWidth()) * ((double) bi.getHeight()) / 8;
int tmp = (int) size;
if(tmp < size)
    tmp += 1;

byte[] b = new byte[tmp];
int bitPos = 7;
int ind = 0;

for(int i = 0 ; i < bi.getHeight() ; i++)
     for(int j = 0 ; j < bi.getWidth() ; j++){
         //add a 1 at the matching position in b, if this pixel isn't black
         b[ind] |= (bi.getRgb(j , i) > 0 ? 0 : (1 << bitPos));

         //next pixel -> next bit
         bitPos -= 1;
         if(bitPos == -1){//the current byte is filled with continue with the next byte
             bitPos = 7;
             ind++;
         }
     }

BigInteger result = new BigInteger(b);

Upvotes: 1

candied_orange
candied_orange

Reputation: 7344

The bitmap is already a number. Just open it and read it.

image = ImageIO.read(getClass().getResourceAsStream("path/to/your/file.bmp"));
int color = image.getRGB(x, y);
BigInteger biColor = BigInteger.valueOf(color); 

Upvotes: 0

Juan
Juan

Reputation: 1794

If you want something very simple and black and white, maybe you can export the image in .pbm format and work with it as a text file, or if you export it as .ppm you might not even have to process it depending on what you want.
Have a look at these formats

Upvotes: 0

Related Questions