Skip to content

Instantly share code, notes, and snippets.

@hugoabonizio
Last active April 28, 2016 13:17
Show Gist options
  • Save hugoabonizio/5ff88368fa2342f0805d to your computer and use it in GitHub Desktop.
Save hugoabonizio/5ff88368fa2342f0805d to your computer and use it in GitHub Desktop.
Exercícios CG ImageJ
package aulavisaocomp;
import ij.ImagePlus;
import ij.io.Opener;
import ij.process.ByteProcessor;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class AulaVisaoComp {
public static void main(String[] args) throws IOException {
// new AulaVisaoComp().drawChess();
// new AulaVisaoComp().drawGrays();
new AulaVisaoComp().drawChessColor();
// new AulaVisaoComp().japanFlag();
new AulaVisaoComp().changeChessColor();
}
void drawChess() throws IOException {
int width = 64;
int height = 64;
boolean flag = false;
ImageProcessor ip = new ByteProcessor(width, height);
// ip.setColor(0);
// ip.fill();
for (int i = 0; i < width; i++) {
if (i % 8 == 0) {
flag = !flag;
}
for (int j = 0; j < height; j++) {
if (j % 8 == 0) {
flag = !flag;
}
if (flag) {
ip.putPixel(i, j, 255);
} else {
ip.putPixel(i, j, 0);
}
}
}
ImagePlus imp = new ImagePlus("Tabuleiro", ip);
imp.show();
}
void drawGrays() {
int width = 16;
int height = 16;
int count = 0;
ImageProcessor ip = new ByteProcessor(width, height);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
ip.putPixel(j, i, count);
count++;
}
}
ImagePlus imp = new ImagePlus("Cinzas", ip);
imp.show();
}
void drawChessColor() throws IOException {
int width = 256;
int height = 256;
boolean flag = false;
ImageProcessor ip = new ColorProcessor(width, height);
for (int i = 0; i < width; i++) {
if (i % 32 == 0) {
flag = !flag;
}
for (int j = 0; j < height; j++) {
if (j % 32 == 0) {
flag = !flag;
}
if (flag) {
ip.putPixel(i, j, Color.YELLOW.getRGB());
} else {
ip.putPixel(i, j, Color.RED.getRGB());
}
}
}
ImagePlus imp = new ImagePlus("Tabuleiro", ip);
imp.show();
ImageIO.write(ip.getBufferedImage(), "png", new File("tabuleiro.jpg"));
}
void japanFlag() {
int width = 100;
int height = 66;
int centroX = 50;
int centroY = 33;
ImageProcessor ip = new ColorProcessor(width, height);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (Math.pow(Math.pow(i - centroX, 2) + Math.pow(j - centroY, 2), 0.5) < 15) {
ip.putPixel(i, j, Color.RED.getRGB());
} else {
ip.putPixel(i, j, Color.WHITE.getRGB());
}
}
}
ImagePlus imp = new ImagePlus("Japon", ip);
imp.show();
}
void changeChessColor() throws IOException {
ImagePlus imp = new Opener().openImage("tabuleiro.jpg");
ImageProcessor ip = imp.getProcessor();
for (int i = 0; i < imp.getWidth(); i++) {
for (int j = 0; j < imp.getHeight(); j++) {
if (ip.getPixel(i, j) == Color.YELLOW.getRGB()) {
ip.putPixel(i, j, Color.BLUE.getRGB());
}
}
}
ImagePlus imp2 = new ImagePlus("Tabuleiro azul", ip);
imp2.show();
ImageIO.write(ip.getBufferedImage(), "png", new File("tabuleiro_azul.jpg"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment