Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2004
    Messaggi
    107

    software elaborazione immagini

    Salve a tutti! Sto provando a realizzare un software per l'elaborazione di immagini. Nulla di che in realtà, visto che mi serve per l'uni.
    L'obiettivo è quello di applicare semplici filtri alle immagini (tipo sfocatura, messa a fuoco, etc.) utilizzando i metodi della classe BufferedImage.
    Per realizzare l'effetto smoothing ad esempio (sfocatura) si fa così:
    - si suddivide l'immagine in matrici 3x3 pixel
    - per ogni matrice si prendono i valori del colore
    - si fa la media (totale/9)
    - si sostituisce il valore dei 9 pixel della matrice con il risultato appena ottenuto (ossia la media)

    semplice, no? a dirsi... a farsi non tanto!! Oddio... l'algoritmo è semplice ma il risultato è sballato!!

    Ho paura che il problema sia sui metodi getRGB e setRGB della classe BufferedImage che mi restituiscono un colore in chissà quale scala!

    Io ho scritto questo codice e dovrebbe andare bene, ma ovviamente il risultato è tutt'altro che una sfocatura...

    codice:
    public static BufferedImage smoothingFilter(BufferedImage img){
    		int[] array = new int[9];
    		int i=0;
    		int tempX = 1;
    		int tempY = 1;
    		
    		for(tempY=1; tempY<=(img.getHeight()-3); tempY+=3){    //ripeto l'operazione per ogni riga
    			for(tempX=1; tempX<=(img.getWidth()-3); tempX+=3){  //ripeto l'operazione per ogni colonna
    				for(int x=tempX; x<tempX+3; x++){
    					for(int y=tempY; y<tempY+3; y++){
    						array[i] = img.getRGB(x,y);  //memorizzo i valori in un array grande 9
    						i++;
    					}
    				}	
    				int somma = 0;
    				for (i=0; i<array.length; i++){
    					somma += (array[i]/9); //faccio la media
    				}
    				for(int x=tempX; x<tempX+3; x++){
    					for(int y=tempY; y<tempY+3; y++){
    						img.setRGB(x,y,somma/9); //imposto il nuovo colore per i 9 pixel della matrice
    					}
    				}	
    				i=0;
    			}
    		}
    		return img;
    	}
    Qualcuno saprebbe aiutarmi o ha qualche idea in mente?
    Grazie mille!!!
    ciaooo
    Giovanni

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Secondo me non puoi operare sui pixel in quel modo, ma bisognerebbe separare i canali. Io ho fatto questa classettina di prova:

    codice:
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.*;
    import javax.imageio.*;
    /**
     *
     * @author Andrea
     */
    public class ImageFilters {
        
        private BufferedImage src,dst;
        private JFrame frame;
        
        public void BlurFilter() {
            dst = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
            for (int y = 1; y < src.getHeight()-1; y++) {
                for (int x = 1; x < src.getWidth() - 1; x++) {
                    int rgb = 0;
                    int r_current = 0;
                    int g_current = 0;
                    int b_current = 0;
                    for (int h = -1; h <= 1; h++) {
                        for (int k = -1; k <= 1; k++) {
                            rgb = src.getRGB(x+k, y+h);
                            //shift per separare i canali, 2 byte a canale
                            r_current += ((rgb >> 16) & 0xff);
                            g_current += ((rgb >> 8) & 0xff);
                            b_current += (rgb & 0xff);
                            
                        }
                    }
                    //faccio la media
                    r_current /= 9;
                    g_current /= 9;
                    b_current /= 9;
                    
                    // e risistemo con lo shifting inverso
                    rgb = r_current << 16 | g_current << 8 | b_current; 
                    // e setto il pixel
                    dst.setRGB(x,y,rgb); 
                }
            }        
        }
        
        public void show() {
            frame = new JFrame("Show Result");
            frame.setSize(dst.getWidth(), dst.getHeight()*2);
            frame.getContentPane().setLayout(new GridLayout(2,1));
            frame.getContentPane().add(new JLabel(new ImageIcon(src)));
            frame.getContentPane().add(new JLabel(new ImageIcon(dst)));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
        
        /** Creates a new instance of BlurFilter */
        public ImageFilters(String path) throws Exception {
            src = ImageIO.read(new File(path));
        }
        
        public static void main (String[] args) {
            try {
                ImageFilters blur = new ImageFilters("C:/Documents and Settings/Andrea/Desktop/nemo.jpg");
                blur.BlurFilter();
                blur.show();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.