// Programme Alexis // GUI : Graphical User Interface // Création d'une fenêtre colorée avec menu déroulant // programme tiré de SwingComboBox.javaavec ajout d'instructions pour prise en compte d'événements de sélection dans liste // requiert l'interface ItemListener package Traitement_image; import java.awt.Image.*; import javax.swing.*; // new : import java.awt.event.*; import java.awt.*; import java.awt.image.*; import java.awt.MediaTracker.*; import java.io.*; import java.awt.image.BufferedImage.*; import javax.imageio.*; class CNAM_image_alexis extends JFrame implements ItemListener { // new : implements ItemListener // new : create here the Combo Box component JComboBox image_trait = new JComboBox(); // island est un objet "menu déroulant " JLabel ChoiceLabel = new JLabel (); String Image_File = "desert"; String S_Egalise_gris = "Egalise_gris"; String S_Egalise_couleurs = "Egalise_couleurs"; String S_Negative = "Negative"; String S_Sobel_couleurs = "Sobel_couleurs"; String S_Gradient_Morphologique = "Gradient_Morphologique"; String S_Transformation_Lineaire = "Transformation_Lineaire"; String S_Nom_Image_resultat; public CNAM_image_alexis() { // le constructeur ici est très important : gestion d'une fenêtre interactive IHM // create windows setTitle ("Traitements d'images ");// => donne un titre à la fenêtre setSize(300,250); // => largeur et hauteur de la fenêtre setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // => argument indique action quand fermeture fenêtre setVisible (true); // => si true : permet d'afficher ce qui vient d'être créé // create content container Container contentArea = getContentPane(); contentArea.setBackground(Color.blue); // create layout-manager FlowLayout flowmanager = new FlowLayout(FlowLayout.CENTER); // caler les affichages sur la gauche de la fenêtre contentArea.setLayout(flowmanager); // new : add the event listeners image_trait.addItemListener(this); // (this) reçoit l'événement du composant validé par la méthode // addItemListener( ) // quand l'utilisateur sélectionne 1 article du menu déroulant, il est passé à la méthode de traitement de l'événement // itemStateChanged() // create Combo Box and add all the MMI Components image_trait.addItem("Choisir un des 6 traitements suivants :"); image_trait.addItem(S_Egalise_gris); image_trait.addItem(S_Egalise_couleurs); image_trait.addItem(S_Negative); image_trait.addItem(S_Sobel_couleurs); image_trait.addItem(S_Gradient_Morphologique); image_trait.addItem(S_Transformation_Lineaire); contentArea.add(image_trait); //contentArea.add(Label); contentArea.add(ChoiceLabel); setContentPane(contentArea); } public class afficherimage extends JFrame { public afficherimage() { super(S_Nom_Image_resultat); int i,j; int haut,larg; try { BufferedImage img = ImageIO.read(new File("images/"+Image_File+".jpg")); haut=img.getHeight(); larg=img.getWidth(); setSize(larg,haut); setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); setVisible(true); Container contentArea = getContentPane(); CustomPanel panel = new CustomPanel(); contentArea.add(panel); setContentPane(contentArea); img.flush(); } catch (Exception e) { System.err.println("erreur -> "+e.getMessage()); } } } class CustomPanel extends JPanel { public void paintComponent (Graphics painter) { Graphics2D painter2D = (Graphics2D) painter; Image pic= Toolkit.getDefaultToolkit().getImage(S_Nom_Image_resultat); painter2D.drawImage(pic, 0, 0, this); } } //new / SwingCheckBox ===>>>>>>> Add event handler (gestionnaire d'événements IHM) public void itemStateChanged(ItemEvent event) { // la méthode getItem() de ItemEvent retrouve la liste passée ItemEvent // Il est utile de convertir "this" en chaine de caractère via la méthode toString() pour // révêler l'article de la liste qui a été choisi try { BufferedImage img=ImageIO.read(new File("images/"+Image_File+".jpg")); int i,j; int haut,larg; haut=img.getHeight(); larg=img.getWidth(); int [][] pr= new int[larg][haut]; int [][] pv= new int[larg][haut]; int [][] pb= new int[larg][haut]; for ( i = 0; i < larg; i++) for (j = 0; j < haut; j++) { Color pixelcolor=new Color(img.getRGB(i,j)); pr[i][j]=pixelcolor.getRed(); pv[i][j]=pixelcolor.getGreen(); pb[i][j]=pixelcolor.getBlue(); } String choice = "toto"; choice = event.getItem().toString(); // Egalisation d'histogramme en niveaux de gris if (choice.compareTo(S_Egalise_gris) == 0) { long h [] = new long[256]; for(i=0;i<256;i++) h[i]=0; int moy; for ( i = 0; i < larg; i++) for (j = 0; j < haut; j++) { moy= (pr[i][j]+pv[i][j]+pb[i][j])/3; h[moy]++; } long hc [] = new long[256]; hc[0]=h[0]; for(i=1;i<256;i++) hc[i]=hc[i-1]+h[i]; long lut [] = new long[256]; for( i=0;i<256;i++) lut[i]=(hc[i]*(long)255)/((long)haut*(long)larg); for( i=0;i<256;i++) System.out.println(lut[i]); for (i = 0; i < larg; i++) for (j = 0; j < haut; j++) { moy= (pr[i][j]+pv[i][j]+pb[i][j])/3; int rgb=new Color((int)lut[moy],(int)lut[moy],(int)lut[moy]).getRGB(); // changer la couleur de pixel avec la nouvelle couleur invers�e img.setRGB(i, j, rgb); } S_Nom_Image_resultat = Image_File+"_"+ choice +".jpg"; ImageIO.write(img, "jpg",new File(S_Nom_Image_resultat)) ;//ImageIO.write()//; afficherimage eg2 = new afficherimage(); } // Egalisation d'histogramme avec les trois couleurs if (choice.compareTo(S_Egalise_couleurs) == 0) { // déclaration et initialisation des histogrammes des 3 couleurs long hr [] = new long[256]; long hv [] = new long[256]; long hb [] = new long[256]; for(i=0;i<256;i++) { hr[i]=0; hv[i]=0; hb[i]=0; } // calcul des histogrammes des trois couleurs for ( i = 0; i < larg; i++) for (j = 0; j < haut; j++) { hr[pr[i][j]]++; hv[pr[i][j]]++; hb[pr[i][j]]++; } // déclaration et initialisation des histogrammes cumulés des trois couleurs long hcr [] = new long[256]; long hcv [] = new long[256]; long hcb [] = new long[256]; hcr[0]=hr[0]; hcv[0]=hv[0]; hcb[0]=hb[0]; for(i=1;i<256;i++) { hcr[i]=hcr[i-1]+hr[i]; hcv[i]=hcv[i-1]+hv[i]; hcb[i]=hcb[i-1]+hb[i]; } // déclaration, initialisation et calcul des lut des trois couleurs // lut: Look Up Table : on ramène les valeurs HC comprise entre 0 et 255 long lutr [] = new long[256]; long lutv [] = new long[256]; long lutb [] = new long[256]; for( i=0;i<256;i++) { lutr[i]=(hcr[i]*(long)255)/((long)haut*(long)larg); lutv[i]=(hcv[i]*(long)255)/((long)haut*(long)larg); lutb[i]=(hcb[i]*(long)255)/((long)haut*(long)larg); } // affichage et vérification des lut[] des 3 couleurs for( i=0;i<256;i++) System.out.println("rouge"+lutr[i]); for( i=0;i<256;i++) System.out.println("vert"+lutv[i]); for( i=0;i<256;i++) System.out.println("bleu"+lutb[i]); // calcul de l'image résultat avec Egalise des couleurs for (i = 0; i < larg; i++) for (j = 0; j < haut; j++) { int rgb=new Color((int)lutr[pr[i][j]],(int)lutv[pv[i][j]],(int)lutb[pb[i][j]]).getRGB(); // changer la couleur de pixel avec la nouvelle couleur composée img.setRGB(i, j, rgb); } S_Nom_Image_resultat = Image_File+"_"+ choice +".jpg"; ImageIO.write(img, "jpg",new File(S_Nom_Image_resultat)) ;//ImageIO.write()//; afficherimage eg2 = new afficherimage(); } // Negatif if (choice.compareTo(S_Negative) == 0 ) { for ( i = 0; i < img.getWidth(); i++) for (j = 0; j < img.getHeight(); j++) { Color pixelcolor=new Color(img.getRGB(i,j)); pr[i][j]=pixelcolor.getRed(); pv[i][j]=pixelcolor.getGreen(); pb[i][j]=pixelcolor.getBlue(); pr[i][j]=255-pr[i][j] ; pv[i][j]=255-pv[i][j] ; pb[i][j]=255-pb[i][j] ; int rgb=new Color(pr[i][j],pv[i][j],pb[i][j]).getRGB(); // changer la couleur de pixel avec la nouvelle couleur composée img.setRGB(i, j, rgb); } S_Nom_Image_resultat = Image_File+"_"+ choice +".jpg"; ImageIO.write(img, "jpg",new File(S_Nom_Image_resultat)) ;//ImageIO.write()//; afficherimage eg2 = new afficherimage(); } // Calcul des contours avec Sobel Couleurs if (choice.compareTo(S_Sobel_couleurs) == 0 ) { int [][] prs= new int[larg][haut]; int [][] pvs= new int[larg][haut]; int [][] pbs= new int[larg][haut]; for ( i = 0; i < larg; i++) for (j = 0; j < haut; j++) { Color pixelcolor=new Color(img.getRGB(i,j)); pr[i][j]=pixelcolor.getRed(); pv[i][j]=pixelcolor.getGreen(); pb[i][j]=pixelcolor.getBlue(); prs[i][j]=0; pvs[i][j]=0; pbs[i][j]=0; } for ( i = 1; i < larg-1; i++) for (j = 1; j < haut-1; j++) { prs[i][j]=Math.abs(pr[i][j+1]-pr[i][j-1]); pvs[i][j]=Math.abs(pv[i][j+1]-pv[i][j-1]); pbs[i][j]=Math.abs(pb[i][j+1]-pb[i][j-1]); int rgb=new Color(prs[i][j],pvs[i][j],pbs[i][j]).getRGB(); // changer la couleur de pixel avec la nouvelle couleur composée img.setRGB(i, j, rgb); } S_Nom_Image_resultat = Image_File+"_"+ choice +".jpg"; ImageIO.write(img, "jpg",new File(S_Nom_Image_resultat)) ;//ImageIO.write()//; afficherimage eg2 = new afficherimage(); } // Calcul des contours par Gradient Morphologique if (choice.compareTo(S_Gradient_Morphologique) == 0 ) { int [][] prg= new int[larg][haut]; int [][] pvg= new int[larg][haut]; int [][] pbg= new int[larg][haut]; int k, l, maxr, maxv, maxb, minr, minv, minb ; for ( i = 0; i < larg; i++) for (j = 0; j < haut; j++) { Color pixelcolor=new Color(img.getRGB(i,j)); // récupération des pixels de couleur pr[i][j]=pixelcolor.getRed(); pv[i][j]=pixelcolor.getGreen(); pb[i][j]=pixelcolor.getBlue(); // initialisation des gradients avant calcul prg[i][j]=0; pvg[i][j]=0; pbg[i][j]=0; } // calcul du gradient morphologique // sur un voisinage 3x3 autour du pixel, faire la différence // entre la valeur max et la valeur min for ( i = 1; i < larg-1; i++) for (j = 1; j < haut-1; j++) { // initialisation des variables de travail maxr = 0; maxv = 0; maxb = 0; minr = 255; minv = 255; minb = 255; // calcul du min et du max pour chaque couleur autour du pixel for ( k = i-1; k <= i+1; k++ ) for ( l = j-1; l <= j+1; l++) { maxr = Math.max(maxr, pr[k][l]); maxv = Math.max(maxv, pv[k][l]); maxb = Math.max(maxb, pb[k][l]); minr = Math.min(maxr, pr[k][l]); minv = Math.min(maxv, pv[k][l]); minb = Math.min(maxb, pb[k][l]); } // calcul du gradient pour chaque couleur autour du pixel : max - min prg[i][j] = maxr-minr; // gradient du rouge pvg[i][j] = maxv-minv; // gradient du vert pbg[i][j] = maxb-minb; // gradient du bleu int rgb=new Color(prg[i][j],pvg[i][j],pbg[i][j]).getRGB(); // changer la couleur de pixel avec la nouvelle couleur composée img.setRGB(i, j, rgb); } S_Nom_Image_resultat = Image_File+"_"+ choice + ".jpg"; ImageIO.write(img, "jpg",new File(S_Nom_Image_resultat)) ;//ImageIO.write()//; afficherimage eg2 = new afficherimage(); } // Calcul de la transformation linéaire if (choice.compareTo(S_Transformation_Lineaire) == 0 ) { int prmin; int pvmin; int pbmin; int prmax; int pvmax; int pbmax; int k, l; for ( i = 0; i < larg; i++) for (j = 0; j < haut; j++) { Color pixelcolor=new Color(img.getRGB(i,j)); // récupération des pixels de couleur pr[i][j]=pixelcolor.getRed(); pv[i][j]=pixelcolor.getGreen(); pb[i][j]=pixelcolor.getBlue(); } // calcul des valeurs min et max pour chaque couleur prmax = 0; pvmax = 0; pbmax = 0; prmin = 255; pvmin = 255; pbmin = 255; for ( i = 1; i < img.getWidth()-1; i++) for (j = 1; j < img.getHeight()-1; j++) { prmax = Math.max(prmax , pr[i][j]); pvmax = Math.max(pvmax , pv[i][j]); pbmax = Math.max(pbmax , pb[i][j]); prmin = Math.min(prmin , pr[i][j]); pvmin = Math.min(pvmin , pv[i][j]); pbmin = Math.min(pbmin , pb[i][j]); } // déclaration et initialisation des histogrammes des 3 couleurs long hr [] = new long[256]; long hv [] = new long[256]; long hb [] = new long[256]; for(i=0;i<256;i++) { hr[i]=0; hv[i]=0; hb[i]=0; } // calcul des histogrammes des trois couleurs for ( i = 0; i < larg; i++) for (j = 0; j < haut; j++) { hr[pr[i][j]]++; hv[pr[i][j]]++; hb[pr[i][j]]++; } // calcul du LUT : (valeur h[i] - min )/(max-min) long lutr [] = new long[256]; long lutv [] = new long[256]; long lutb [] = new long[256]; for( i=0;i<256;i++) { lutr[i]=((long)hr[i]- (long)prmin)/((long)prmax-(long)prmin); lutv[i]=((long)hv[i]- (long)pvmin)/((long)pvmax-(long)pvmin); lutb[i]=((long)hb[i]- (long)pbmin)/((long)pbmax-(long)pbmin); } // affichage et vérification des lut[] des 3 couleurs for( i=0;i<256;i++) System.out.println("rouge"+lutr[i]); for( i=0;i<256;i++) System.out.println("vert"+lutv[i]); for( i=0;i<256;i++) System.out.println("bleu"+lutb[i]); // calcul de l'image résultat for (i = 0; i < larg; i++) for (j = 0; j < haut; j++) { int rgb=new Color((int)lutr[pr[i][j]],(int)lutv[pv[i][j]],(int)lutb[pb[i][j]]).getRGB(); // changer la couleur de pixel avec la nouvelle couleur composée img.setRGB(i, j, rgb); } S_Nom_Image_resultat = Image_File+"_"+ choice +".jpg"; ImageIO.write(img, "jpg",new File(S_Nom_Image_resultat)) ;//ImageIO.write()//; afficherimage eg2 = new afficherimage(); } } catch (Exception e) { System.err.println("erreur -> "+e.getMessage()); } } public static void main(String[] args) { CNAM_image_alexis eg = new CNAM_image_alexis(); } }