IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Calcul scientifique Python Discussion :

Construire une image RVB (à partir image multispectrale)


Sujet :

Calcul scientifique Python

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    91
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 91
    Par défaut Construire une image RVB (à partir image multispectrale)
    Bonjour à tous,
    J'essaie de construire une image RVB à partir de trois images monochromes (des tableaux numpy.array):
    • R, G, B

    . J'ai suivi les indications de l'auteur de astrobetter.Le code est censé produire un tableau "rgb".
    Dans mon code cela donne:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    #Reconstitute original image size
    #should be a red image of size =shape
    R=c3_r.reshape(shape)
    G=c3_g.reshape(shape)
    B=c3_b.reshape(shape)
    #Try to build a rgb image
    rgb = np.zeros((shape[0],shape[1],3),dtype=float)
    print rgb.shape
    mxr=np.max(R)
    mxg=np.max(G)
    mxb=np.max(B)
    #normalize to 0-255 uint8
    Rnorm=np.uint8((255*(R/mxr)))
    Gnorm=np.uint8((255*(R/mxg)))
    Bnorm=np.uint8((255*(R/mxb)))
    #copy each RGB component in an RGB array
    rgb[:,:,0]=Rnorm
    rgb[:,:,1]=Gnorm
    rgb[:,:,2]=Bnorm
    Quand j'essaie d'afficher cette image avec:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    pylab.gray()
    pylab.subplot(224,frameon=False, xticks=[], yticks=[])
    pylab.imshow(rgb)
    pylab.show()
    Je ne vois pas une image rvb mais une image en niveaux de gris avec un fond blanc (au lieu de noir) et je n'arrive pas à la sauver en png.

    Pour raconter toute l'histoire, j'ai au départ cinq images correspondant une scène vue à différentes longueur d'ondes (du bleu au proche infra rouge). Je souhaite combiner ces cinq images en trois pour les visualiser simultannement. On peut faire cela en quelques clic avec imagej et le plugins image5D.
    J'essaie de refaire la même chose en python+numpy+pylab. J'aurais également des questions sur la construction de la matrice m.Voila le code complet:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    import numpy as np
    import readmagick
    import os
    import pylab
    user=os.path.expanduser("~")
    workdir=os.path.join(user,"Applications","ImagesTest","MFISH")
    blue="Aqua.tif"
    green="Green.tif"
    gold="Gold.tif"
    red="Red.tif"
    frd="FarRed.tif"
    complete_path=os.path.join(workdir,blue)
    i1=readmagick.readimg(complete_path)
    #
    complete_path=os.path.join(workdir,green)
    i2=readmagick.readimg(complete_path)
    #
    complete_path=os.path.join(workdir,gold)
    i3=readmagick.readimg(complete_path)
    #
    complete_path=os.path.join(workdir,red)
    i4=readmagick.readimg(complete_path)
    #
    complete_path=os.path.join(workdir,frd)
    i5=readmagick.readimg(complete_path)
    #
    shape=i5.shape
     
    b=i1.flatten()
    g=i2.flatten()
    y=i3.flatten()
    r=i4.flatten()
    f=i5.flatten()
    #make a 2D array
    #each line is a pixel, column=channel
    channel5=np.vstack((f,r,y,g,b))
    c5=channel5.T
    #print c5.shape
    #try some coef
    m=np.array([[0.80, 0.10, 0.01],
                [0.10, 0.80, 0.04],
                [0.10, 0.05, 0.10],
                [0.00, 0.03, 0.15],
                [0.00, 0.02, 0.70]])
    print m.shape
     
    #produce  a flat rgb image
    #combine linearly the five channels into three RGB channels
    c3=np.dot(c5,m)
     
    #isolate R,G,B component
    c3_r=c3[:,0]
    c3_g=c3[:,1]
    c3_b=c3[:,2]
    #Reconstitute original image size
    #should be a red image of size =shape
    R=c3_r.reshape(shape)
    G=c3_g.reshape(shape)
    B=c3_b.reshape(shape)
    #Try to build a rgb image
    rgb = np.zeros((shape[0],shape[1],3),dtype=float)
    print rgb.shape
    mxr=np.max(R)
    mxg=np.max(G)
    mxb=np.max(B)
    #normalize to 0-255 uint8
    Rnorm=np.uint8((255*(R/mxr)))
    Gnorm=np.uint8((255*(R/mxg)))
    Bnorm=np.uint8((255*(R/mxb)))
    #copy each RGB component in an RGB array
    rgb[:,:,0]=Rnorm
    rgb[:,:,1]=Gnorm
    rgb[:,:,2]=Bnorm
    #complete_path=os.path.join(workdir,"R.png")
    #readmagick.writeimg(R,complete_path)
    pylab.gray()
    #
    pylab.subplot(221,frameon=False, xticks=[], yticks=[])
    pylab.imshow(Rnorm)
    #complete_path=os.path.join(workdir,"R.png")
    #readmagick.writeimg(Rnorm,complete_path)
    #
    pylab.subplot(222,frameon=False, xticks=[], yticks=[])
    pylab.imshow(Gnorm)
    #complete_path=os.path.join(workdir,"G.png")
    #readmagick.writeimg(Gnorm,complete_path)
    #
    pylab.subplot(223,frameon=False, xticks=[], yticks=[])
    pylab.imshow(Bnorm)
    #complete_path=os.path.join(workdir,"B.png")
    #readmagick.writeimg(Bnorm,complete_path)
    #pylab.clf()
    pylab.subplot(224, aspect='equal',frameon=False, xticks=[], yticks=[])
    pylab.imshow(rgb)
    pylab.show()

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    91
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 91
    Par défaut
    J'ai simplifié mon code vite fait mal fait sans algèbre lineaire.

    Le résultat n'est surrement pas optimal

  3. #3
    Membre émérite
    Homme Profil pro
    Ingénieur R&D en apprentissage statistique
    Inscrit en
    Juin 2009
    Messages
    447
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur R&D en apprentissage statistique

    Informations forums :
    Inscription : Juin 2009
    Messages : 447
    Par défaut
    Bonjour,

    je pars du principe que tu as récupéré tes images dans i1,i2,...,i5:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    multi_spectr = np.dstack([i1,i2,i3,i4,i5])
    conv_matrix = np.array([
    ...
    ])
     
    shape = multi_spectr.shape
    multi_spectr = multi_spectr.reshape(shape[0]*shape[1],shape[2])
    rgb = np.dot(multi_spectr,conv_matrix.T).reshape(shape[:2]+(3,))

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    91
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 91
    Par défaut
    merci, je vais essayer cela.

    edit:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    # -*- coding: utf-8 -*-
    """
    Created on Mon May 23 14:07:40 2011
     
    @author: jean-pat
    """
     
    import numpy as np
    import os
    import pylab
    #from scipy import linalg as la
    #from scipy import ndimage as nd
    import scipy as sp
    user=os.path.expanduser("~")
    workdir=os.path.join(user,"Applications","ImagesTest","MFISH")
    blue="Aqua.tif"
    green="Green.tif"
    gold="Gold.tif"
    red="Red.tif"
    frd="FarRed.tif"
    complete_path=os.path.join(workdir,blue)
    i1=sp.misc.imread(complete_path)
    #
    complete_path=os.path.join(workdir,green)
    i2=sp.misc.imread(complete_path)
    #
    complete_path=os.path.join(workdir,gold)
    i3=sp.misc.imread(complete_path)
    #
    complete_path=os.path.join(workdir,red)
    i4=sp.misc.imread(complete_path)
    #
    complete_path=os.path.join(workdir,frd)
    i5=sp.misc.imread(complete_path)
    ######################
    multi_spectr = np.dstack([i1,i2,i3,i4,i5])
    conv_matrix = np.array([
    [0.8,0.15,0.05,0,0],
    [0,0.05,0.05,0.8,0.1],
    [0,0,0.05,0.15,0.8]
    ])
     
    shape = multi_spectr.shape
    multi_spectr = multi_spectr.reshape(shape[0]*shape[1],shape[2])
    rgb = np.dot(multi_spectr,conv_matrix.T).reshape(shape[:2]+(3,))
    rgb8=np.uint8(rgb)
    ########################
    #R=0.8*i5+0.15*i4+0.05*i3+0.00*i2+0.00*i1
    #V=0.00*i5+0.05*i4+0.05*i3+0.80*i2+0.10*i1
    #B=0.00*i5+0.00*i4+0.05*i3+0.15*i2+0.80*i1
    #shape=i5.shape
    #rgb = np.zeros((shape[0],shape[1],3),dtype=np.uint8)
    #rgb[:,:,0]=np.copy(R)
    #rgb[:,:,1]=np.copy(V)
    #rgb[:,:,2]=np.copy(B)
    pylab.subplot(111,frameon=False, xticks=[], yticks=[])
    pylab.imshow(rgb8)
    pylab.show()
    Il faut convertir en 8 bits pour voir l'image avec pylab.

    merci

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    91
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 91
    Par défaut
    Au passage, existe t-il de "meilleures" matrices conv_matrix que d'autres?
    A mon sens une bonne matrice est une matrice qui permet au mieux de séparer les couleurs.
    On peut imaginer qu'il en existe une bonne qui donne une image RVB, cette image peut ensuite être convertie en HSI. Par rotation de la composante hue on peut d'une image à une autre. Il devrait donc y avoir "beaucoup" de matrices "équivalentes" parce qu'il est possible de passer d'une image RGB à l'autre en modifiant la composante Hue.

    Initialement, les cinqs images de départ codent pour 24 couleurs différentes. Peut pratiquement faire une ACP (SVD...) pour construire cette image RVB?

    merci

    Jean-Patrick

  6. #6
    Membre émérite
    Homme Profil pro
    Ingénieur R&D en apprentissage statistique
    Inscrit en
    Juin 2009
    Messages
    447
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur R&D en apprentissage statistique

    Informations forums :
    Inscription : Juin 2009
    Messages : 447
    Par défaut
    Effectivement une ACP aurait l'avantage d'être optimale pour tes données, tu peux imaginer prendre une ou plusieurs photos pour calculer la matrice de conversion une fois pour toute.

  7. #7
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    91
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 91
    Par défaut
    Citation Envoyé par Alexis.M Voir le message
    Bonjour,

    je pars du principe que tu as récupéré tes images dans i1,i2,...,i5:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    multi_spectr = np.dstack([i1,i2,i3,i4,i5])
    conv_matrix = np.array([
    ...
    ])
     
    shape = multi_spectr.shape
    multi_spectr = multi_spectr.reshape(shape[0]*shape[1],shape[2])
    rgb = np.dot(multi_spectr,conv_matrix.T).reshape(shape[:2]+(3,))
    Avec la même matrice le résultat est différent, ce qui était bleu devient rouge

  8. #8
    Membre émérite
    Homme Profil pro
    Ingénieur R&D en apprentissage statistique
    Inscrit en
    Juin 2009
    Messages
    447
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur R&D en apprentissage statistique

    Informations forums :
    Inscription : Juin 2009
    Messages : 447
    Par défaut
    C'est parce que dans ton code original tu utilises les images dans l'ordre inverse [i5,i4,i3,i2,i1]

  9. #9
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    91
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2009
    Messages : 91
    Par défaut


    merci encore

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. fonction vtk ou (autre)pour reconstruire une image 3D à partir image(s) 2D
    Par wassimbik dans le forum Traitement d'images
    Réponses: 3
    Dernier message: 24/02/2012, 15h51
  2. Réponses: 8
    Dernier message: 25/01/2010, 09h35
  3. [Débutant] construire une surface 3D à partir d'un fichier contenant 3 colonnes
    Par galega dans le forum MATLAB
    Réponses: 1
    Dernier message: 19/06/2009, 15h47
  4. Réponses: 4
    Dernier message: 05/12/2006, 09h07
  5. [Débutant][Image] créer un Image à partir d'un .jpeg
    Par molusk dans le forum Entrée/Sortie
    Réponses: 4
    Dernier message: 07/07/2005, 11h26

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo