Problème YUV->RGB

silvervana Messages postés 10 Date d'inscription mardi 21 février 2006 Statut Membre Dernière intervention 16 mai 2006 - 4 avril 2006 à 11:53
molloc459 Messages postés 1 Date d'inscription lundi 6 mars 2006 Statut Membre Dernière intervention 15 mai 2008 - 15 mai 2008 à 18:56
Bonjour,

Je voudrais pouvoir passer d'un mode RGB à un mode YUV, et inversement. Le problème c'est qu'en partant d'une image RGB que je modifie en YUV, puis que je remodifie en RGB, je n'arrive pas à retrouver mon image de départ... je pense à un problème de conversion mais je ne vois pas comment faire.... Je joint le code, merci pour vos idées et commentaires !!!

Code :
//je récupère mon image et je lance ma méthode RGBtoYUV(rgbsImage, rgbsImage)
File fImg = new File (szPathImage);
Image = ImageIO.read (fImg);


//récupère les pixels de l'image
int iWidthImage = Image.getWidth ();
int iHeightImage = Image.getHeight ();
int [] rgbsImage = new int[iWidthImage * iHeightImage];


Image.getRGB (0, 0,iWidthImage, iHeightImage, rgbsImage, 0, iWidthImage);
RGBtoYUV(rgbsImage, rgbsImage);


//puis je lance l'inverse et je réécris...
YUVtoRGB(rgbsImage, rgbsImage);
BufferedImage im = new BufferedImage(Image.getWidth(), Image.getHeight(),BufferedImage.TYPE_INT_RGB);
im.setRGB (0, 0,Image.getWidth(), Image.getHeight(), rgbsImage, 0, Image.getWidth());
// On sauve l'image
File fic= new File(szNom+ "." + szFormat);
ImageIO.write(im, szFormat, fic);

Voici mes méthodes :

private void RGBtoYUV(int [] rgbs, int [] yuvs)
{
for(int i=0; i<rgbs.length; i++)
{
int rgb = rgbs[i];
int A = (rgb >>24 ) & 0xff;
int R = (rgb >>16 ) & 0xFF;
int G = (rgb >> 8 ) & 0xFF;
int B = rgb & 0xFF;


/*int Y = (int)(0.299* R + 0.587 * G + 0.114 *B);
int U = (int)(-0.147*R - 0.289*G +0.436*B);
int V = (int)(0.615*R -0.515*G - 0.100*B);*/


int Y = (int)(0.299 * R + 0.587 * G + 0.114 * B);
int U = (int)(R - Y);
int V = (int)(B - Y);


rgb = (A<<24)+(Y<<16)+(U<<8)+V;
yuvs[i] = (int)rgb;
}
}



private void YUVtoRGB(int [] yuvs, int [] rgbs )
{
for(int i=0; i<rgbs.length; i++)
{
int yuv = yuvs[i];
int A = (yuv >>24 ) & 0xff;
int Y = (yuv >>16 ) & 0xFF;
int U = (yuv >> 8 ) & 0xFF;
int V = yuv & 0xFF;


/*int R = (int)(Y + 1.140 * V);
int G = (int)(Y - 0.395 * U - 0.581 * V);
int B = (int)(Y + 2.032 * U);*/


int R = (int)(U + Y);
int B = (int)(V + Y);
int G = (int)((Y*1.7 - R*0.509 - B*0.194));


yuv = (A<<24)+(R<<16)+(G<<8)+B;
rgbs[i] = (int)yuv;
}
}

Merci !!!!!

1 réponse

molloc459 Messages postés 1 Date d'inscription lundi 6 mars 2006 Statut Membre Dernière intervention 15 mai 2008
15 mai 2008 à 18:56
Salut,

j'ai juste regardé ton code de conversion  de rgb vers HUV et y'a une erreur, voici la conversion :

        int r = (rgb>>16) & 0xFF;
        int g = (rgb>>8) & 0xFF;
        int b = rgb & 0xFF;

        int y = 0.299 *r + 0.587 * g + 0.114 * b;
        int u = 0.493 * (b - y);
        int v = 0.877 * (r - y);

Voila, pr l'inverse un simple calcul mathématiques et le tour est joué !!

J'espere avoir résolu ton pb !!
0
Rejoignez-nous