Précédent   Forum du club des développeurs et IT Pro > Environnements de développement > MATLAB > Téléchargez
Téléchargez Récupérez et commentez les sources et outils mis à disposition par la rubrique MATLAB
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 12/10/2010, 16h24   #1
Dut
Responsable MATLAB & Hardware/PC

 
Avatar de Dut
 
Inscription : novembre 2006
Messages : 15 102
Détails du profil
Informations personnelles :
Localisation : France

Informations forums :
Inscription : novembre 2006
Messages : 15 102
Points : 31 169
Points : 31 169
Par défaut [image] Générer une image avec des pixels non rectangulaires (losange, octogone et hexagone)



voici une fonction qui imite le fonctionnement de la fonction IMAGE de MATLAB mais qui produit des pixels non rectangulaires.

Code :
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
function pixshape(X,shape)
% PIXSHAPE Display image with non-rectangular pixels
%

% Author : Jerome Briot (Dut)
% Contact : dutmatlab#yahoo#fr -or- briot#cict#fr
% Profil : www.mathworks.com/matlabcentral/newsreader/author/94805
%        : http://www.developpez.net/forums/member.php?u=125006
%
% Version : 1.0 - 12 Oct 2010
%

% MATLAB : 7.8.0.347 (R2009a)
% System : Linux 2.6.32-25-generic
%

error(nargchk(2,2,nargin));

ndX = ndims(X);

if ndX == 2 %Indexed
    nX = numel(X);
    cdata = X(:).';
elseif ndX == 3 %RGB
    nX = size(X,1)*size(X,2);
    cdata = reshape(X,1,nX,3);
else
    error('Indexed CData must be size [MxN], TrueColor CData must be size [MxNx3]')
end

switch class(X)
    
    case 'double'
        %
    case {'uint8' 'int8'}
        
        if ndX==3
            cdata = double(cdata)/255;
        else
            cdata = double(cdata)+1;
        end
        
    case {'uint16' 'int16'}
        
        if ndX==3
            cdata = double(cdata)/65535;
        else
            cdata = double(cdata)+1;
        end
        
    case 'logical'
        cdata = double(cdata);
        
    otherwise
        error('Numeric or logical matrix required for image CData')
end

if ndX==3 && (min(cdata(:))<0 || max((cdata(:))>1))
    error('TrueColor CData contains element out of range 0.0 <= value <= 1.0')
end

xshape = [];
yshape = [];

getshape;

if nargout == 0
    
    patch(xshape,yshape,'r','facecolor','flat','cdata',cdata,'edgecolor','none');
else
    h = patch(xshape,yshape,'r','facecolor','flat','cdata',cdata,'edgecolor','none');
