MaxUpGenGros Posted April 18, 2013 at 11:28 PM Report #503920 Posted April 18, 2013 at 11:28 PM Boa noite. Estou a fazer um trabalho de processamento de imagem em java, e tenho algumas questões e gostava que alguém me ajudasse se possivel. Resumindo: -Abro uma imagem; -Faço a conversão dessa imagem para gray para me ser mais facil fazer o processamento; -A seguir coloco na matriz que obtenho num array; -Percorro o array resultante e procuro os 10 valores mais altos em todo o array, e guardo os indices; -Depois crio um array com o mesmo tamanho do anterior, mas com todos os elementos a 0; -Em seguida neste array com zeros, coloco a 1's os indices que obtive na procura dos maiores valores; -E agora tenho que colocar este array novamente numa matriz com a dimensão da inicial, e depois fazer o print dessa matriz, de onde vai resultar uma imagem a preto e branco, onde o branco são os indices que guardei como sendo os 10 indices com os valores mais altos. A minha dúvida é neste ultimo ponto e não estou a conseguir implementar. Em Matlab faço isto em 5minutos sem problemas, o meu grande problema é ser em Java. Se alguém me pudesse dar uma ajuda agradecia.. FCoelho
HappyHippyHippo Posted April 19, 2013 at 12:09 AM Report #503925 Posted April 19, 2013 at 12:09 AM apresenta o código que já tens IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
MaxUpGenGros Posted April 19, 2013 at 12:12 AM Author Report #503926 Posted April 19, 2013 at 12:12 AM (edited) package imagetesting; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.*; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * * @author SynchroKvb */ public class ImageTesting extends Component { private static int c; private static int[][] i; private static int[] h; private static int[][] V; BufferedImage image; private BufferedImage g; public void paint(Graphics g) { g.drawImage(image, 0, 0, null); } public ImageTesting() { try { image = ImageIO.read(new File("teste.jpg")); } catch (IOException e) { } } public Dimension getPreferredSize() { if (image == null) { return new Dimension(400, 400); } else { return new Dimension(image.getWidth(null), image.getHeight(null)); } } public static BufferedImage rgb2gray(BufferedImage bi)//converter a imagem para gray { int heightLimit = bi.getHeight(); int widthLimit = bi.getTileWidth(); BufferedImage converted = new BufferedImage(widthLimit, heightLimit, BufferedImage.TYPE_BYTE_GRAY); for (int height = 0; height < heightLimit; height++) { for (int width = 0; width < widthLimit; width++) { Color c = new Color(bi.getRGB(width, height) & 0x00fffff); int newRed = (int) ((0.2989f * c.getRed()) * 1.45);//0.2989f//multiplicr po 2 int newGreen = (int) ((0.5870f * c.getGreen()) * 1.45);//0.5870f int newBlue = (int) ((0.1140f * c.getBlue()) * 1.45); int roOffset = newRed + newGreen + newBlue; converted.setRGB(width, height, roOffset); } } return converted; } public static BufferedImage BuffImg2rgb() { BufferedImage img = null; try { img = ImageIO.read(new File("Teste.jpg")); } catch (IOException e) { } return img; } // public static int length(BufferedImage bi) { // int x; // x = bi.getHeight() * bi.getWidth(); // return x; // } public static int[][] reshape(int[][] A, int m, int n) { int origM = A.length; int origN = A[0].length; if(origM*origN != m*n){ throw new IllegalArgumentException("New matrix must be of same area as matix A"); } int[][] B = new int[m][n]; int[] A1D = new int[A.length * A[0].length]; int index = 0; for(int i = 0;i<A.length;i++){ for(int j = 0;j<A[0].length;j++){ A1D[index++] = A[j]; } } index = 0; for(int i = 0;i<n;i++){ for(int j = 0;j<m;j++){ B[j] = A1D[index++]; } } return B; } public static int[] img2matriz(BufferedImage bi) { WritableRaster a = bi.getRaster(); DataBuffer b = a.getDataBuffer(); int o = b.getSize(); int[] C = new int[o]; for (int i = 0; i < bi.getHeight()*bi.getWidth(); i++) {//320 for (int j = 0; j < bi.getWidth(); j++) {//240 C[j] = b.getElem(j); } } return C; } public static int[] zeros(int a) { int arr[] = new int[a]; for (int i = 0; i < arr.length; i++) { arr = 0; } return arr; } private static int[][] convertTo2DWithoutUsingGetRGB(BufferedImage image) { final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); final int width = image.getWidth(); final int height = image.getHeight(); final boolean hasAlphaChannel = image.getAlphaRaster() != null; int[][] result = new int[height][width]; if (hasAlphaChannel) { final int pixelLength = 4; for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) { int argb = 0; argb += (((int) pixels[pixel] & 0xff) << 24); // alpha argb += ((int) pixels[pixel + 1] & 0xff); // blue argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red result[row][col] = argb; col++; if (col == width) { col = 0; row++; } } } else { final int pixelLength = 3; for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) { int argb = 0; argb += -16777216; // 255 alpha argb += ((int) pixels[pixel] & 0xff); // blue argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red result[row][col] = argb; col++; if (col == width) { col = 0; row++; } } } return result; } /** * @param args the command line arguments */ public static void main(String[] args) throws IOException { // TODO code application logic here JFrame f = new JFrame("Load Image Sample"); BufferedImage a; BufferedImage b; BufferedImage c = null; int g[]; f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.add(new ImageTesting()); f.pack(); f.setVisible(true); a = BuffImg2rgb(); b = rgb2gray(a); JFrame frame = new JFrame(); frame.getContentPane().add(new JLabel(new ImageIcon(b))); frame.pack(); frame.setVisible(true); // // g = img2matriz(b); i=convertTo2DWithoutUsingGetRGB(b); V=reshape(i,b.getHeight()*b.getWidth(),1); //****************TESTE int k[]=zeros(V.length); k[0]=1;k[1]=1;k[2]=1; BufferedImage teste=new BufferedImage(320,240,BufferedImage.TYPE_INT_RGB); for(int m=0;m<100;m++){ for(int j=0;j<100;j++) { teste.setRGB(m, j, 0); }; }; frame.getContentPane().add(new JLabel(new ImageIcon(teste))); frame.pack(); frame.setVisible(true); } } Edited April 19, 2013 at 12:30 PM by brunoais geshi FCoelho
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now