In my program I am reading an image using java, taking its byte array and applying modifications to it by applying random functions. Now during the entire process the size of array is maintained but when I am trying to construct an image using the byte array an empty image is obtained. I have tried using both java and opencv to reconstruct the image (the image is grayscale image of 440*442 size .png type).
opencv code:
byte[] bytesArrayForImage=new byte[array.length];
for (int i=0; i<array.length; i++) {
bytesArrayForImage[i] = (byte) Integer.parseInt(array[i]);
}
Mat mat = new Mat(440,442, CvType.CV_8UC1);
mat.put(0,0, bytesArrayForImage);
String filename111 = Path;
Highgui.imwrite(filename111, mat);
java code:
BufferedImage image=ImageIO.read(new ByteArrayInputStream(bytearray));
ImageIO.write(image, "png", new File(C:\\Users\\domin\\Desktop\\Sop\\,"modified.png"));
Related
This question already has answers here:
Java - get pixel array from image
(7 answers)
Closed 4 years ago.
I want to read in a bitmap so that I have an array of the 256-RGB values for each pixel.
Currently my code is:
File image = serverConfig.get(map.bmp);
BufferedImage buffer = ImageIO.read(image);
dimX = buffer.getWidth();
dimY = buffer.getHeight();
byte[] pixlesB = (byte[]) buffer.getRaster().getDataElements(0, 0, buffer.getWidth(), buffer.getHeight(), null);
This produces a byte array of the bitmap e.g.
[pixel1Red,pixel1Green,pixel1Blue,pixel2Red,pixel2Green,pixel2Blue,...]
My problem is that when I load a large bitmap (currently the one I'm trying is about 706,000 pixles^2) the bitmap lossless compresses the file and I just get a string of semi-meaningless numbers.
Is there any way to force java to read out the RGB values for all bitmaps, like it does for small ones?
EDIT:
To clarify, I am getting back a [pixel1Red,pixel1Green,pixel1Blue,pixel2Red,pixel2Green,pixel2Blue,...] style list, but the values in there aren't the 0-255 bytes I'm expecting, they're just random, compressed numbers. I need to actual 0-255 values for RGB (or better yet just a byte array of all the hex values) in order for the rest of my code to reliably work.
Try this method:
File image = serverConfig.get(map.bmp);
BufferedImage buffer = ImageIO.read(image);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffer, "bmp", baos );
baos.flush();
byte[] pixlesB = baos.toByteArray();
baos.close();
I need to compress a tif file that has several gray 16bit images (multi-page). I have tried working with ImageIO as here: Tiff compression using Java ImageIO Initially, each image that will be in the tif file comes from another tiff file. When I want to use the compressors, I have the following options:
CCITT RLE, CCITT T.4, CCITT T.6: They give me the error: "javax.imageio.IIOException: I/O error writing TIFF file!"
LZW. I cannot use it. My images are 16bit and LZW increases the size of 16bit images
JPEG. Not possible for 16bit images.
ZLIB. It only reduces 10% even if I specify setCompressionQuality(0.0f);
PackBits. Does not compress.
Deflate. Like ZLIB.
EXIF JPEG. It gives me the error: "javax.imageio.IIOException: Old JPEG compression not supported!"
Does any know any other alternative? I saw an apache imaging library but the tif compression only support the above or less options. Does anyone know about JPEG2000 compressor? Any other kind of alternative?
PNG compresses 16-bit images losslessly. Libraries and utilities are widely available. JPEG2000 has a lossy 16-bit mode, but you'd have to find some software that supports it. Open JPEG might.
However I'd have to ask: what are your criteria for when you have acceptable image quality and when you do not? If it is visual, then you likely end up at normal JPEG anyway, with a good bit less than 8 bits per pixel effective.
Reducing the image 16 bit to 8 bit. Consider that you have a byte[] variable plane16 where you have all the pixels of your image.
Note: My byte[] plane16 gets the data from a 16bit image but byte is 8bit=1byte. Therefore, 2 elements in row of this array are 2byte = 16bit. That is why I convert it to a short[] before operating. If you start from a short[], ommit "ByteBuffer.wrap(plane16).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);"
byte[] plane16; //Fill it with your image!!!
//Do stuff with imageIO. Set writer and compresion method
ImageIO.scanForPlugins();
TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi();
javax.imageio.ImageWriter writerIO = tiffspi.createWriterInstance();
ImageWriteParam param = writerIO.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("ZLib");
param.setCompressionQuality(0.5f);
File fOutputFile = new File(route+".tif");
ImageOutputStream ios = ImageIO.createImageOutputStream(fOutputFile);
writerIO.setOutput(ios);
//Reducing 16bit to 8bit
short[] shorts = new short[plane16.length/2];
ByteBuffer.wrap(plane16).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
int max = 0;
int min = 999999;
for (int v = 0; v<shorts.length;v++){
if (max<shorts[v]) max = shorts[v];
if (min>shorts[v]) min = shorts[v];
}
double range = 255./(max-min);
byte[] plane8 = new byte[shorts.length];
for (int v = 0; v<plane8.length;v++){
plane8[v] = (byte) ((shorts[v]+min)*range - 128);
}
//16bit:
/*BufferedImage convertedGrayscale = new BufferedImage(width,
heigth, BufferedImage.TYPE_USHORT_GRAY);
convertedGrayscale.getRaster().setDataElements(0, 0, width,
heigth, shorts);*/
//8bit:
BufferedImage convertedGrayscale = new BufferedImage(width,
heigth, BufferedImage.TYPE_BYTE_GRAY);
convertedGrayscale.getRaster().setDataElements(0, 0, width,
heigth, plane8);
//Save image
//If you have a stack of images in tiff, do this trick. "image" is the image number you are setting inside the tiff. If you only have 1 image, remove the if and take the expression from the else.
if (image!=0){
writerIO.writeInsert(image, new IIOImage(convertedGrayscale, null, null), param);
}else{
writerIO.write(null, new IIOImage(convertedGrayscale, null, null), param);
}
//do the next only after the last image to be saved
writerIO.dispose();
ios.flush();
ios.close();
I got a function where i process different arrays[][] and I need to use a Mat because I'm working with images, so I need the pixels of that image.
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat img = Imgcodecs.imread(imagelocation);
I need a way to get the pixels values of that Mat.
But I was wondering if I can convert an array[][] into a Mat.
I hope you understand and help me.
To get the pixel values of the Mat you can use: reference
Mat m;
int bufferSize = m.channels()*m.cols()*m.rows();
byte [] b = new byte[bufferSize];
m.get(0,0,b); // get all the pixels
double pixelValue = m.get(0, 0) // get pixel at row 0, column 0
To convert an array to Mat:
byte[] raw_data = ...;
Mat mat = new Mat();
mat.put(0, 0, raw_data);
I m trying to develop an app which would auto crop the edges of a card which is detected by using opencv.
Here in this function i am doing the following :
1) Converting the byte array (ie data) into a bitmap and then to a Mat
2) Cropping the required portion of the card and re sizing it
3) Converting the mat back into a byte array
4) Saving the modified byte array in a File(jpg)
#Override
public void onPictureTaken(byte[] data, Camera camera) {
Mat mat=new Mat();
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
Utils.bitmapToMat(bmp, mat); //converting a mat to bitmap
Mat matCrop = mat.submat((int)p1.y,(int)p2.y,(int)p3.x,(int)p4.x);//surely no issues here
Imgproc.resize(matCrop, matCrop, mat.size());
byte[] imageInBytes = new byte[(int)(matCrop.total() * matCrop.channels())];
mat.get(0, 0, imageInBytes);
data=imageInBytes;
try {
FileOutputStream fos = new FileOutputStream(mPictureFileName);
fos.write(data);
fos.close();
} catch (java.io.IOException e) {}
}
The picture gets saved in memory but the preview as well as the image both are blank . The saved file also takes about 3.5 Mb , What could be the reason for this ?
Thanks in advance.
I am trying to create a helper function using OpenCV Java API that would process an input image and return the output byte array. The input image is a jpg file saved in the computer. The input and output image are displayed in the Java UI using Swing.
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// Load image from file
Mat rgba = Highgui.imread(filePath);
Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGB2GRAY, 0);
// Convert back to byte[] and return
byte[] return_buff = new byte[(int) (rgba.total() * rgba.channels())];
rgba.get(0, 0, return_buff);
return return_buff;
When the return_buff is returned and converted to BufferedImage I get NULL back. When I comment out the Imgproc.cvtColor function, the return_buff is properly converted to a BufferedImage that I can display. It seems like the Imgproc.cvtColor is returning a Mat object that I couldn't display in Java.
Here's my code to convert from byte[] to BufferedImage:
InputStream in = new ByteArrayInputStream(inputByteArray);
BufferedImage outputImage = ImageIO.read(in);
In above code, outputImage is NULL
Does anybody have any suggestions or ideas?
ImageIO.read(...) (and the javax.imageio package in general) is for reading/writing images from/to file formats. What you have is an array containing "raw" pixels. It's impossible for ImageIO to determine file format from this byte array. Because of this, it will return null.
Instead, you should create a BufferedImage from the bytes directly. I don't know OpenCV that well, but I'm assuming that the result of Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGB2GRAY, 0) will be an image in grayscale (8 bits/sample, 1 sample/pixel). This is the same format as BufferedImage.TYPE_BYTE_GRAY. If this assumption is correct, you should be able to do:
// Read image to Mat as before
Mat rgba = ...;
Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGB2GRAY, 0);
// Create an empty image in matching format
BufferedImage gray = new BufferedImage(rgba.width(), rgba.height(), BufferedImage.TYPE_BYTE_GRAY);
// Get the BufferedImage's backing array and copy the pixels directly into it
byte[] data = ((DataBufferByte) gray.getRaster().getDataBuffer()).getData();
rgba.get(0, 0, data);
Doing it this way, saves you one large byte array allocation and one byte array copy as a bonus. :-)
I used this kind of code to convert Mat object to Buffered Image.
static BufferedImage Mat2BufferedImage(Mat matrix)throws Exception {
MatOfByte mob=new MatOfByte();
Imgcodecs.imencode(".jpg", matrix, mob);
byte ba[]=mob.toArray();
BufferedImage bi=ImageIO.read(new ByteArrayInputStream(ba));
return bi;
}