imagej always shows black images - java

for an assignment I have to display an image using imagej in java. So I used the following code:
FloatProcessor abc=new FloatProcessor(imageSizeX,imageSizeY);
for (int i=0;i<imageSizeX;i++){
for(int j=0;j<imageSizeY;j++){
abc.putPixel(i, j, 100);
}
}
ImagePlus im=new ImagePlus("test",abc);
im.show();
but the Image I get is always completely black. Can you tell me what the mistake is?
It should at least be white if the value was 0 shouldn't it?
(FYI: imageSizeX=imageSizeY=256)

.putPixel uses the conversion Float.intBitsToFloat.
If you want a direct access to the pixels, you can use setf(int x, int y, float value).
Moreover, if you already have the pixels into an array, you can use the constructors to immediately set the pixel values FloatProcessor(int width, int height, int[] pixels).

Related

Trying to get multiple images out of a single image

I've been stuck at something recently.
What I want to do is to get multiple sub-images out of 1 big image.
So take this example. I have a frame of 128x128 pixels where all the images need to be in.
I'm putting all the bufferedImages inside a list and scaling all those images to 128x128.
The image you see on that link is showing that I need 4 sub-images from that image, so at the end, I have 4 images which are 128x128 but 4 times.
Or if you have an image with 128x384 it will give 3 sub-images going from top to bottom.
https://i.stack.imgur.com/RsCkf.png
I know there is a function called
BufferedImage.getSubimage(int x, int y, int w, int h);
But the problem is that I can't figure out what math I need to implement.
What I tried is if the height or width is higher than 200 then divide it by 2 but that never worked for me.
I'm not sure I fully understand what you are asking, but I think what you want is something like this:
First, loop over the image in both dimensions.
Then compute the size of the tile (the smaller value of 128 and (image dimension - start pos)). This is to make sure you don't try to fetch a tile out of bounds. If your images are always a multiple of 128 in any dimension, you could just skip this step and just use 128 (just make sure you validate that input images follow this assumption).
If you only want tiles of exactly 128x128, you could also just skip the remainder, if the tile is less than 128x128, I'm not sure what your requirement is here. Anyway, I'll leave that to you. :-)
Finally, get the subimage of that size and coordinates and store in the list.
Code:
BufferedImage image = ...;
int tileSize = 128;
List<BufferedImage> tiles = new ArrayList<>();
for (int y = 0; y < image.height(); y += tileSize) {
int h = Math.min(tileSize, image.height() - y);
for (int x = 0; x < image.width(); x += tileSize) {
int w = Math.min(tileSize, image.width() - x);
tiles .add(image.getSubimage(x, y, w, h));
}
}

Trying to resize image in Processing (PDE) language

I am in the holiday spirit, and I was thinking about writing a little countdown timer for Christmas, but ran into some difficulty that I cannot resolve. I have 2 PImage variables, one that was directly loaded from an image on the hard drive called "origbkg", while the other is the actual image that is being displayed as the background for my program. I made it this way so that if the user changes the size of the window, the image will resize correspondingly. However, when I make the window very small, and then try to maximize it again it loses resolution, although it should be getting reset to the original full resolution image that was loaded off the drive.
When I have tried to display the "origbkg" on the screen, it also is getting modified for some reason when all I am doing is setting bkgimg=origbkg.
Here is my code:
//Christmas Countdown Clock
//by: Ben Templin
//============================================================
//-------------------------Variables-------------------------
boolean bkgsized;
String background;
PImage origbkg;
PImage bkgimg;
int count;
int psize;
int size;
//-------------------------Setup-------------------------
void setup()
{
frameRate(30);
smooth();
size(displayWidth-250, displayHeight-250);
frame.setResizable(true);
background="default";
textSize(20);
fill(255, 0, 0);
text("Loading Images...", width/2-100, height/2);
origbkg=loadImage("Christmas-Tree.png");
bkgsized=false;
bkgimg=origbkg;
}
//-------------------------Draw-------------------------
void draw()
{
size=width+height;
if(psize!=size){
bkgsized=false;
}
if(bkgsized==false){
sizebkg();
}
psize=width+height;
}
//-------------------------Size the Background-------------------------
void sizebkg()
{
bkgimg=origbkg;
bkgimg.resize(width, height);
image(bkgimg, 0, 0);
bkgsized=true;
}
Can someone please tell me why "origbkg" is getting changed? Thanks in advance, and
Merry Christmas!
bkgimg=origbkg;
bkgimg.resize(width, height);
Variable origbkg holds a reference to a PImage object. First, this code copies that reference into bkgimg, so that both bkgimg and origbkg refer to the same PImage. Then, it resizes the image which bkgimg refers to--which is the same image that origbkg refers to! To use this technique, you'll have to use some method to clone or copy the image, and set bkgimg to the copy, not the original. However, for this use case, it is actually more efficient to simply use the image() method with additional parameters to rescale the image to fit into your width and height:
image(origbkg, 0, 0, width, height);
This way, you don't have to create a new copy of the image each time it is resized.
As a side note, your draw() method's check to see if the window has been resized is flawed: It checks whether the sum of width+height has changed, but this check fails to account for the possibility that the window is resized such that its width grows by the same distance that height shrinks, or vice versa. To properly check for a resize, you must store both a pwidth variable and a pheight variable and compare them.

