I have the following code:
Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_4444);
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
int index = y * WIDTH + x;
bitmap.setPixel(x, y, Color.argb(255, 0, mask[index],0)); // mask is an array of int between 0 and 255
}
}
It works properly: I get my bitmap but...this code is really extremely slow.
I tried to replace it with:
Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_4444);
bitmap.setPixels(mask, 0, WIDTH, 0, 0, WIDTH, HEIGHT);
but this is not working. I get a black image.
Anybody can help ?
Thanks !
Related
I want to remove particular color, and when I set
wrres.setSample(x, y, 0, wrsrc.getSample(x, y, 0)) ;
I don't have error but when I set something else like
wrres.setSample(x, y, 1, wrsrc.getSample(x, y, 1)) ; then I've got
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at java.awt.image.MultiPixelPackedSampleModel.getSample(MultiPixelPackedSampleModel.java:359)
at java.awt.image.Raster.getSample(Raster.java:1648)
it gives me the same image (just copied)
BufferedImage imres = user_space(img,1);
int height = img.getHeight();
int width = img.getWidth();
BufferedImage imsrc = img; // The source image, RGBA
BufferedImage imres = user_space(img,1); // The resulting image, RGB or BGR
WritableRaster wrsrc = imsrc.getRaster() ;
WritableRaster wrres = imres.getRaster() ;
for (int y=0 ; y < img.getHeight() ; y++)
for (int x=0 ; x < img.getWidth() ; x++)
{
wrres.setSample(x, y, 0, wrsrc.getSample(x, y, 0)) ;
wrres.setSample(x, y, 1, wrsrc.getSample(x, y, 1)) ;
wrres.setSample(x, y, 2, wrsrc.getSample(x, y, 2)) ;
}
return
new BufferedImage(imsrc.getColorModel(),wrres,imsrc.getColorModel().isAlphaPremultiplied(), null);
}
How to use setSample to set different colors not only black and white(band equal to 0 ) '
I don't want to use getRGB from BufferedImage
I have a code that turns a RGB bitmap into a bitmap of black and white colors, using this code:
public static Bitmap setDefaultValues(Bitmap bmp) {
Mat srcMat = new Mat();
org.opencv.android.Utils.bitmapToMat(bmp, srcMat, true);
final Bitmap bitmap = Bitmap.createBitmap(srcMat.clone().width(), srcMat.clone().height(), Bitmap.Config.ARGB_8888);
Imgproc.cvtColor(srcMat, srcMat, Imgproc.COLOR_BGR2GRAY, 0);
Mat srcMat1 = srcMat;
Imgproc.GaussianBlur(srcMat1, srcMat1, new Size(3, 3), 0);
//Mat srcMat1 = new Mat(srcMat.rows(), srcMat.cols(), CV_8UC1);
//int kernalsize = 3;
//Imgproc.bilateralFilter(srcMat, srcMat1, kernalsize, kernalsize * 2, kernalsize / 2);
srcMat1.convertTo(srcMat1, 0, 1.9, -120);
srcMat1.convertTo(srcMat1, CvType.CV_8U, 1.9, -120);
Imgproc.cvtColor(srcMat1, srcMat1, Imgproc.COLOR_GRAY2RGBA, 4);
org.opencv.android.Utils.matToBitmap(srcMat, bitmap, true);
return bitmap;
}
I have implement this code for convert RGB image into black and white.
this is return me as right, but my question is here i cant remove shadow from image.
also i have compare other application this is convert perfectly, i don't understand where i am wrong.
this is original Image :
this is my application output
this is other application output
So please help me how can i achieve my goal.
Please use following code for convert your color image to black and white.
public static Bitmap createContrast(Bitmap src, double value) {
// image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
// get contrast value
double contrast = Math.pow((100 + value) / 100, 2);
// scan through all pixels
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
// apply filter contrast for every channel R, G, B
R = Color.red(pixel);
R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if(R < 0) { R = 0; }
else if(R > 255) { R = 255; }
G = Color.red(pixel);
G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if(G < 0) { G = 0; }
else if(G > 255) { G = 255; }
B = Color.red(pixel);
B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if(B < 0) { B = 0; }
else if(B > 255) { B = 255; }
// set new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;}
Please try this if you will get the solution
public static Bitmap test(Bitmap src){
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
int gray = (int) (0.2989 * R + 0.5870 * G + 0.1140 * B);
// use 128 as threshold, above -> white, below -> black
if (gray > 128) {
gray = 255;
}
else{
gray = 0;
}
// set new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, gray, gray, gray));
}
}
return bmOut;
}
Please see the answer on this thread. He has explained and provide a good result in output.
#Threshold image using opencv (Java)
I need to put a random image inside a screen with given resolution (640x480, 1280x720, etc). I finished it in Java. In Android do not support the BufferedImage and Graphics2D, I wonder if there is a way to replace this code from Java to Android. Here is my code from Java:
public BufferedImage resizeImage(BufferedImage originalImage, int type){
BufferedImage resizedImage = new BufferedImage(screenWidth, screenHeight, type);
Graphics2D g = resizedImage.createGraphics();
int imgWidth = originalImage.getWidth();
int imgHeight = originalImage.getHeight();
int newImgWidth = 0;
int newImgHeight = 0;
int X = 0;
int Y = 0;
if (imgWidth > screenWidth){
// scale width to fit
newImgWidth = screenWidth;
//scale height to maintain aspect ratio
newImgHeight = (newImgWidth * imgHeight) / imgWidth;
}
if (newImgHeight > screenHeight) {
//scale height to fit instead
newImgHeight = screenHeight;
//scale width to maintain aspect ratio
newImgWidth = (newImgHeight * imgWidth) / imgHeight;
}
if (imgWidth < screenWidth && imgHeight < screenHeight) {
X = screenWidth/2 - imgWidth/2;
Y = screenHeight/2 - imgHeight/2;
g.drawImage(originalImage, X, Y, imgWidth, imgHeight, null);
g.dispose();
return resizedImage;
}
X = screenWidth/2 - newImgWidth/2;
Y = screenHeight/2 - newImgHeight/2;
g.drawImage(originalImage, X, Y, newImgWidth, newImgHeight, null);
g.dispose();
return resizedImage;
}
Thank you in advance!
I have the following Java code:
public static BufferedImage createImage(byte[] data, int width, int height)
{
BufferedImage res = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
byte[] rdata = ((DataBufferByte)res.getRaster().getDataBuffer()).getData();
for (int y = 0; y < height; y++) {
int yi = y * width;
for (int x = 0; x < width; x++) {
rdata[yi] = data[yi];
yi++;
}
}
return res;
}
Is there a faster way to do this?
In C++ I would use memcpy, but in Java?
Or maybe it is possible to initialize the result image with the passed data directly?
Well, to copy the array quickly you can use System.arraycopy:
System.arraycopy(data, 0, rdata, 0, height * width);
I don't know about initializing the BufferedImage to start with though, I'm afraid.
Have you tried:
res.getRaster().setDataElements(0, 0, width, height, data);
?
My real goal is to read the values from a graph in GIF format, into some meaningful data structure but in order to get started I need to be able to read the colour of each pixel of the GIF in question.
In order to test this i want to save the segment of the GIF i am reading to file for visual analysis, but am having trouble.
after reading this post I attempted to do something similar, however my output GIF always comes out completely black.
can anyone tell me what i've misunderstood?
BufferedImage bi = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/commons/3/36/Sunflower_as_GIF.gif"));
int x = 100;
int y = 100;
int width = 100;
int height = 100;
int[] data = grabPixels(bi, x, y, width, height);
BufferedImage img = createImage(data, width, height);
ImageIO.write(img, "gif", new File("part.gif"));
...
private int[] grabPixels(BufferedImage img, int x, int y, int width, int height)
{
try
{
PixelGrabber pg = new PixelGrabber(img, x, y, width, height, true);
pg.grabPixels();
if ((pg.getStatus() & ImageObserver.ABORT) != 0)
throw new RuntimeException("image fetch aborted or errored");
return convertPixels((int[]) pg.getPixels(), width, height);
}
catch (InterruptedException e)
{
throw new RuntimeException("interrupted waiting for pixels", e);
}
}
public int[] convertPixels(int[] pixels, int width, int height)
{
int[] newPix = new int[width * height * 3];
int n = 0;
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
int pixel = pixels[j * width + i];
newPix[n++] = (pixel >> 16) & 0xff;
newPix[n++] = (pixel >> 8) & 0xff;
newPix[n++] = (pixel) & 0xff;
}
}
return newPix;
}
private BufferedImage createImage(int[] pixels, int width, int height)
{
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
WritableRaster raster = (WritableRaster) image.getData();
raster.setPixels(0, 0, width, height, pixels);
return image;
}
All black sounds like zeros, as if the image hadn't loaded yet. You might check the result returned by grabPixels() or specify a timeout. Once you have a BufferedImage, you could use getRaster() and work with the WritableRaster.