Accueil > > > IMAGES : FILTRES D'ACCENTUATION
IMAGES : FILTRES D'ACCENTUATION
Information sur la source
Description
Il s'agit de filtres dit de convolutions (on applique une matrice à chaque pixel de l'image). Parmi ces filtres, il existe une population de filtres linéaires qui permettent de réhausser les contours de zones dans l'image. Voici un exemple de matrice appliqué au pixel -1 | -2 | -1 -2 | 16 | -2 -1 | -2 | -1 Grâce à cet technique, les images pas très nets en votre possession peuvent être corrigées.
Source
- //matrice pour les filtres de convolutions
- private struct matrice
- {
- internal int [] tableau;
- internal int facteur;
- internal int offset;
- }
-
- public static void Accentuation(Bitmap bitmap)
- {
- matrice m;
- m.tableau = new int[] {-1,-2,-1,-2,16,-2,-1,-2,-1};
- m.facteur = 4;
- m.offset = 0;
-
- CalculMatrice(bitmap,m,1);
- }
-
- public static void AccentuationPlusForte(Bitmap bitmap)
- {
- matrice m;
- m.tableau = new int[] {-1,-1,-1,-1,-1,-1,-2,-4,-2,-1,-2,-4,48,-4,-2,-1,-2,-4,-2,-1,-1,-1,-1,-1,-1};
- m.facteur = 6;
- m.offset = 0;
-
- CalculMatrice(bitmap,m,2);
- }
-
-
- private static void CalculMatrice(Bitmap bitmap, matrice m,int n)
- {
- //on crée un bord de n pixel sur l'image avec effet mirroir
- Bitmap imagecloned = CreateBitmapWithMirrorForN(bitmap,n);
-
- //on détermine la taille de la matrice
- int tailletab = (int)Math.Pow((2*n)+1,2);
-
- //les valeurs de la matrice
- int [] tableau = new int[m.tableau.Length];
- tableau = m.tableau;
- int facteur = m.facteur;//division
- int moffset = m.offset;//ajout
-
- int widthcloned = imagecloned.Width;
- int heightcloned = imagecloned.Height;
- int width = bitmap.Width;
- int height = bitmap.Height;
-
- unsafe
- {
- BitmapData bmpData=bitmap.LockBits(new Rectangle(0,0,width,height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
- BitmapData bmpDatacloned=imagecloned.LockBits(new Rectangle(0,0,widthcloned,heightcloned),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
-
- byte * newPixel = (byte*)(void *)bmpData.Scan0;
- byte * mPixel = (byte *)(void *)bmpDatacloned.Scan0;
-
- int red = 0;
- int blue = 0;
- int green = 0;
- int indice = 0;//indice pour le tableau
-
- //largeur de numérisation
- int stridecloned = bmpDatacloned.Stride;
- int stride = bmpData.Stride;
-
- //offsets
- int offsetcloned = widthcloned % 4;
- int offset = width % 4;
-
- mPixel+=(stridecloned*n)+(3*n);//on positionne en n par n
-
- for (int y = n; y < (heightcloned -n); y++)
- {
- for (int x = n; x < (widthcloned - n); x++)
- {
- red = 0;
- green = 0;
- blue = 0;
-
- //parcours de chaque éléments de la matrice
- for (int k=-n; k<n+1; k++)
- for (int l=-n; l<n+1; l++)
- {
- indice = (l+n)*(2*n+1)+(k+n);//où on se trouve dans la matrice
- int loc = (k*3)+(l*stridecloned);//localisation du pixel
- //on applique la matrice
- red = (red + ( mPixel[loc] * tableau[indice]));
- green = (green + ( mPixel[loc+1] * tableau[indice]));
- blue = (blue + ( mPixel[loc+2] * tableau[indice]));
- }
- red = ((red / facteur)+moffset);
- blue = ((blue / facteur)+moffset);
- green = ((green / facteur)+moffset);
-
- //bornage
- red = (red < 0) ? 0 : (red > 255) ? 255 : red;
- blue = (blue < 0) ? 0 : (blue > 255) ? 255 : blue;
- green = (green < 0) ? 0 : (green > 255) ? 255 : green;
-
- newPixel[0] = (byte) red ;
- newPixel[1] = (byte)green;
- newPixel[2] = (byte)blue;
-
- newPixel+=3;
- mPixel+=3;
- }
- newPixel+=offset;
- mPixel+=offsetcloned+(2*n*3);
- }
- bitmap.UnlockBits(bmpData);
- imagecloned.UnlockBits(bmpDatacloned);
- }
- imagecloned.Dispose();
- }
-
-
- /// <summary>
- /// Permet de renvoyer une image avec un bord n
- /// dont ce dernier est le miroir du bord de l'image initiale.
- /// Cette image sert dans le cas d'application de matrice 2*n +1
- /// </summary>
- /// <param name="n">correspond à la bordure supplémentaire en pixel.</param>
- /// <returns></returns>
- public static Bitmap CreateBitmapWithMirrorForN(Bitmap bitmap, int n)
- {
- Bitmap bitmapWithMirror = new Bitmap(bitmap.Width+(n*2),bitmap.Height+(n*2));
-
- //copie de l'image au centre de la nouvelle
- Graphics g = Graphics.FromImage(bitmapWithMirror);
- g.DrawImage(bitmap,n,n,bitmap.Width,bitmap.Height);
- g.Dispose();
-
- //on va copier les lignes manquantes (recopie des lignes Nord, Sud, Ouest, Est de l'image)
- int width = bitmap.Width;
- int height = bitmap.Height;
- int heightcloned = bitmapWithMirror.Height;
- int widthcloned = bitmapWithMirror.Width;
-
- unsafe
- {
- BitmapData bmpDatacloned=bitmapWithMirror.LockBits(new Rectangle(0,0,widthcloned,heightcloned),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
-
- byte * newPixel = (byte *)(void *)bmpDatacloned.Scan0;
-
- //largeur de numérisation
- int stridecloned = bmpDatacloned.Stride;
- int newloc = 0;//localisation des nouveaux pixels ajoutés
- int loc = 0;//localisation des pixels servant à créer le mirroir
-
-
- for (int i = 1; i<=n;++i)
- for(int x=0;x<width;++x)
- {
- //rangee Nord
- newloc = ((x+n)*3)+((i-1)*stridecloned);
- loc = ((x+n)*3)+((2*n-i+1)*stridecloned);
- newPixel[newloc]= newPixel[loc];
- newPixel[newloc+1]= newPixel[loc+1];
- newPixel[newloc+2]= newPixel[loc+2];
-
- //rangee Sud
- newloc = ((x+n)*3)+((heightcloned-i)*stridecloned);
- loc = ((x+n)*3)+((heightcloned -(2*n-i+1))*stridecloned);
- newPixel[newloc]= newPixel[loc];
- newPixel[newloc+1]= newPixel[loc+1];
- newPixel[newloc+2]= newPixel[loc+2];
- }
- for (int i=1; i<=n ; ++i)
- {
-
- for(int y=0;y<heightcloned;++y)
- {
- //rangee Ouest
- newloc = ((n-i)*3)+(y*stridecloned);
- loc = ((n+i-1)*3)+ (y*stridecloned);
- newPixel[newloc]= newPixel[loc];
- newPixel[newloc+1]= newPixel[loc+1];
- newPixel[newloc+2]= newPixel[loc+2];
-
- //rangee Est
- newloc = ((widthcloned-(n-i+1))*3)+(y*stridecloned);
- loc = ((widthcloned-(n+i))*3)+ (y*stridecloned);
- newPixel[newloc]= newPixel[loc];
- newPixel[newloc+1]= newPixel[loc+1];
- newPixel[newloc+2]= newPixel[loc+2];
- }
- }
- bitmapWithMirror.UnlockBits(bmpDatacloned);
- }
- //l'image renvoyee peut être exploitee via des matrices (2*n)+1 X (2*n)+1
- //pas de réduction donc de la taille initiale pour l'image traitée.
- //Une copie en miroir évite les effets de bords
- return bitmapWithMirror;
- }
//matrice pour les filtres de convolutions
private struct matrice
{
internal int [] tableau;
internal int facteur;
internal int offset;
}
public static void Accentuation(Bitmap bitmap)
{
matrice m;
m.tableau = new int[] {-1,-2,-1,-2,16,-2,-1,-2,-1};
m.facteur = 4;
m.offset = 0;
CalculMatrice(bitmap,m,1);
}
public static void AccentuationPlusForte(Bitmap bitmap)
{
matrice m;
m.tableau = new int[] {-1,-1,-1,-1,-1,-1,-2,-4,-2,-1,-2,-4,48,-4,-2,-1,-2,-4,-2,-1,-1,-1,-1,-1,-1};
m.facteur = 6;
m.offset = 0;
CalculMatrice(bitmap,m,2);
}
private static void CalculMatrice(Bitmap bitmap, matrice m,int n)
{
//on crée un bord de n pixel sur l'image avec effet mirroir
Bitmap imagecloned = CreateBitmapWithMirrorForN(bitmap,n);
//on détermine la taille de la matrice
int tailletab = (int)Math.Pow((2*n)+1,2);
//les valeurs de la matrice
int [] tableau = new int[m.tableau.Length];
tableau = m.tableau;
int facteur = m.facteur;//division
int moffset = m.offset;//ajout
int widthcloned = imagecloned.Width;
int heightcloned = imagecloned.Height;
int width = bitmap.Width;
int height = bitmap.Height;
unsafe
{
BitmapData bmpData=bitmap.LockBits(new Rectangle(0,0,width,height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
BitmapData bmpDatacloned=imagecloned.LockBits(new Rectangle(0,0,widthcloned,heightcloned),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
byte * newPixel = (byte*)(void *)bmpData.Scan0;
byte * mPixel = (byte *)(void *)bmpDatacloned.Scan0;
int red = 0;
int blue = 0;
int green = 0;
int indice = 0;//indice pour le tableau
//largeur de numérisation
int stridecloned = bmpDatacloned.Stride;
int stride = bmpData.Stride;
//offsets
int offsetcloned = widthcloned % 4;
int offset = width % 4;
mPixel+=(stridecloned*n)+(3*n);//on positionne en n par n
for (int y = n; y < (heightcloned -n); y++)
{
for (int x = n; x < (widthcloned - n); x++)
{
red = 0;
green = 0;
blue = 0;
//parcours de chaque éléments de la matrice
for (int k=-n; k<n+1; k++)
for (int l=-n; l<n+1; l++)
{
indice = (l+n)*(2*n+1)+(k+n);//où on se trouve dans la matrice
int loc = (k*3)+(l*stridecloned);//localisation du pixel
//on applique la matrice
red = (red + ( mPixel[loc] * tableau[indice]));
green = (green + ( mPixel[loc+1] * tableau[indice]));
blue = (blue + ( mPixel[loc+2] * tableau[indice]));
}
red = ((red / facteur)+moffset);
blue = ((blue / facteur)+moffset);
green = ((green / facteur)+moffset);
//bornage
red = (red < 0) ? 0 : (red > 255) ? 255 : red;
blue = (blue < 0) ? 0 : (blue > 255) ? 255 : blue;
green = (green < 0) ? 0 : (green > 255) ? 255 : green;
newPixel[0] = (byte) red ;
newPixel[1] = (byte)green;
newPixel[2] = (byte)blue;
newPixel+=3;
mPixel+=3;
}
newPixel+=offset;
mPixel+=offsetcloned+(2*n*3);
}
bitmap.UnlockBits(bmpData);
imagecloned.UnlockBits(bmpDatacloned);
}
imagecloned.Dispose();
}
/// <summary>
/// Permet de renvoyer une image avec un bord n
/// dont ce dernier est le miroir du bord de l'image initiale.
/// Cette image sert dans le cas d'application de matrice 2*n +1
/// </summary>
/// <param name="n">correspond à la bordure supplémentaire en pixel.</param>
/// <returns></returns>
public static Bitmap CreateBitmapWithMirrorForN(Bitmap bitmap, int n)
{
Bitmap bitmapWithMirror = new Bitmap(bitmap.Width+(n*2),bitmap.Height+(n*2));
//copie de l'image au centre de la nouvelle
Graphics g = Graphics.FromImage(bitmapWithMirror);
g.DrawImage(bitmap,n,n,bitmap.Width,bitmap.Height);
g.Dispose();
//on va copier les lignes manquantes (recopie des lignes Nord, Sud, Ouest, Est de l'image)
int width = bitmap.Width;
int height = bitmap.Height;
int heightcloned = bitmapWithMirror.Height;
int widthcloned = bitmapWithMirror.Width;
unsafe
{
BitmapData bmpDatacloned=bitmapWithMirror.LockBits(new Rectangle(0,0,widthcloned,heightcloned),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
byte * newPixel = (byte *)(void *)bmpDatacloned.Scan0;
//largeur de numérisation
int stridecloned = bmpDatacloned.Stride;
int newloc = 0;//localisation des nouveaux pixels ajoutés
int loc = 0;//localisation des pixels servant à créer le mirroir
for (int i = 1; i<=n;++i)
for(int x=0;x<width;++x)
{
//rangee Nord
newloc = ((x+n)*3)+((i-1)*stridecloned);
loc = ((x+n)*3)+((2*n-i+1)*stridecloned);
newPixel[newloc]= newPixel[loc];
newPixel[newloc+1]= newPixel[loc+1];
newPixel[newloc+2]= newPixel[loc+2];
//rangee Sud
newloc = ((x+n)*3)+((heightcloned-i)*stridecloned);
loc = ((x+n)*3)+((heightcloned -(2*n-i+1))*stridecloned);
newPixel[newloc]= newPixel[loc];
newPixel[newloc+1]= newPixel[loc+1];
newPixel[newloc+2]= newPixel[loc+2];
}
for (int i=1; i<=n ; ++i)
{
for(int y=0;y<heightcloned;++y)
{
//rangee Ouest
newloc = ((n-i)*3)+(y*stridecloned);
loc = ((n+i-1)*3)+ (y*stridecloned);
newPixel[newloc]= newPixel[loc];
newPixel[newloc+1]= newPixel[loc+1];
newPixel[newloc+2]= newPixel[loc+2];
//rangee Est
newloc = ((widthcloned-(n-i+1))*3)+(y*stridecloned);
loc = ((widthcloned-(n+i))*3)+ (y*stridecloned);
newPixel[newloc]= newPixel[loc];
newPixel[newloc+1]= newPixel[loc+1];
newPixel[newloc+2]= newPixel[loc+2];
}
}
bitmapWithMirror.UnlockBits(bmpDatacloned);
}
//l'image renvoyee peut être exploitee via des matrices (2*n)+1 X (2*n)+1
//pas de réduction donc de la taille initiale pour l'image traitée.
//Une copie en miroir évite les effets de bords
return bitmapWithMirror;
}
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Filtre imagelist [ par Monico9385 ]
Bonjour tout le monde, j'ai un soucis sur le filtrage d'image. En fait, j'ai un ImageList associé à un ListView, et j'aimerai lister que l
traitement d'image en c [ par noussadk84 ]
Svp jai un projet en c qui permet de faire des opérations de base sur les images de changer la luminosité ,le contraste appliquer le filtre uniforme e
PictureBox problème d'affichage avec c# [ par idrissess ]
Bonjour; j'ai essayé d'afficher une image dans un pictureBox avec c# mais rien ne se passe! juste j'aurai une zone blanche au lieu d'avoir cette image
[VIEW] > avoir la main d'afficher ou non une image html helper [ par sisimo ]
bonjour, dans ma vue il y a un emplacement pour des image , ces image je l'ai recupere apartie d'une base de donne avec Linq, et je les afficher avec
Essayer d'insérer des images en selectionnant l'url dans une database [ par ahorel ]
Bonjour, J'ai crée une database contenant la table suivante : display_image id_image enu name l
adaptation des image sur c# [ par kaoutarac ]
bonjour j ai un problem concernant une image dans une form. si j agrandi la fenetre la taille de l image ne s'adapte pas avec cette derniere. si vous
comment enlever le fond blanc d'une image avec visual c# [ par ami7 ]
Salut à tous, voilà j'ai ajouter à mon application un bouton , j'ai changé la propriété Image de ce dernier mais le bouton parait une image a un fon
Comment attribué un entier(variable) dans le nom d'un Button? [ par darkdog85 ]
Bonjour, Je ne sais pas si mon titre est très clair alors je vais directement passé a mon exemple : [code=cs] int i; for (i=1;i < 11;i++) { button
Afficher image BMP d'uni fichier XML [ par thib89 ]
Bonsoir, Excuser moi de vous déranger, si quelqu'un pouvais m'aider se serait vraiment bienvenu. Je programme sous VS 2010 en C#. J'ai un fichier XM
comment dessiner des objets graphiques sur une image [ par ami7 ]
bonsoir, j'utilise un code c# qui permet de dessiner des objets ghraphiques (rectangle , ellipse, ligne...) le question c'est que je veut tout d'abord
|
Derniers Blogs
TECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PCTECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PC par ROMELARD Fabrice
Speakers: Thierry Rapatout, Antoine Petit et Xavier Trebbia Cette session entre dans le cadre des RDV Décideurs des TechDays 2012, elle est liée à la consumérisation de l'IT et la mise en place du "DeskTop as a Service" dans de plus en ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLETECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLE par ROMELARD Fabrice
Speakers: Julien Marechal, Gautier Confiant, Sébastien MEYER La session débute par le positionnement de la solution System Center par rapport aux concepts d'organisation ITIL. Le portail du catalogue de se...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : PLEINIèRE SECOND JOURTECHDAYS PARIS 2012 : PLEINIèRE SECOND JOUR par ROMELARD Fabrice
Après une première journée dédiée aux développeurs, cette seconde journée est dédiée au monde des entreprises et de ses applications. Ainsi, cette pleinière est dédiée à faire un 360 de l'évolution des applications Business aux demandes ac...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : RETOUR D'EXPéRIENCE SUR LA MISE EN PLACE D'UN CLOUD PRIVéTECHDAYS PARIS 2012 : RETOUR D'EXPéRIENCE SUR LA MISE EN PLACE D'UN CLOUD PRIVé par ROMELARD Fabrice
Speaker : Guillaume Rochette Cette session est dédiée à fournir le retour sur la mise en place d'un cloud privé (IaaS) par Osiatis pour son compte ou celui de ses clients. Ce projet s'est déroulé sur 4 mois et a permis de faire évoluer...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : COMMENT SHAREPOINT A SAUVé MES TECHDAYSTECHDAYS PARIS 2012 : COMMENT SHAREPOINT A SAUVé MES TECHDAYS par ROMELARD Fabrice
Speakers : Lionel Limozin et Alain Marty La session commence par une découverte de SharePoint à travers la mise en place d'un environnement SharePoint pour la gestion des Sessions animées par BeWise. Le besoin est très ba...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|