How to create a grayscale image using java - java

can anyone help me and tell me how to create a gray scale image where one pixel of the image is shown as a square with the size 2 x 2?
I already searched for help and found this how to create a gray scale image from pixel values using java but i don't know how to create a gray scale with the information that one pixel is shown as a square with the size 2 x 2.
thanks!

to create a picture where each pixel has the size 2x2 you must either scale the image (factor 2) for display only... or if you want to create a image you have to do it manually and create an image and draw with scale factor 2 on it
int[] pixels = ... //we already have our gray scale pixels here
int widthOriginal = ... //size of original image
int heightOriginal = ...
//let's create an buffered Image twice the size
BufferedImage img =
new BufferedImage(2*widthOriginal, 2*heightOriginal, BufferedImage.TYPE_4BYTE_ABGR);
//we paint on the buffered image's graphic...
Graphics gr = img.getGraphics();
//we draw all pixels on the graphic
for(int y = 0; y < heightOriginal; y ++){
for(int x = 0; x < widthOriginal; x ++){
int index = y*widthOriginal + x;
int gray = pixels[index];
//to draw we first set the color
gr.setColor(new Color(gray));
//then draw the pixel
gr.drawRect(2*x, 2*y,2,2); //draw a 2x2 pixel instead of a 1x1 pixel
}
}
uhm - honestly i've written that code entirely out of my head, so there may be some minor compilation problems... but the technique is explained properly...

Related

how to load image as pixel image in android java?

i want to creat an android app
that shows all pixels of an image but when i zoom color of the corners of the pixels blend with other pixels around it i want pixels to show their color perfectly in their squares
( like pixel art or ms paint when you zoom it)
i tried drawing a rectangle on canvas and drawing a bitmap 1×1
with color what i want but it blends
here is a picture and code
upper pic 4×4 what it creates
lower pic what i want
try{
//creating bitmap from other for background
bt2 = Bitmap.createBitmap(bt,i2,i3,i,i);
}
Canvas c=new Canvas(bt2);
c.drawColor(Color.argb(255,255,255,255));
//creating 1×1 image
Bitmap bt3 = Bitmap.createBitmap(bt,0,0,1, 1 );
//blue color to draw for pixel looking
Canvas c2 =new Canvas(bt3);
c2.drawColor(Color.argb(255,0,0,255));
int w=bt2.getWidth();
int h= bt2.getHeight();
//geting pixels so i would use it
btpixels=new int [w*h];
bt2.getPixels(btpixels,
0,w,0,0,w,h );
int j =0;
Paint paint =new Paint();
for(j=0;j<w;j++)
{
paint.setARGB(255,0,255,0);
//drawing blue bitmap on bt2
c.drawBitmap(bt3,j,j,paint) ;
//paint.setColor( btpixels[j]);
/* c.drawRect(
(float)i-1,
(float) i-1,
(float)i,
(float)i,
paint ); */
}
img.setImageBitmap( bt2 );
}catch(IllegalArgumentException e){ }
i understood how to stop pixels from blending. I was using 2×2 image on the screen 300ppi . If i want to show 1 pixel per inch i should use pixel bitmap(here bt3) width as 300
best would be
width of the pixel square =
width of the screen / num of pixels

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));
}
}

Image and Canvas coordinate displacement when drawing

I am drawing lines on Canvas of SurfaceView which is in FrameLayout. I receive image from camera preview, process it, get coordinates of rectangles and raw the lines of it on canvas. I get a displacement of those lines in y axis when drawn, the lower the line is, the bigger displacement occurs (see photos below):
With red line I marked (with Paint program, not the actual app) the approximate position of where the coordinates coordinates of lower line are on bitmap, and the lower green line of rectangle (placed by the actual app, drawn on canvas) along with red line shows, how much coordinates are displaced. The coordinates on top cannot cross the screen, but on the bottom they can- if the rectangle is low enough, lines come below the screen, but on top they do not (they should not do that on either of all 4 sides, because the given coordinates are never out of bitmap).
The drawing function looks like this:
public void DrawAllContours(List<Point[]> rectPoints, int ratio)
{
paint.setColor(Color.GREEN);
paint.setStrokeWidth(2.5f);
canvas = sh.lockCanvas();
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
for (int i = 0; i < rectPoints.size(); i++)
{
for (int j = 0; j < 4; j++)
canvas.drawLine((float)rectPoints.get(i)[j].x*ratio, (float)rectPoints.get(i)[j].y*ratio,
(float)rectPoints.get(i)[(j+1)%4].x*ratio, (float)rectPoints.get(i)[(j+1)%4].y*ratio, paint);
}
sh.unlockCanvasAndPost(canvas);
}
The ratio variable is the difference between the actual preview image size and resized bitmap for processing size (usualy the value is 3). The actual coordinates are as they should be on preview bitmap, but not on Canvas.
How can I overcome this issue and place lines on their exact places?
Thank you in advance.
So I found out, that camera preview size does differ from the canvas size. My preview size was 1920X1080, and canvas 1845X1080 or so, so thats why Y coordinate was displaced. I just found out the difference ratio and drew lines respectively by that ratio. Canvas resolution can be found with Canvas.getWidth(), Canvas.getHeight().

Detect object-boundary in jpeg image with a "seeming" uniform background

I have a Jpeg image with a "seeming" uniform background and an actual object in it. I want to crop the image to extract only the object.
I started with following code where I am trying to find the (x,y) on left side to start cropping.
I found out that bottom left corner of the image has (0,0) coordinates. I am looping through and trying to find out the exact point where the color changes.
The problem is during first iteration itself : when x=0 and y is incrementing, though there is no change in color it is giving different RGB values for few pixels.(like pixel 240 , 241, 242)
BufferedImage bf =ImageIO.read.file(imagePath);
for(int x= 0;x<bf.getWidth();x++)
{
for(int y=0; j<bf.getHeight();y++)
{
int color = bf.RGB(x,y);
int adjacentColor = bf.(x,y+1);
if(color !=adjacentColor)
{
LeftBoundaryPixels[count]=y;
count++;
break;
}
}
}
You can use a color distance formula (like the distance formula sqrt(x^2 + y^2)) with a threshold distance to dismiss pixels that are close but not completely the same. For example: sqrt(r^2 + g^2 + b^2);.

image processing

I want to change the value of the pixels in an image, for which i need to store the image as a matrix. How can i perform this job? Please guide.
BufferedImage image = ImageIO.read(..);
image.setRGB(x, y, rgb);
Check the documentation of BufferedImage
Using image.setRGB is extremely slow.
You can use Catalano Framework
Example:
FastBitmap fb = new FastBitmap(bufferedImage);
int x = fb.getRed(0,0);
//If you prefer to retrieve the matrix you can do too.
int[][][] image = new int[fb.getHeight][fb.getWidth][3];
fb.toArrayRGB(image);
Firstly read the image into a BufferedImage.
BufferedImage image = ImageIO.read(new File("..."));
Then create matrix like structure in the 2D array like this and set RGB:
for(int i = 0; i < image.getWidth(); i++){
for(int j = 0; j < image.getHeight(); j++){
image.setRGB(i, j, rgb);
}
}
Image is 2d representation of data (pixel info)
2D means x&y directions. In case of image, these directions are generally treated as rows & columns
To change the pixel value, we have to get its location in these rows and columns
Getting pixel location is like that class teacher addressing the unknown student with his sitting position (ex:2nd bench 3rd person)
Like this we have to address the pixel by its rows and column location

Categories

Resources