when I am trying to convert BufferedImage to Mat using this function.
public Mat matify(BufferedImage im) {
// Convert INT to BYTE
//im = new BufferedImage(im.getWidth(), im.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
// Convert bufferedimage to byte array
byte[] pixels = ((DataBufferByte) im.getRaster().getDataBuffer())
.getData();
// Create a Matrix the same size of image
Mat image = new Mat(im.getHeight(), im.getWidth(), CvType.CV_8UC3);
// Fill Matrix with image values
image.put(0, 0, pixels);
return image;
}
I get this error.
Exception in thread "main" java.lang.UnsupportedOperationException: Provided data element number (7955216) should be multiple of the Mat channels count (3)
at org.opencv.core.Mat.put(Mat.java:2549)
at Main.matify(Main.java:78)
at Main.doOpenCV(Main.java:48)
at Main.main(Main.java:40)
the error is caused by this line
image.put(0,0,pixels);
so why I am getting this error ? How can I convert BufferedImage to opencv Mat in java ?
This is what worked for me
public Mat bufferedImageToMat(BufferedImage bi) {
Mat mat = new Mat(bi.getHeight(), bi.getWidth(), CV_8UC(3));
int r, g, b;
UByteRawIndexer indexer = mat.createIndexer();
for (int y = 0; y < bi.getHeight(); y++) {
for (int x = 0; x < bi.getWidth(); x++) {
int rgb = bi.getRGB(x, y);
r = (byte) ((rgb >> 0) & 0xFF);
g = (byte) ((rgb >> 8) & 0xFF);
b = (byte) ((rgb >> 16) & 0xFF);
indexer.put(y, x, 0, r);
indexer.put(y, x, 1, g);
indexer.put(y, x, 2, b);
}
}
indexer.release();
return mat;
}
Related
I'm using JavaFX and I have a 16bit grayscale image that I'm able to show with the following code:
public Image createNewImageFromArrayPixels(int[][] pixels) {
int width = pixels.length;
int height = pixels[0].length;
int[] buffer = from2DArrayTo1DArray(pixels);
ColorModel colorModel = new ComponentColorModel(
ColorSpace.getInstance(ColorSpace.CS_GRAY), //we have grayscale images
new int[]{16}, //bit depth: 16 bit per pixel
false,
false,
Transparency.OPAQUE,
DataBuffer.TYPE_INT); //every pixel has an integer intensity
WritableRaster raster = colorModel.createCompatibleWritableRaster(height, width);
DataBufferInt buff = (DataBufferInt) raster.getDataBuffer();
int[] bufferData = buff.getData();
System.arraycopy(buffer, 0, bufferData, 0, (buffer.length < bufferData.length ? buffer.length : bufferData.length));
BufferedImage bm = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
Image img = SwingFXUtils.toFXImage(bm, null);
return img;
}
Now I want to transform it in a 48bit RGB image (16bit per channel), but I can't understand how.
At the moment I'm able to create a RGB 24bit image (8 bit per channel) by taking the int value of a pixel from the grayscale image and building manually the bytes that will compose the pixels with the following code:
private Image createRGBImageFromGrayPixels(int[][] pixels) {
int width = pixels.length;
int height = pixels[0].length;
BufferedImage bm = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int converted = from16to8(pixels[y][x]);
System.out.println("original " + pixels[y][x] + " = " + converted + " = " + from8to16(converted));
int rgb = converted;
rgb = (rgb << 8) + converted;
rgb = (rgb << 8) + converted;
bm.setRGB(x, y, rgb);
}
}
Image img = SwingFXUtils.toFXImage(bm, null);
return img;
}
private int from16to8(int value) {
int originalStart = 0;
int originalEnd = 65535;
int newStart = 0;
int newEnd = 255;
double scale = (double)(newEnd - newStart) / (originalEnd - originalStart);
double res = newStart + ((value - originalStart) * scale);
System.out.println(res);
int result = Math.round(Math.round(res));
return result;
}
Similarly, I'm also able to create the same structure for 16bits obtaining the desired rgb value, but it is a long and is not accepted by the setRGB() method, so I'm not able to create the bufferedImage, whose type I can't specify.
Can anyone help me with this final steps, please? Thank you in advance.
private Image createRGBImageFromGrayPixels_16(int[][] pixels) {
int width = pixels.length;
int height = pixels[0].length;
BufferedImage bm = new BufferedImage(width, height, BufferedImage...); //BufferedImage.TYPE ???
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
long rgb = pixels[y][x];
rgb = (rgb << 16) + pixels[y][x];
rgb = (rgb << 16) + pixels[y][x];
System.out.println(pixels[y][x] + "\t->\t" + rgb);
System.out.println();
bm.setRGB(x, y, rgb); //ERROR: long type for rgb is not accepted!
}
}
Image img = SwingFXUtils.toFXImage(bm, null);
return img;
}
If needed, I can also install external jar libraries (obviously, only if opersource) that can help me achieving this goal.
I got this error.
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
rgb2 cannot be resolved to a variable
its always the rgb2 array that caused the error. How to solve this problem?
BufferedImage img1 = ImageIO.read(file1);
BufferedImage img2 = ImageIO.read(file2);
int w = img1.getWidth();
int h = img1.getHeight();
long diff = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int rgb1[] = img1.getRGB(x, y, w, h, rgb1, 0, w);
int rgb2[]= img2.getRGB(x, y, w, h, rgb2, 0, w);
int index = y * w + x;
int r1 = (rgb1[index] >> 16) & 0xff;
int g1 = (rgb1[index] >> 8) & 0xff;
int b1 = (rgb1[index] ) & 0xff;
int r2 = (rgb2[index] >> 16) & 0xff;
int g2 = (rgb2[index]>> 8) & 0xff;
int b2 = (rgb2[index] ) & 0xff;
r2 += Math.abs(r2 - r1);
g2 += Math.abs(g2 - g1);
b2 += Math.abs(b2 - b1);
rgb2[index] = (((r2 & 0xff) << 16) + ((g2 & 0xff) << 8) +(b2 & 0xff));
rgb2[index] = (rgb2[index]*17);
}
}
int i = 0;
for (int y = 0; y < h; y++) {
int red = (y * 255) / (h - 1);
for (int x = 0; x < w; x++) {
int green = (x * 255) / (w - 1);
int blue = 128;
rgb2[i++] = (red << 16) | (green << 8) | blue;//the problem is at this line
}
}
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, w, h, rgb2, 0, w);
Graphics g = image.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
File imageFile = new File("saved.jpeg");
ImageIO.write(image, "jpg", imageFile);
}
I got this error after declare outside the loop.
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable rgb1 may not have been initialized
int w = img1.getWidth();
int h = img1.getHeight();
int scale = w * h * 3;
int rgb1[] = img1.getRGB(0, 0, w, h, rgb1, 0, w);
int rgb2[] = img2.getRGB(0, 0, w, h, rgb2, 0, w);
Your problem is because rgb2[] is declared inside a for loop immediately before, in this line:
int rgb2[]= img2.getRGB(x, y, w, h, rgb2, 0, w);
Then the for loop ends, so rgb2[] falls out of scope and is released from memory, and no longer defined. If you want rgb2[] to be accessible from outside the loop, you have to say int rgb2[]; before the loop is called, so that the variable is in the same scope as the line where it needs to be called. Remember, scope is inherited downwards -- anything accessible immediately before the loop is accessible inside it -- but not the other way around.
You declared your rgb2 variable inside your first for loop, which is not visible to your second for loop, where the problem occurs. To fix it, simply declare rgb2 array before your for loops.
This is how I am doing to convert from RGB to CMYK using the more "correct" way - i.e using an ICC color profile.
// Convert RGB to CMYK with level shift (minus 128)
private void RGB2CMYK(int[] rgb, float[][] C, float[][] M, float[][] Y, float[][] K, int imageWidth, int imageHeight) throws Exception {
ColorSpace instance = new ICC_ColorSpace(ICC_Profile.getInstance(JPEGWriter.class.getResourceAsStream(pathToCMYKProfile)));
float red, green, blue, cmyk[];
//
for(int i = 0, index = 0; i < imageHeight; i++) {
for(int j = 0; j < imageWidth; j++, index++) {
red = ((rgb[index] >> 16) & 0xff)/255.0f;
green = ((rgb[index] >> 8) & 0xff)/255.0f;
blue = (rgb[index] & 0xff)/255.0f;
cmyk = instance.fromRGB(new float[] {red, green, blue});
C[i][j] = cmyk[0]*255.0f - 128.0f;
M[i][j] = cmyk[1]*255.0f - 128.0f;
Y[i][j] = cmyk[2]*255.0f - 128.0f;
K[i][j] = cmyk[3]*255.0f - 128.0f;
}
}
}
My problem is: it's prohibitively slow given a large image. In one case, it took about 104s instead of the usual 2s for me to write the data as a JPEG image. It turns out the above transform is the most time-consuming part.
I am wondering if there is any way to make it faster. Note: I am not going to use the cheap conversion algorithm one can find form the web.
Update: following haraldK's suggestion, here is the revised version:
private void RGB2CMYK(int[] rgb, float[][] C, float[][] M, float[][] Y, float[][] K, int imageWidth, int imageHeight) throws Exception {
if(cmykColorSpace == null)
cmykColorSpace = new ICC_ColorSpace(ICC_Profile.getInstance(JPEGWriter.class.getResourceAsStream(pathToCMYKProfile)));
DataBuffer db = new DataBufferInt(rgb, rgb.length);
WritableRaster raster = Raster.createPackedRaster(db, imageWidth, imageHeight, imageWidth, new int[] {0x00ff0000, 0x0000ff00, 0x000000ff}, null);
ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorConvertOp cco = new ColorConvertOp(sRGB, cmykColorSpace, null);
WritableRaster cmykRaster = cco.filter(raster, null);
byte[] o = (byte[])cmykRaster.getDataElements(0, 0, imageWidth, imageHeight, null);
for(int i = 0, index = 0; i < imageHeight; i++) {
for(int j = 0; j < imageWidth; j++) {
C[i][j] = (o[index++]&0xff) - 128.0f;
M[i][j] = (o[index++]&0xff) - 128.0f;
Y[i][j] = (o[index++]&0xff) - 128.0f;
K[i][j] = (o[index++]&0xff) - 128.0f;
}
}
}
Update: I also found out it's much faster to do filter on a BufferedImage instead of a Raster. See this post: ARGB int array to CMYKA byte array convertion
You should get rid of the memory allocation within the innermost loop. new is a prohibitively expensive operation. Also it might kick the garbage collector into action, which adds a further penality.
If you can affort the memory consumption, you could create a lookup table:
private void RGB2CMYK(int[] rgb, float[][] C, float[][] M, float[][] Y, float[][] K, int imageWidth, int imageHeight) throws Exception {
ColorSpace cs = new ICC_ColorSpace(...);
int[] lookup = createRGB2CMYKLookup(cs);
for(int y = 0, index = 0; y < imageHeight; y++) {
for(int x = 0; x < imageWidth; x++, index++) {
int cmyk = lookup[rgb[index]];
C[y][x] = ((cmyk >> 24) & 255) - 128F;
M[y][x] = ((cmyk >> 16) & 255) - 128F;
Y[y][x] = ((cmyk >> 8) & 255) - 128F;
K[y][x] = ((cmyk ) & 255) - 128F;
}
}
}
static int[] createRGB2CMYKLookup(ColorSpace cs) {
int[] lookup = new int[16 << 20]; // eats 16m times 4 bytes = 64mb
float[] frgb = new float[3];
float fcmyk[];
for (int rgb=0; rgb<lookup.length; ++rgb) {
frgb[0] = ((rgb >> 16) & 255) / 255F;
frgb[1] = ((rgb >> 8) & 255) / 255F;
frgb[2] = ((rgb ) & 255) / 255F;
fcmyk = cs.fromRGB(frgb);
int c = (int) (fcmyk[0] * 255F);
int m = (int) (fcmyk[1] * 255F);
int y = (int) (fcmyk[2] * 255F);
int k = (int) (fcmyk[3] * 255F);
int icmyk = (c << 24) | (m << 16) | (y << 8) | k;
}
return lookup;
}
Now this may actually worsen performance for small images as it is. It will only help if you can re-use the lookup table for multiple images, but as your example looks you're using actually the same ICC profile over and over. Thus you could cache the lookup table and pay its initialization cost only once:
static int[] lookup;
static {
ColorSpace cs = new ICC_ColorSpace(...);
lookup = createRGB2CMYKLookup(cs);
}
// convert always using (the same) lookup table
private void RGB2CMYK(int[] rgb, float[][] C, float[][] M, float[][] Y, float[][] K, int imageWidth, int imageHeight) throws Exception {
for(int y = 0, index = 0; y < imageHeight; y++) {
for(int x = 0; x < imageWidth; x++, index++) {
int cmyk = lookup[rgb[index]];
C[y][x] = ((cmyk >> 24) & 255) - 128F;
M[y][x] = ((cmyk >> 16) & 255) - 128F;
Y[y][x] = ((cmyk >> 8) & 255) - 128F;
K[y][x] = ((cmyk ) & 255) - 128F;
}
}
}
You should probably use ColorConvertOp. It uses optimized native code on most platforms, and supports ICC profile transforms.
Not sure how fast it will work when using float based Rasters, but it does the job.
Something like:
ICC_Profile cmyk = ...;
ICC_Profile sRGB = ...;
ColorConvertOp cco = new ColorConvertOp(sRGB, cmyk);
Raster rgbRaster = ...;
WritableRaster cmykRaster = cco.filter(rgbRaster, null);
// Or alternatively, if you have a BufferedImage input
BufferedImage rgbImage = ...;
BufferedImage cmykImage = cco.filter(rgbImage, null);
This question already has an answer here:
How to scan the screen for a specific color/image in java?
(1 answer)
Closed 9 years ago.
I'm not sure where to start on this one, but is there a way I can use Java to scan an image row by row for a specific color, and pass all of the positions into and ArrayList?
Can you? yes. Here's how:
ArrayList<Point> list = new ArrayList<Point>();
BufferedImage bi= ImageIO.read(img); //Reads in the image
//Color you are searching for
int color= 0xFF00FF00; //Green in this example
for (int x=0;x<width;x++)
for (int y=0;y<height;y++)
if(bi.getRGB(x,y)==color)
list.add(new Point(x,y));
Try using a PixelGrabber. It accepts an Image or ImageProducer.
Here's an example adapted from the documentation:
public void handleSinglePixel(int x, int y, int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel ) & 0xff;
// Deal with the pixel as necessary...
}
public void handlePixels(Image img, int x, int y, int w, int h) {
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return;
}
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
handleSinglePixel(x+i, y+j, pixels[j * w + i]);
}
}
}
In your case, you would have:
public void handleSinglePixel(int x, int y, int pixel) {
int target = 0xFFABCDEF; // or whatever
if (pixel == target) {
myArrayList.add(new java.awt.Point(x, y));
}
}
Hi. I want rgb values in this format: In a 1d vector I want first R values, then G values, and then B Values. I tried to use this code:
pixels = new int[bitmap.getHeight() * bitmap.getWidth()];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0,
bitmap.getWidth(), bitmap.getHeight());
// int R, G, B,Y;
for (int y = 0; y < bitmap.getHeight(); y++) {
for (int x = 0; x < bitmap.getWidth(); x++) {
int index = y * bitmap.getHeight() + x;
int R = (pixels[index] >> 16) & 0xff; // bitwise shifting
int G = (pixels[index] >> 8) & 0xff;
int B = pixels[index] & 0xff;
// R,G.B - Red, Green, Blue
// to restore the values after RGB modification, use
// next statement
pixels[index] = 0xff000000 | (R << 16) | (G << 8) | B;
}
}
bitmap.recycle();
} catch (NullPointerException exception) {
Log.e("Error Utils",
"Photo is damaged or does not support this format!");
}
return pixels;
But, I still have only a 300*200 1d array.
Not 300*200*3 1d array!
Maybe it's that what you try to do
public static int[] getPixel(Bitmap bitmap) {
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
int[] pixelIn = new int[width * height];
bitmap.getPixels(pixelIn, 0, width, 0, 0, width, height);
bitmap.recycle();
int[] pixelOut = new int[width * height * 3];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int index = y * height + x;
int R = (pixelIn[index] >> 16) & 0xff;
int G = (pixelIn[index] >> 8) & 0xff;
int B = (pixelIn[index] >> 0) & 0xff;
int indexOut = index * 3;
pixelOut[indexOut++] = R;
pixelOut[indexOut++] = G;
pixelOut[indexOut ] = B;
}
}
return pixelOut;
}
Untested but it should create an int[] (you should consider byte[]) that is filled [R][G][B][R][G][B]...
same for bytes
public static byte[] getPixelBytes(Bitmap bitmap) {
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
final int total = width * height;
int[] pixelIn = new int[total];
bitmap.getPixels(pixelIn, 0, width, 0, 0, width, height);
bitmap.recycle();
byte[] pixelOut = new byte[total * 3];
int indexOut = 0;
for (int pixel : pixelIn) {
byte R = (byte) ((pixel >> 16) & 0xff);
byte G = (byte) ((pixel >> 8) & 0xff);
byte B = (byte) ((pixel ) & 0xff);
pixelOut[indexOut++] = R;
pixelOut[indexOut++] = G;
pixelOut[indexOut++] = B;
}
return pixelOut;
}
And to get it in three separate arrays like [R R R R][G G G G][B B B B]
public static byte[][] getPixelBytes(Bitmap bitmap) {
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
final int total = width * height;
int[] pixelIn = new int[total];
bitmap.getPixels(pixelIn, 0, width, 0, 0, width, height);
bitmap.recycle();
byte[][] result = new byte[3][total];
int index = 0;
for (int pixel : pixelIn) {
byte R = (byte) ((pixel >> 16) & 0xff);
byte G = (byte) ((pixel >> 8) & 0xff);
byte B = (byte) ((pixel ) & 0xff);
result[0][index] = R;
result[1][index] = G;
result[2][index] = B;
index++;
}
return result;
}
The rgb values of the 5th (= index 4) pixel would be
byte R = result[0][4];
byte G = result[1][4];
byte B = result[2][4];
Or to separate that into 3 arrays
byte[] rArray = result[0]; // each 0 .. (width x height - 1)
byte[] gArray = result[1];
byte[] bArray = result[2];
Also don't forget that Java's byte is -128..127, not 0..255.