end
axis equal ij off tight

    function getshape
        
        switch shape
            
            case 'rhb'
                
                xsqr = [1 0 -1 0];
                ysqr = [0 1 0 -1];
                
                xshape = repmat(xsqr(:),1,nX);
                yshape = repmat(ysqr(:),1,nX);
                
                B= repmat(0:2:2*(size(X,2)-1),size(X,1),1);
                xshape = bsxfun(@plus,xshape,B(:).');
                B= repmat(0:2:2*(size(X,1)-1),1,size(X,2));
                yshape = bsxfun(@plus,yshape,B);
                
            case 'oct'
                
                xoct = [0.3827 0.9239 0.9239 0.3827 -0.3827 -0.9239 -0.9239 -0.3827];
                yoct = [0.9239 0.3827 -0.3827 -0.9239 -0.9239 -0.3827 0.3827 0.9239];
                
                xshape = repmat(xoct(:),1,nX);
                yshape = repmat(yoct(:),1,nX);
                
                c = cos(pi/8);
                
                B= repmat(0:2:2*(size(X,2)-1),size(X,1),1);
                xshape = bsxfun(@plus,xshape,c*B(:).');
                B= repmat(0:2:2*(size(X,1)-1),1,size(X,2));
                yshape = bsxfun(@plus,yshape,c*B);
                
            case 'hex'
                
                xhex = [0.5 1.0 0.5 -0.5 -1.0 -0.5 ];
                yhex = [0.866 0.000 -0.866 -0.866 -0.000 0.866];
                
                xshape = repmat(xhex(:),1,nX);
                yshape = repmat(yhex(:),1,nX);
                
                B= repmat(0:2:2*(size(X,2)-1),size(X,1),1);
                xshape = bsxfun(@plus,xshape,B(:).');
                B= repmat(0:1:(size(X,1)-1),1,size(X,2));
                yshape = bsxfun(@plus,yshape,1.732*B);
                
            otherwise
                error('The shape argument must be one of ''rhb'', ''oct'' or ''hex''}')
                
        end
        
    end

end
Le premier argument X est le même que pour la fonction IMAGE.
Cela peut être un tableau 2D MxN (image en couleurs indexées) ou 3D MxNx3 (image en couleurs vraies)

Le second argument shape définit la forme des pixels :
  • 'rhb' : losange
  • 'oct' : octogone
  • 'hex' : hexagone

L'objet affiché n'est pas de type Image mais de type Patch.
On peut récupérer son identifiant en utilisant l'appel suivant :

Un fichier demo :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
X = rand(4,6,3);

figure('color','w','numbertitle','off','name','pixshape.m demo')
subplot(221)
image(X);
axis image off
title('image(X)')
subplot(222)
pixshape(X,'rhb');
axis off equal tight
title('pixshape(X,''rhb'');')
subplot(223)
pixshape(X,'hex');
axis off equal tight
title('pixshape(X,''hex'');')
subplot(224)
pixshape(X,'oct');
axis off equal tight
title('pixshape(X,''oct'');')
qui produit la figure attachée à ce message.

N'hésitez pas à poster vos remarques ou commentaires à la suite de ce message.
Images attachées
Type de fichier : png pixshape.png (12,9 Ko, 18 affichages)
__________________
Identification de processeur sous MATLAB (3/3) Identification de processeur sous MATLAB (2/3) Mes contributions MATLAB (R2009a - Windows & Linux)

J'étais le meilleur ami que le vieux Jim avait au monde. Il fallait choisir. J'ai réfléchi un moment, puis je me suis dit : "Tant pis ! J'irai en enfer" (Saint Huck)
Dut est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 12/10/2010, 17h13   #2
magelan
Modérateur
 
Inscription : août 2007
Messages : 4 106
Détails du profil
Informations forums :
Inscription : août 2007
Messages : 4 106
Points : 5 448
Points : 5 448
Salut Dut,

dans les logiciels de traitement d'images qui utilisent des grilles hexagonales pour les pixels, en général (enfin ceux que je connais), c'est un pavage régulier qui ne possède pas de trous entre les hexagones. Ce qui implique que 2 lignes consécutives ne possèdent pas le même nombre de pixels.

Mais bon, ton code donne tous les éléments pour créer ce type d'image.

Bonne contribution
__________________
Pour une bonne utilisation des balises code c'est ici!
Petit guide du voyageur MATLABien : Le forum La faq Les tutoriels Les sources


La nature est un livre écrit en langage mathématique. Galilée.
magelan est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Vieux 12/10/2010, 17h25   #3
Dut
Responsable MATLAB & Hardware/PC

 
Avatar de Dut
 
Inscription : novembre 2006
Messages : 15 102
Détails du profil
Informations personnelles :
Localisation : France

Informations forums :
Inscription : novembre 2006
Messages : 15 102
Points : 31 169
Points : 31 169
Citation:
Envoyé par magelan Voir le message
dans les logiciels de traitement d'images qui utilisent des grilles hexagonales pour les pixels, en général (enfin ceux que je connais), c'est un pavage régulier qui ne possède pas de trous entre les hexagones. Ce qui implique que 2 lignes consécutives ne possèdent pas le même nombre de pixels.
Je me doutais bien... c'est pour ça que j'ai une version 2 en cours... mais je n'ai pas fini de coder tous les pattern !

Je fais ça au plus tôt
__________________
Identification de processeur sous MATLAB (3/3) Identification de processeur sous MATLAB (2/3) Mes contributions MATLAB (R2009a - Windows & Linux)

J'étais le meilleur ami que le vieux Jim avait au monde. Il fallait choisir. J'ai réfléchi un moment, puis je me suis dit : "Tant pis ! J'irai en enfer" (Saint Huck)
Dut est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 13/10/2010, 10h40   #4
Dut
Responsable MATLAB & Hardware/PC

 
Avatar de Dut
 
Inscription : novembre 2006
Messages : 15 102
Détails du profil
Informations personnelles :
Localisation : France

Informations forums :
Inscription : novembre 2006
Messages : 15 102
Points : 31 169
Points : 31 169
Voici une version 1.1 avec quelques patterns supplémentaires :

Code :
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
function pixshape(X,shape,pattern)
% PIXSHAPE Display image with non-rectangular pixels
%

% Author : Jerome Briot (Dut)
% Contact : dutmatlab#yahoo#fr -or- briot#cict#fr
% Profil : www.mathworks.com/matlabcentral/newsreader/author/94805
%        : http://www.developpez.net/forums/member.php?u=125006
%
% Version : 1.0 - 12 Oct 2010
%           1.1 - 13 Oct 2010 : Add patterns for hex shape

% MATLAB : 7.8.0.347 (R2009a)
% System : Linux 2.6.32-25-generic
%

error(nargchk(2,3,nargin));

ndX = ndims(X);

if ndX == 2 %Indexed
    nX = numel(X);
    cdata = X(:).';
elseif ndX == 3 %RGB
    nX = size(X,1)*size(X,2);
    cdata = reshape(X,1,nX,3);
else
    error('Indexed CData must be size [MxN], TrueColor CData must be size [MxNx3]')
end

switch class(X)
    
    case 'double'
        %
    case {'uint8' 'int8'}
        
        if ndX==3
            cdata = double(cdata)/255;
        else
            cdata = double(cdata)+1;
        end
        
    case {'uint16' 'int16'}
        
        if ndX==3
            cdata = double(cdata)/65535;
        else
            cdata = double(cdata)+1;
        end
        
    case 'logical'
        cdata = double(cdata);
        
    otherwise
        error('Numeric or logical matrix required for image CData')
end

if ndX==3 && (min(cdata(:))<0 || max((cdata(:))>1))
    error('TrueColor CData contains element out of range 0.0 <= value <= 1.0')
end

xshape = [];
yshape = [];

getshape;

if nargout == 0
    
    patch(xshape,yshape,'r','facecolor','flat','cdata',cdata,'edgecolor','none');
else
    h = patch(xshape,yshape,'r','facecolor','flat','cdata',cdata,'edgecolor','none');
end
axis equal ij off tight

    function getshape
        
        switch shape
            
            case 'rhb'
                
                xsqr = [1 0 -1 0];
                ysqr = [0 1 0 -1];
                
                xshape = repmat(xsqr(:),1,nX);
                yshape = repmat(ysqr(:),1,nX);
                
                B= repmat(0:2:2*(size(X,2)-1),size(X,1),1);
                xshape = bsxfun(@plus,xshape,B(:).');
                B= repmat(0:2:2*(size(X,1)-1),1,size(X,2));
                yshape = bsxfun(@plus,yshape,B);
                
            case 'oct'
                
                xoct = [0.3827 0.9239 0.9239 0.3827 -0.3827 -0.9239 -0.9239 -0.3827];
                yoct = [0.9239 0.3827 -0.3827 -0.9239 -0.9239 -0.3827 0.3827 0.9239];
                
                xshape = repmat(xoct(:),1,nX);
                yshape = repmat(yoct(:),1,nX);
                
                c = cos(pi/8);
                
                B= repmat(0:2:2*(size(X,2)-1),size(X,1),1);
                xshape = bsxfun(@plus,xshape,c*B(:).');
                B= repmat(0:2:2*(size(X,1)-1),1,size(X,2));
                yshape = bsxfun(@plus,yshape,c*B);
                
            case 'hex'
                
                xhex = [0.5 1.0 0.5 -0.5 -1.0 -0.5 ];
                yhex = [0.866 0.000 -0.866 -0.866 -0.000 0.866];
                
                xshape = repmat(xhex(:),1,nX);
                yshape = repmat(yhex(:),1,nX);
                
                if pattern == 1
                    B= repmat(0:2:2*(size(X,2)-1),size(X,1),1);
                    xshape = bsxfun(@plus,xshape,B(:).');
                    B= repmat(0:1:(size(X,1)-1),1,size(X,2));
                    yshape = bsxfun(@plus,yshape,1.732*B);
                    
                elseif pattern == 2
                    B = repmat(0:1.5:1.5*(size(X,2)-1),size(X,1),1);
                    xshape = bsxfun(@plus,xshape,B(:).');
                    
                    B = zeros(1,size(X,2));
                    B(2:2:end) = -1;
                    B = repmat(B,size(X,1),1);
                    B = bsxfun(@plus,B,(0:2:2*(size(X,1)-1)).');
                    B = B*cos(pi/6);
                    yshape = bsxfun(@plus,yshape,B(:).');
                    
                elseif pattern==3
                    B = repmat(0:1.5:1.5*(size(X,2)-1),size(X,1),1);
                    xshape = bsxfun(@plus,xshape,B(:).');
                    
                    B = zeros(1,size(X,2));
                    B(2:2:end) = 1;
                    B = repmat(B,size(X,1),1);
                    B = bsxfun(@plus,B,(0:2:2*(size(X,1)-1)).');
                    B = B*cos(pi/6);
                    yshape = bsxfun(@plus,yshape,B(:).');
                end
                
            otherwise
                error('The shape argument must be one of ''rhb'', ''oct'' or ''hex''}')
                
        end
        
    end

end
La suite... un peu plus tard

Si vous avez des suggestions... n'hésitez pas.

J'avoue que personnellement, je n'ai jamais eu à utiliser de telles "images".
Ce n'est pas du tout mon domaine.
Images attachées
Type de fichier : png pixshapeV1.1.png (12,6 Ko, 23 affichages)
__________________
Identification de processeur sous MATLAB (3/3) Identification de processeur sous MATLAB (2/3) Mes contributions MATLAB (R2009a - Windows & Linux)

J'étais le meilleur ami que le vieux Jim avait au monde. Il fallait choisir. J'ai réfléchi un moment, puis je me suis dit : "Tant pis ! J'irai en enfer" (Saint Huck)
Dut est déconnecté   Envoyer un message privé Réponse avec citation 10
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 17h20.


 
 
 
 
Partenaires

Hébergement Web