Matrice niveau de gris Java.

edbarbier Messages postés 7 Date d'inscription jeudi 12 novembre 2009 Statut Membre Dernière intervention 18 novembre 2009 - 12 nov. 2009 à 22:50
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 - 17 nov. 2009 à 15:11
suis débutant. Le programme compile mais ne me renvoi rien.
En faisant divers tests, j'ai eu parfois des erreurs de compilation du genre variable pas initialisées, ou bien le programme me renvoyait des valeurs bizares. Bref j'ai besoin d'un petit coup de main.

import java.lang.*;
import tp3.*;
class tp3
{
public static int [][] getImageData(String path)
{

int y = 0; // COLONNES
int x = 0; // LIGNES	
int [][] tab = new int [0][0];
path = new String ("C:\\Users\\Utilisateur\\Desktop\\UCL\\ECGE12BA\\sinf1160\\test.jpg"); 
for(x = 0; x < tab.length;x++)
{ 
for(y = 0; y < tab[x].length; y++)
{
System.out.println(tab[y]+","+tab[x]);

}

}

return tab;
}

public static void main(String [] args)
{
int [][] tab;
int x;
int y;
String path = ("C:\\Users\\Utilisateur\\Desktop\\UCL\\ECGE12BA\\sinf1160\\test.jpg");
tab= getImageData(path);
System.out.println("Voici l'image mise sous forme de tableau");
}
}

3 réponses

cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
12 nov. 2009 à 23:06
Salut,

Tu es encore très loin du résultat que tu cherches à obtenir : ton code ne fait rien du tout actuellement : il ne fait que créer un tableau de taille 0 et une chaîne de caractère codée en dur...
Il te faut ouvrir le fichier, récupèrer la matrice de pixel, calculer la taille, créer ton tableau en conséquence, extraire les pixels un à un afin de calcul le niveau de gris correspondant, bref, il te reste encore beaucoup de boulot !

Déjà, pour corriger un minimum ton code :
import java.lang.*;
import tp3.*;
class tp3
{
public static int [][] getImageData(String path)
{

int y = 0; // COLONNES
int x = 0; // LIGNES	
        int nombreLigne = 10; // à calculer bien entendu !
        int nombreColonne = 10; // à calculer bien entendu !
int [][] tab = new int [nombreLigne][nombreColonne];
File fichier = new File(path); 
for(x = 0; x < tab.length;x++)
{ 
for(y = 0; y < tab[x].length; y++)
{
// System.out.println(tab[y]+","+tab[x]); TU N'OBTIENDRAS RIEN AVEC CETTE LIGNE TANT QUE TON TABLEAU NE SERA PAS REMPLIT...

}

}

return tab;
}

public static void main(String [] args)
{
int [][] tab;
int x;
int y;
String path = ("C:\\Users\\Utilisateur\\Desktop\\UCL\\ECGE12BA\\sinf1160\\test.jpg");
tab= getImageData(path);
System.out.println("Voici l'image mise sous forme de tableau");
}
}

______________________________________
DarK Sidious
0
edbarbier Messages postés 7 Date d'inscription jeudi 12 novembre 2009 Statut Membre Dernière intervention 18 novembre 2009
12 nov. 2009 à 23:22
d'accord merci beaucoup!

c'est déja un début ... je vais continuer à bosser dessus...
0
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
17 nov. 2009 à 15:11
Salut,

Tiens une méthode qui permet de convertir un point RGB[A] en niveau de gris:

private int convert2Gray(int rgb, boolean addAlpha) {
  int r = (rgb & 0xFF0000) >> 16;
  int g = (rgb & 0xFF00) >> 8;
  int b = (rgb & 0xFF);
  int moy = ((r + g + b) / 3);
  int alpha = 0;
  if(addAlpha) alpha = (rgb & 0xFF000000);
  return alpha + (moy << 16) + (moy << 8) + (moy);
}









------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

OoWORAoO
0
Rejoignez-nous