java - how to make an image using setRGB?

i have 2D array to keep color component value :
p[pixel_value][red]
p[pixel_value][green]
p[pixel_value][blue]
i just dont know how to use them to make an image.
i read about setRGB, what i understand is i should mix all of them to become a RGBArray. then how to make it?
is it any better way to make an image without setRGB ? i need some explanation.
The method setRGB() can be used to set the color of a pixel of an already existing image. You can open an already existing image and set all the pixels of it, using the values stored in your 2D array.
You can do it like this:
BufferedImage img = ImageIO.read(new File("image which you want to change the pixels"));
for(int width=0; width < img.getWidth(); width++)
{
for(int height=0; height < img.getHeight(); height++)
{
Color temp = new Color(p[pixel_value][red], p[pixel_value][green], p[pixel_value][blue]);
img.setRGB(width, height, temp.getRGB());
}
}
ImageIO.write(img, "jpg", new File("where you want to store this new image"));
Like this, you can iterate over all the pixels and set their color accordingly.
NOTE: By doing this, you will lose your original image. This is just a way which I know.
What you need is a BufferedImage. Create a BufferedImage of type TYPE_3BYTE_BGR, if RGB is what you want, with a specified width and height.
QuickFact:
The BufferedImage subclass describes an Image with an accessible
buffer of image data.
Then, call the getRaster() method to get a WritableRaster
QuickFact:
This class extends Raster to provide pixel writing capabilities.
Then, use the setPixel(int x, int y, double[] dArray) method to set the pixels.
Update:
If all you want is to read an image, use the ImageIO.read(File f) method. It will allow you to read an image file in just one method call.
Somewhat SSCCE:
BufferedImage img = null;
try {
img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
}
You want to manually set RGB values?
You need to know that since an int is 32bit it contains all 4 rgb values (1 for the transparency).
xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
^Alpha ^red ^green ^blue
You can accomplish using 4 int values by the use of binary arithmetic:
int rgb = (red << 16) && () (green << 8) & (blue);
bufferedImage.setRGB(x, y, rgb);
IN the above you can add Alpha as well if needed. You just "push" the binary codes into the right places.

How to calculate "scanLegth" param from Bitmap.getRGB565(...)

I'm trying get bytes from bitmap in blackberry using the next method in Bitmap:
getRGB565(byte[] rgbData, int offset, int scanLength, int x, int y, int width, int height)
But i have read the params and i don't know how must i calculate scanLength:
scanLength - Width of a scanline (in bytes) within the data array.
Any idea?
Here scanLength is the full width of the original image, while width is the width of the rectangle you are copying from.
If you are copying the whole image it is the same, but if you are copying only a part of the image you'll have scanLength > width.
See also the Bitmap#getRGB565 javadoc
To get byte[] from Bitmap I used this: http://blackberry-digger.blogspot.com/2009/05/code-convert-bitmap-to-png-and-then.html and it worked fine.
Sorry it was too easy . In getARG is another example , it must be used usually with the same int width param

Convert jpeg/png to an array of pixels in java

How can I convert a string containing a jpeg or png to an array (preferably one dimensional) of pixels? Ideally using classes built into java?
It turns out you need commons-fileupload. Look at the user guide for how to obtain the image InputStream. From there you can simply call:
BufferedImage image = ImageIO.read(item.getInputStream());
From here on there are many ways:
loop over the image dimensions and for each x and y call int rgb = image.getRGB(x, y);
same as above, but call getRed(x, y), getGreen(x, y), getBlue(x, y)
get the ColorModel and call the above methods there
call getRGB(startX, startY, w, h, rgbArray, offset, scansize)
call getData(), which returns a Raster, and call getPixes(..) there
Use PixelGraber. It returns one-dimensional array of RGB data.

Categories

Resources