I would like to know how can I specify the z-direction for my cube. It needs to be translated to -5 along z-direction.
The whole problem statement is like this: Implement the transformation pipeline, by defining a cube in 3d space, setting up a camera in the origin of the coordinate system and a projection matrix that has a 90° FOV. The cube size should be 2 unites (2x2x2), laid down 5 unites along the negative z axis
package transformations;
import java.util.*;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
public class Transformations {
//declare list of polygons
static ArrayList<Integer>p_left=new ArrayList<Integer>();
static ArrayList<Integer>p_right=new ArrayList<Integer>();
static ArrayList<Integer>p_down=new ArrayList<Integer>();
static ArrayList<Integer>p_top=new ArrayList<Integer>();
static ArrayList<Integer>p_near=new ArrayList<Integer>();
static ArrayList<Integer>p_far=new ArrayList<Integer>();
static int count=0;
public static BufferedImage gradientSetRaster(BufferedImage img) {
int width = img.getWidth();
int height = img.getHeight();
WritableRaster raster = img.getRaster();
int[] pixel = new int[3]; //RGB
for (int y = 0; y < height; y++) {
int val = (int) (y * 255f / height);
for (int shift = 1; shift < 3; shift++) {
pixel[shift] = val;
}
for (int x = 0; x < width; x++) {
raster.setPixel(x, y, pixel);
}
}
return img;
}
public static BufferedImage drawLine(BufferedImage img, int x0, int y0, int xn, int yn)
{
WritableRaster raster=img.getRaster();
int[] pixel = new int[3]; //RGB
for(int k=0; k<3; k++)
{
pixel[k]=255;
}
int dx=Math.abs(xn-x0);
int dy=Math.abs(yn-y0);
int m=dy/dx;
int twoDy=2*dy;
int twoDyMintwoDx=twoDy-2*dx;
if(dx>dy) //slope is positive and is less than or equal to 1
{
int p=twoDy-dx;
for(int k=0; k<dx-1; k++)
{
if(p<0)
{
x0=x0+1;
raster.setPixel(x0, y0, pixel);
p=p+twoDy;
}
else
{
x0=x0+1;
y0=y0+1;
raster.setPixel(x0, y0, pixel);
p=p+twoDy-2*dx;
}
}
}
if(dy>dx)
{
int p=2*dx-dy;
int temp1=x0;
x0=y0;
y0=temp1;
int temp2=xn;
xn=yn;
yn=temp2;
for(int k=0; k<dx-1; k++)
{
x0=x0+1;
y0=y0+1;
raster.setPixel(x0, y0, pixel);
}
}
if(dy==dx)
{
x0=x0+1;
y0=y0+1;
raster.setPixel(x0, y0, pixel);
}
return img;
}
public static BufferedImage drawUpPolygon(Graphics g, BufferedImage img, ArrayList<Integer>p_top)
{
g.drawLine(p_top.get(0), p_top.get(1), p_top.get(2), p_top.get(3));
g.drawLine(p_top.get(0), p_top.get(1), p_top.get(4), p_top.get(5));
g.drawLine(p_top.get(4), p_top.get(5), p_top.get(6), p_top.get(7));
g.drawLine(p_top.get(6), p_top.get(7), p_top.get(2), p_top.get(3));
return img;
}
public static BufferedImage drawDownPolygon(Graphics g, BufferedImage img, ArrayList<Integer>p_top)
{
g.drawLine(p_down.get(0), p_down.get(1), p_down.get(2), p_down.get(3));
g.drawLine(p_down.get(0), p_down.get(1), p_down.get(4), p_down.get(5));
g.drawLine(p_down.get(4), p_down.get(5), p_down.get(6), p_down.get(7));
g.drawLine(p_down.get(6), p_down.get(7), p_down.get(2), p_down.get(3));
return img;
}
public static BufferedImage drawBetween(Graphics g, BufferedImage img, ArrayList<Integer>p_top, ArrayList<Integer>p_down)
{
g.drawLine(p_top.get(0), p_top.get(1), p_down.get(0), p_down.get(1));
g.drawLine(p_top.get(2), p_top.get(3), p_down.get(2), p_down.get(3));
g.drawLine(p_top.get(4), p_top.get(5), p_down.get(4), p_down.get(5));
g.drawLine(p_top.get(6), p_top.get(7), p_down.get(6), p_down.get(7));
return img;
}
public static void main(String... args) {
Frame w = new Frame("Raster"); //window
final int imageWidth = 500;
final int imageHeight = 500;
w.setSize(imageWidth,imageHeight);
w.setLocation(100,100);
w.setVisible(true);
Graphics g = w.getGraphics();
BufferedImage img = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
gradientSetRaster(img);
g.drawImage(img, 0, 0, (img1, infoflags, x, y, width, height) -> false); //draw the image. You can think of this as the display method.
w.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
//order will be(vertex->index)
//lowerLeft->0,1
//upperLeft->2,3
//lowerRight->4,5
//upperRight->6,7
count++;
int t_lowLeftX=e.getX();
int t_lowLeftY=e.getY();
//top polygon
int t_lowRightX;
int t_lowRightY;
int t_uppLeftX;
int t_uppLeftY;
int t_uppRightX;
int t_uppRightY;
//bottom polygon
int d_lowLeftX;
int d_lowLeftY;
int d_lowRightX;
int d_lowRightY;
int d_uppLeftX;
int d_uppLeftY;
int d_uppRightX;
int d_uppRightY;
if(count==1) {
//lowerLeft indexes
t_lowLeftX=e.getX();
t_lowLeftY=e.getY();
t_lowRightX=t_lowLeftX+100;
t_lowRightY=t_lowLeftY;
t_uppLeftX=t_lowLeftX+50;
t_uppLeftY=t_lowLeftY+50;
t_uppRightX=t_uppLeftX+100;
t_uppRightY=t_uppLeftY;
d_lowLeftX=t_lowLeftX;
d_lowLeftY=t_lowLeftY-70;
d_lowRightX=t_lowRightX;
d_lowRightY=t_lowRightY-70;
d_uppLeftX=t_uppLeftX;
d_uppLeftY=t_uppLeftY-70;
d_uppRightX=t_uppRightX;
d_uppRightY=t_uppRightY-70;
//connect(lowLeft->uppLeft, lowLeft->lowRight, uppLeft->uppRight,
//uppRight->lowRight ...)
p_top.add(t_lowLeftX);
p_top.add(t_lowLeftY);
p_top.add(t_lowRightX);
p_top.add(t_lowRightY);
p_top.add(t_uppLeftX);
p_top.add(t_uppLeftY);
p_top.add(t_uppRightX);
p_top.add(t_uppRightY);
p_down.add(d_lowLeftX);
p_down.add(d_lowLeftY);
p_down.add(d_lowRightX);
p_down.add(d_lowRightY);
p_down.add(d_uppLeftX);
p_down.add(d_uppLeftY);
p_down.add(d_uppRightX);
p_down.add(d_uppRightY);
// g.drawLine(lowLeftX, lowLeftY, uppLeftX, uppLeftY);
// g.drawLine(lowLeftX, lowLeftY, lowRightX, lowRightY);
// g.drawLine(uppLeftX, uppLeftY, uppRightX, uppRightY);
// g.drawLine(uppRightX, uppRightY, lowRightX, lowRightY);
drawUpPolygon(g, img, p_top);
drawDownPolygon(g, img, p_down);
drawBetween(g, img, p_top, p_down);
int[] pixel = {255,255,255};
img.getRaster().setPixel(t_lowLeftX,t_lowLeftY,pixel);
img.getRaster().setPixel(t_uppLeftX, t_uppLeftY,pixel);
img.getRaster().setPixel(t_lowLeftX, t_lowLeftY, pixel);
img.getRaster().setPixel(t_lowRightX, t_lowRightY, pixel);
img.getRaster().setPixel(t_uppRightX, t_uppRightY, pixel);
img.getRaster().setPixel(d_lowLeftX,d_lowLeftY,pixel);
img.getRaster().setPixel(d_uppLeftX, d_uppLeftY,pixel);
img.getRaster().setPixel(d_lowLeftX, d_lowLeftY, pixel);
img.getRaster().setPixel(d_lowRightX, d_lowRightY, pixel);
img.getRaster().setPixel(d_uppRightX, d_uppRightY, pixel);
};
//top polygon's lower left vertex
}
});
w.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
w.dispose();
g.dispose();
System.exit(0);
}
});
}
}
Related
So I'm working on a paint program in school and now I have to double buffer it. After a week of searching and using tutorials online I am still unable to double buffer my program properly... I have written a double buffer function but then you can't paint anything... When i disable the double buffer you can paint fine but the screen flickers. Please help
(Yes I am aware that some of my code is deprecated and I should fix that)
Code:
import java.applet.*;
import java.awt.*;
public class PaintRun extends Applet
{
static int posx = 0;
static int posy = 0;
static int posxOld = 0;
static int posyOld = 0;
static int size = 20;
static int toolNum = 0;
static boolean ready = false;
static Color col = new Color(120,120,120);
static Button clear = new Button(0,0,20,'C');
static Button sizeInc = new Button(20,0,20,'+');
static Button sizeDec = new Button(40,0,20,'-');
static Tools tool = new Tools(posx,posy);
public void paint(Graphics g)
{
drawButtons(g);
detectButtons(g);
}
private static void detectButtons(Graphics g)
{
if (clear.detect(posx,posy) == true)
{
g.setColor(Color.WHITE);
g.fillRect(0, 0, 800000, 800000);
drawButtons(g);
}
else if (sizeInc.detect(posx, posy))
{
size+=2;
drawButtons(g);
}
else if (sizeDec.detect(posx, posy))
{
size-=2;
if (size <= 2)
size = 2;
drawButtons(g);
}
else
{
if (ready == true)
{
g.setColor(Color.BLACK);
g.drawLine(posxOld, posyOld, posx, posy);
drawButtons(g);
}
}
}
private static void drawButtons(Graphics g)
{
clear.setColor(col, Color.BLACK);
sizeInc.setColor(col, Color.BLACK);
sizeDec.setColor(col, Color.BLACK);
clear.drawButton(g);
sizeInc.drawButton(g);
sizeDec.drawButton(g);
}
public boolean mouseDown(Event e, int x, int y)
{
posx = x;
posy = y;
posxOld = x;
posyOld = y;
ready = true;
repaint();
return true;
}
public boolean mouseDrag(Event e, int x, int y)
{
posxOld = posx;
posyOld = posy;
posx = x;
posy = y;
repaint();
return true;
}
// Normal Update function
public void update(Graphics g)
{
paint(g);
}
// Double buffered Update function
/*public void update(Graphics g)
{
Graphics offgc;
Image offscreen = null;
Dimension d = size();
offscreen = createImage(d.width, d.height);
offgc = offscreen.getGraphics();
offgc.setColor(getBackground());
offgc.fillRect(0, 0, d.width, d.height);
offgc.setColor(getForeground());
paint(offgc);
g.drawImage(offscreen, 0, 0, this);
}
*/
}
class Button extends Applet
{
int minX; // X coord where the button is drawn
int minY; // Y coord where the button is drawn
int maxX; // X size
int maxY; // Y size
char letter;
Color back;
Color line;
// Constructor block
public Button(char desc)
{
minX = 0;
minY = 0;
maxX = 40;
maxY = 40;
back = new Color(255,255,255);
line = new Color(0,0,0);
letter = desc;
}
public Button(int x, int y, char desc)
{
minX = x;
minY = y;
maxX = 40;
maxY = 40;
back = new Color(255,255,255);
line = new Color(0,0,0);
letter = desc;
}
public Button(int x, int y, int size, char desc)
{
minX = x;
minY = y;
maxX = size;
maxY = size;
back = new Color(255,255,255);
line = new Color(0,0,0);
letter = desc;
}
public Button(int x, int y, int sizeX, int sizeY, char desc)
{
minX = x;
minY = y;
maxX = sizeX;
maxY = sizeY;
back = new Color(255,255,255);
line = new Color(0,0,0);
letter = desc;
}
// Draws the button
public void drawButton(Graphics g)
{
g.setColor(back);
g.fillRect(minX, minY, maxX, maxY);
g.setColor(line);
g.drawRect(minX, minY, maxX, maxY);
g.drawString(Character.toString(letter), (minX+maxX/2)-4, (minY+maxY/2)+4);
}
// Sets the button colors
public void setColor(Color b, Color l)
{
back = b;
line = l;
}
// Detects if those coordinates are in the button location or not
public boolean detect(int x, int y)
{
boolean flag = false;
if (x > minX && x < (minX + maxX) && y > minY && y < (minY + maxY))
flag = true;
else
flag = false;
return flag;
}
}
so i have a little Problem and i try to solve it since hours.
I have a BufferedImage and i want to change the colour in a fluent way f.e. from red to white.
My main:
public static void main(String[] args) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x != width; x++) {
for (int y = 0; y != height; y++) {
image.setRGB(x, y, color12(x, y));
try {
,,,
My method to change the color:
static int color12(int x, int y) {
int size = 100;
if (Math.abs(width / 2 - x) < size / 2 && Math.abs(height / 2 - y) < size / 2)
return new Color(255, 0, 0).getRGB();
else
return new Color(200, 200, 255).getRGB();
}
}
So i played around with the method, but i cant get it done.
My best "solution" was this:
int r = 0 ;
int b = 0;
int g = 0;
for (int i = 1; i < 255; i++)
r++;
else return new Color(r,g,b).getRGB();
Can anyone help me?
I'm not sure how you want the gradient (e.g. horizontal, vertical or diagonal), but here's an example using linear interpolation for a horizontal or vertical gradient.
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class ExampleFrame extends JFrame {
private static enum GradientOrientation {
HORIZONTAL, VERTICAL
};
private static BufferedImage createGradientImg(int width, int height, Color startColor, Color endColor, GradientOrientation o) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int pos = o.equals(GradientOrientation.HORIZONTAL) ? x : y;
int size = o.equals(GradientOrientation.HORIZONTAL) ? width : height;
image.setRGB(x, y, getGradientRGB(startColor, endColor, pos, size));
}
}
return image;
}
private static int getGradientRGB(Color startColor, Color endColor, int pos, int size) {
double perc = (double) pos / size;
int newRed = (int) (endColor.getRed() * perc + startColor.getRed() * (1 - perc));
int newGreen = (int) (endColor.getGreen() * perc + startColor.getGreen() * (1 - perc));
int newBlue = (int) (endColor.getBlue() * perc + startColor.getBlue() * (1 - perc));
return new Color(newRed, newGreen, newBlue).getRGB();
}
public void createAndShow() {
BufferedImage img1 = createGradientImg(200, 100, Color.RED,
Color.WHITE, GradientOrientation.HORIZONTAL);
BufferedImage img2 = createGradientImg(200, 100, Color.BLUE,
Color.YELLOW, GradientOrientation.HORIZONTAL);
BufferedImage img3 = createGradientImg(200, 100, Color.WHITE,
Color.YELLOW, GradientOrientation.VERTICAL);
BufferedImage img4 = createGradientImg(200, 100, Color.BLACK,
Color.WHITE, GradientOrientation.VERTICAL);
BoxLayout layout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
getContentPane().setLayout(layout);
getContentPane().add(new JLabel(new ImageIcon(img1)));
getContentPane().add(new JLabel(new ImageIcon(img2)));
getContentPane().add(new JLabel(new ImageIcon(img3)));
getContentPane().add(new JLabel(new ImageIcon(img4)));
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
ExampleFrame ef = new ExampleFrame();
ef.createAndShow();
}
});
}
}
I was wondering whether there is any way to convert an image/graphic into a Shape? For example, can I convert the outline of a motorcycle shape into a Shape so I can then use it in Java? I know you can do it with normal squares or with rounded corners, polygons, etc. But is there a way to do a custom shape?
motorcycle.jpg
motorcycle-03.png
ImageOutline.java
This code requires some patience (when running).
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.geom.Area;
import javax.imageio.ImageIO;
import java.io.File;
import java.util.Date;
import javax.swing.*;
/* Motorcycle image courtesy of ShutterStock
http://www.shutterstock.com/pic-13585165/stock-vector-travel-motorcycle-silhouette.html */
class ImageOutline {
public static Area getOutline(BufferedImage image, Color color, boolean include, int tolerance) {
Area area = new Area();
for (int x=0; x<image.getWidth(); x++) {
for (int y=0; y<image.getHeight(); y++) {
Color pixel = new Color(image.getRGB(x,y));
if (include) {
if (isIncluded(color, pixel, tolerance)) {
Rectangle r = new Rectangle(x,y,1,1);
area.add(new Area(r));
}
} else {
if (!isIncluded(color, pixel, tolerance)) {
Rectangle r = new Rectangle(x,y,1,1);
area.add(new Area(r));
}
}
}
}
return area;
}
public static boolean isIncluded(Color target, Color pixel, int tolerance) {
int rT = target.getRed();
int gT = target.getGreen();
int bT = target.getBlue();
int rP = pixel.getRed();
int gP = pixel.getGreen();
int bP = pixel.getBlue();
return(
(rP-tolerance<=rT) && (rT<=rP+tolerance) &&
(gP-tolerance<=gT) && (gT<=gP+tolerance) &&
(bP-tolerance<=bT) && (bT<=bP+tolerance) );
}
public static BufferedImage drawOutline(int w, int h, Area area) {
final BufferedImage result = new BufferedImage(
w,
h,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = result.createGraphics();
g.setColor(Color.white);
g.fillRect(0,0,w,h);
g.setClip(area);
g.setColor(Color.red);
g.fillRect(0,0,w,h);
g.setClip(null);
g.setStroke(new BasicStroke(1));
g.setColor(Color.blue);
g.draw(area);
return result;
}
public static BufferedImage createAndWrite(
BufferedImage image,
Color color,
boolean include,
int tolerance,
String name)
throws Exception {
int w = image.getWidth();
int h = image.getHeight();
System.out.println("Get Area: " + new Date() + " - " + name);
Area area = getOutline(image, color, include, tolerance);
System.out.println("Got Area: " + new Date() + " - " + name);
final BufferedImage result = drawOutline(w,h,area);
displayAndWriteImage(result, name);
return result;
}
public static void displayAndWriteImage(BufferedImage image, String fileName) throws Exception {
ImageIO.write(image, "png", new File(fileName));
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(image)));
}
public static void main(String[] args) throws Exception {
final BufferedImage outline = ImageIO.read(new File("motorcycle.jpg"));
BufferedImage crop = outline.getSubimage(17,35,420,270);
displayAndWriteImage(crop, "motorcycle-01.png");
BufferedImage crude = createAndWrite(crop, Color.white, false, 60, "motorcycle-02.png");
BufferedImage combo = createAndWrite(crude, Color.red, true, 0, "motorcycle-03.png");
}
}
function getArea_FastHack is build upon Andrew Thompsons work, which was very helpful.
Mine should be faster, however:
(//Edit: and sloppier, too)
import java.awt.*;
import java.awt.geom.Area;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
/**
* CustomShape
* based on a Class from Andrew Thompson *
* Source: http://stackoverflow.com/questions/7052422/image-graphic-into-a-shape-in-java/7059497#7059497
* #author Samuel Schneider, Andrew Thompson
*
*
*/
class CustomShape {
private BufferedImage image=null;
/**
* Creates an Area with PixelPerfect precision
* #param color The color that is draws the Custom Shape
* #param tolerance The color tolerance
* #return Area
*/
public Area getArea(Color color, int tolerance) {
if(image==null) return null;
Area area = new Area();
for (int x=0; x<image.getWidth(); x++) {
for (int y=0; y<image.getHeight(); y++) {
Color pixel = new Color(image.getRGB(x,y));
if (isIncluded(color, pixel, tolerance)) {
Rectangle r = new Rectangle(x,y,1,1);
area.add(new Area(r));
}
}
}
return area;
}
public Area getArea_FastHack() {
//Assumes Black as Shape Color
if(image==null) return null;
Area area = new Area();
Rectangle r;
int y1,y2;
for (int x=0; x<image.getWidth(); x++) {
y1=99;
y2=-1;
for (int y=0; y<image.getHeight(); y++) {
Color pixel = new Color(image.getRGB(x,y));
//-16777216 entspricht RGB(0,0,0)
if (pixel.getRGB()==-16777216) {
if(y1==99) {y1=y;y2=y;}
if(y>(y2+1)) {
r = new Rectangle(x,y1,1,y2-y1);
area.add(new Area(r));
y1=y;y2=y;
}
y2=y;
}
}
if((y2-y1)>=0) {
r = new Rectangle(x,y1,1,y2-y1);
area.add(new Area(r));
}
}
return area;
}
public static boolean isIncluded(Color target, Color pixel, int tolerance) {
int rT = target.getRed();
int gT = target.getGreen();
int bT = target.getBlue();
int rP = pixel.getRed();
int gP = pixel.getGreen();
int bP = pixel.getBlue();
return(
(rP-tolerance<=rT) && (rT<=rP+tolerance) &&
(gP-tolerance<=gT) && (gT<=gP+tolerance) &&
(bP-tolerance<=bT) && (bT<=bP+tolerance) );
}
public CustomShape(String path) {
try {
BufferedImage image = ImageIO.read(new File(path));
this.image = image;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Here's something faster but less accurate, useful for collision checking or 2D physics.
Point[] MakePoly(BufferedImage spr,int d,int angle){
//creates an outline of a transparent image, points are stored in an array
//arg0 - BufferedImage source image
//arg1 - Int detail (lower = better)
//arg2 - Int angle threshold in degrees (will remove points with angle differences below this level; 15 is a good value)
// making this larger will make the body faster but less accurate;
int w= spr.getWidth(null); int h= spr.getHeight(null);
// increase array size from 255 if needed
int[] vertex_x=new int[255], vertex_y=new int[255], vertex_k=new int[255];
int numPoints=0, tx=0,ty=0,fy=-1,lx=0,ly=0; vertex_x[0]=0; vertex_y[0]=0; vertex_k[0]=1;
for (tx=0;tx<w;tx+=d) for (ty=0;ty<h;ty+=1) if((spr.getRGB(tx,ty)>>24) != 0x00 )
{vertex_x[numPoints]=tx; vertex_y[numPoints]=h-ty; vertex_k[numPoints]=1; numPoints++; if (fy<0) fy=ty; lx=tx; ly=ty; break; }
for (ty=0;ty<h;ty+=d) for (tx=w-1;tx>=0;tx-=1) if((spr.getRGB(tx,ty)>>24) != 0x00 && ty > ly)
{vertex_x[numPoints]=tx; vertex_y[numPoints]=h-ty; vertex_k[numPoints]=1; numPoints++; lx=tx; ly=ty; break; }
for (tx=w-1;tx>=0;tx-=d) for (ty=h-1;ty>=0;ty-=1) if((spr.getRGB(tx,ty)>>24) != 0x00 && tx < lx)
{vertex_x[numPoints]=tx; vertex_y[numPoints]=h-ty; vertex_k[numPoints]=1; numPoints ++; lx=tx; ly=ty; break; }
for (ty=h-1;ty>=0;ty-=d) for (tx=0;tx<w;tx+=1) if((spr.getRGB(tx,ty)>>24) != 0x00 && ty < ly && ty > fy)
{vertex_x[numPoints]=tx; vertex_y[numPoints]=h-ty; vertex_k[numPoints]=1; numPoints ++; lx=tx; ly=ty; break; }
double ang1,ang2; for (int i=0;i<numPoints-2;i++) {
ang1 = PointDirection(vertex_x[i],vertex_y[i], vertex_x[i+1],vertex_y[i+1]);
ang2 = PointDirection(vertex_x[i+1],vertex_y[i+1], vertex_x[i+2],vertex_y[i+2]);
if (Math.abs(ang1-ang2) <= angle) vertex_k[i+1] = 0; }
ang1 = PointDirection(vertex_x[numPoints-2],vertex_y[numPoints-2], vertex_x[numPoints-1],vertex_y[numPoints-1]);
ang2 = PointDirection(vertex_x[numPoints-1],vertex_y[numPoints-1], vertex_x[0],vertex_y[0]);
if (Math.abs(ang1-ang2) <= angle) vertex_k[numPoints-1] = 0;
ang1 = PointDirection(vertex_x[numPoints-1],vertex_y[numPoints-1], vertex_x[0],vertex_y[0]);
ang2 = PointDirection(vertex_x[0],vertex_y[0], vertex_x[1],vertex_y[1]);
if (Math.abs(ang1-ang2) <= angle) vertex_k[0] = 0;
int n=0;for (int i=0;i<numPoints;i++)if(vertex_k[i]==1)n++;
Point[] poly= new Point[n]; n=0; for (int i=0;i<numPoints;i++) if (vertex_k[i]==1)
{ poly[n]=new Point(); poly[n].x=vertex_x[i]; poly[n].y=h-vertex_y[i];n++;} return poly;
}
double PointDirection(double xfrom,double yfrom,double xto,double yto){
return Math.atan2(yto-yfrom,xto-xfrom)*180/Math.PI ;
}
I'm looking for some help in applying 2D Gabor Wavelets Formula to an image in java.
This is the formula that I'm using.
Gabor Formula
I need my output to look like this
I've read the image in and its currently stored in a 2D array. My code is as follows:` public void RunGabor() throws IOException
{
double[][]pixels=getImage();
int H= pixels.length;
int W =pixels[0].length;
size=H*W;
gaussian=size/2;
System.out.println(gaussian);
GaborGrid = new int[H][W];
GaborNorm = new int[H][W];
double X=0,Y=0, gx=-gaussian,gy=-gaussian, count=0, total=0;
int ax=0, dy=0;
for(int x=0; x<pixels.length; x++)
{
for(int k=0;k< pixels[0].length; k++)
{
X=gx*Math.cos(theta)+gy*Math.sin(theta);
Y=-gx*Math.sin(theta)+gy*Math.cos(theta);
pixels[dy][ax]=((Math.exp(-(Math.pow(X, 2)+(Math.pow(Y, 2)*
Math.pow(upsi,2)))/(2*Math.pow(sigma, 2))))*
(Math.cos((kappa*X+varphi))));
System.out.println("Pixels" +pixels[dy][ax]);
total+=pixels[dy][ax];
count++;
System.out.println("Count" +count);
gx+=1;
ax++;
}
System.out.println("second loop");
ax=0;
dy++;
gy+=1;
gx=-gaussian;
}
mean=total/count;
System.out.println("Mean" +mean);
NormaliseImage(pixels);
}`
From there it calls a method normaliseImage
public void NormaliseImage(double[][] pixels)
{
double minII = pixels[0][0];
double maxII = pixels[0][0];
for(int y=0; y<pixels.length; y++){
for(int x= 0; x<pixels[0].length; x++){
if(pixels[y][x] <= minII){
minII =pixels[y][x];
}
if(pixels[y][x]>= maxII){
maxII=pixels[y][x];
}
}
My create image class looks like this
public CreateImage(int[][] data, String IMGname)
{
name = IMGname;
width = data[0].length;
height = data.length;
System.out.println("NEW IMAGE 1");
pixels = new int[height*width];
int count=0;
for(int i=0; i<data.length; i++)
{
for(int j=0; j<data[0].length; j++)
{
pixels[count] = (int)Math.abs(data[i][j]);
pixels[count] = convert2pixel(pixels[count]);
count++;
}
}
Create(width, height, pixels, name);
}
public int convert2pixel(int pixel)
{
return ((0xff<<24)|(pixel<<16)|(pixel<<8)|pixel);
}
public int convert2grey(double pixel)
{
int red=((int)pixel>>16) & 0xff;
int green = ((int)pixel>>8) & 0xff;
int blue = (int)pixel & 0xff;
return (int)(0.3*red+0.6*green+0.1*blue);
}
public void Create(int Width, int Height, int pixels[], String n)//throws Exception
{
//System.out.println("Inside Create Image");
MemoryImageSource MemImg = new MemoryImageSource(Width,Height,pixels,0,Width);
Image img2= Toolkit.getDefaultToolkit().createImage(MemImg);
BufferedImage bfi = new BufferedImage(Height,Width, BufferedImage.TYPE_INT_BGR);
Graphics2D g2D = bfi.createGraphics();
g2D.drawImage(img2, 0, 0, Width, Height, null);
try
{
ImageIO.write(bfi, "png", new File(n+".png"));
}
catch(Exception e){}
}
}
My output is just a grey screen, any help would be appreciated.
Update: Gabor Driver Class `
public class Gabor_Driver{
public static void main(String[]args)throws IOException
{
double lamda=75;
double theta=45;
double varphi=90;
double upsi=10;
double bandW=10;
//int size=500;
Gabor gabor = new Gabor(lamda, theta, varphi, upsi, bandW );
}
}
`
Gabor class :
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class Gabor
{
CreateImage ci;
private double lamda=0;
private double theta=0;
private double varphi=0;
private double upsi=0;
private double bandW=0;
private double B=0;
private double sigma=0;
private double kappa=0;
private int[][] GaborGrid;
private int[][] GaborNorm;
int size=0;
double toRadians=180/Math.PI, min=500, max=-500, mean=0;;
int gaussian=0;
double rotation;
double GLFmean=0;
//Standard Gabor no quantization
public Gabor(double l, double t, double v, double u, double b) throws IOException
{
lamda=l;
theta=t/toRadians;
varphi=v/toRadians;
upsi=u;
bandW=b;
kappa=(2*Math.PI)/lamda;
Calculate_Sigma();
RunGabor();
}
public void RunGabor() throws IOException
{
double[][]pixels=getImage();
int H= pixels.length;
int W =pixels[0].length;
size=H*W;
gaussian=size/2;
System.out.println(gaussian);
GaborGrid = new int[H][W];
GaborNorm = new int[H][W];
double X=0,Y=0, gx=-gaussian,gy=-gaussian, count=0, total=0;
int ax=0, dy=0;
for(int x=0; x<pixels.length; x++)
{
for(int k=0;k< pixels[0].length; k++)
{
X=gx*Math.cos(theta)+gy*Math.sin(theta);
Y=-gx*Math.sin(theta)+gy*Math.cos(theta);
pixels[dy][ax]=((Math.exp(-(Math.pow(X, 2)+(Math.pow(Y, 2)*
Math.pow(upsi,2)))/(2*Math.pow(sigma, 2))))*
(Math.cos((kappa*X+varphi))));
System.out.println("Pixels" +pixels[dy][ax]);
total+=pixels[dy][ax];
count++;
System.out.println("Count" +count);
gx+=1;
ax++;
}
System.out.println("second loop");
ax=0;
dy++;
gy+=1;
gx=-gaussian;
}
mean=total/count;
System.out.println("Mean" +mean);
NormaliseImage(pixels);
}
public double[][] getImage() throws IOException{
int[][]pixels =null;
double[][] doubles = null;
JFileChooser fc = new JFileChooser();
int returnValue = fc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fc.getSelectedFile();
BufferedImage image = ImageIO.read(selectedFile);
System.out.println(selectedFile.getName());
int W =image.getWidth();
int H= image.getHeight();
int width = image.getWidth();
int height = image.getHeight();
pixels = new int[height][width];
for (int row = 0; row < height; row++) {
image.getRGB(0, row, width, 1, pixels[row], 0, width);
}
doubles = new double[pixels.length][pixels[0].length];
for(int i=0; i<pixels.length; i++) {
for(int j=0; j<pixels[0].length; j++)
doubles[i][j] = pixels[+i][+j];
}
}
return doubles;
}
public void NormaliseImage(double[][] pixels)
{
double minII = pixels[0][0];
double maxII = pixels[0][0];
for(int y=0; y<pixels.length; y++){
for(int x= 0; x<pixels[0].length; x++){
if(pixels[y][x] <= minII){
minII =pixels[y][x];
}
if(pixels[y][x]>= maxII){
maxII=pixels[y][x];
}
}
}
int count=0;
double total=0;
for(int y=0; y<pixels.length; y++){
for(int x= 0; x<pixels[0].length; x++){
total += pixels[y][x];
count++;
}
}
double average =(double)total/count;
for(int y=0; y<pixels.length; y++){
for(int x= 0; x<pixels[0].length; x++){
double normalise= ((((pixels[y][x]-min))/((max-min)))*255);
if(normalise<=average){
normalise =0;
}
GaborNorm[y][x] = (int) normalise;
}
}
ci = new CreateImage(GaborNorm, "Gabor");
}
private void Calculate_Sigma()
{
B=(1/Math.PI)*(0.588705011)*((Math.pow(2, bandW)+1)/(Math.pow(2, bandW)-1));
sigma=B*lamda;
}
}
Create image class:
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class CreateImage
{
int[] pixels;
int width=0, height=0;
String name;
public CreateImage(int[][] data, String IMGname)
{
name = IMGname;
width = data[0].length;
height = data.length;
System.out.println("NEW IMAGE 1");
pixels = new int[height*width];
int count=0;
for(int i=0; i<data.length; i++)
{
for(int j=0; j<data[0].length; j++)
{
pixels[count] = (int)Math.abs(data[i][j]);
pixels[count] = convert2pixel(pixels[count]);
count++;
}
}
Create(width, height, pixels, name);
}
public int convert2pixel(int pixel)
{
return ((0xff<<24)|(pixel<<16)|(pixel<<8)|pixel);
}
public int convert2grey(double pixel)
{
int red=((int)pixel>>16) & 0xff;
int green = ((int)pixel>>8) & 0xff;
int blue = (int)pixel & 0xff;
return (int)(0.3*red+0.6*green+0.1*blue);
}
public void Create(int Width, int Height, int pixels[], String n)//throws Exception
{
//System.out.println("Inside Create Image");
MemoryImageSource MemImg = new MemoryImageSource(Width,Height,pixels,0,Width);
Image img2= Toolkit.getDefaultToolkit().createImage(MemImg);
BufferedImage bfi = new BufferedImage(Height,Width, BufferedImage.TYPE_INT_BGR);
Graphics2D g2D = bfi.createGraphics();
g2D.drawImage(img2, 0, 0, Width, Height, null);
try
{
ImageIO.write(bfi, "png", new File(n+".png"));
}
catch(Exception e){}
}
}
Your calculations are mostly correct. You are calculating the function itself in RunGabor - I think gx, gy should be replaced with x, k. This
for(int x=0; x<pixels.length; x++)
{
for(int k=0;k< pixels[0].length; k++)
{
X=gx*Math.cos(theta)+gy*Math.sin(theta);
Y=-gx*Math.sin(theta)+gy*Math.cos(theta);
pixels[dy][ax]=((Math.exp(-(Math.pow(X, 2)+(Math.pow(Y, 2)*
Math.pow(upsi,2)))/(2*Math.pow(sigma, 2))))*
(Math.cos((kappa*X+varphi))));
should be replaced with
public Kernel getKernel() {
double sigma = calculateSigma(waveLength, bandwidth);
float[] data = new float[width*height];
for(int k = 0, x = -width/2; x <= width/2; x++) {
for(int y = -height/2; y <= height/2; y++) {
for(double orientation : orientations) {
double x1 = x*Math.cos(orientation) + y*Math.sin(orientation);
double y1 = -x*Math.sin(orientation) + y*Math.cos(orientation);
data[k] += (float)(gaborFunction(x1, y1, sigma, aspectRatio, waveLength, phaseOffset));
}
k++;
}
}
But you have to apply the function on the pixels that you are reading, the image. If you look at this other implementation https://github.com/clumsy/gabor-filter/blob/master/src/main/java/GaborFilter.java
you will find many parallels. See if you can fix it.
At least as a first step you should try to print the values of the gabor function either 3x3 or 5x5. If they look reasonable you can go ahead to apply it to the image.
I have this code, in which I extracted the value of RGB for each pixel, but I'm wondering how to store each RGB value in an array so that I can use it further in my project. I want to take these stored RGB values as an input for backpropagation algorithm.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
public class PrintImageARGBPixels
{
public static void main(String args[])throws IOException
{
BufferedImage image = ImageIO.read(new File("C:\\Users\\ark\\Desktop\\project_doc\\logo_1004.jpg"));
int r=0,g=0,b=0;
int w = image.getWidth();
int h = image.getHeight();
System.out.println("Image Dimension: Height-" + h + ", Width-"+ w);
int total_pixels=(h * w);
ArrayList <Color> arr = new ArrayList<Color>();
for(int x=0;x<w;x++)
{
for(int y=0;y<h;y++)
{
int rgb = image.getRGB(x, y);
Color c = new Color(rgb);
r=c.getRed();
g=c.getGreen();
b=c.getBlue();
}
}
Color co = new Color(r,g,b);
arr.add(co);
for(int i=0;i<total_pixels;i++)
System.out.println("Element 1"+i+1+", color: Red " + arr.get(i).getRed() + " Green +arr.get(i).getGreen()+ " Blue " + arr.get(i).getBlue());
}
}
// Store the color objects in an array
int total_pixels = (h * w);
Color[] colors = new Color[total_pixels];
int i = 0;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
colors[i] = new Color(image.getRGB(x, y));
i++;
}
}
// Later you can retrieve them
for (int i = 0; i < total_pixels; i++)
{
Color c = colors[i];
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
System.out.println("Red" + r + "Green" + g + "Blue" + b);
}
IGNORE THE BELOW, it is my old answer
Use a multidimensional array:
[
[255, 255, 255],
[108, 106, 107],
[100, 100, 55],
...
]
You can then refer to each pixel, [0][x] to get the colour values.
Why don't you just create a RGB Object like this
public class RGB {
private int R, G, B;
public RGB(int R, int G, int B){
this.R = R;
this.G = G;
this.B = B;
}
public int getR() {
return R;
}
public void setR(int r) {
R = r;
}
public int getG() {
return G;
}
public void setG(int g) {
G = g;
}
public int getB() {
return B;
}
public void setB(int b) {
B = b;
}
}
So you can store RGB Objects in your array to use them later.
Greetings!
It can be achieved easier with HashMap, where Key is an int[] (x and y) and value is the another int[] (r, g, b).