begin process at 2012 02 11 05:57:43
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Graphique

 > IMAGES : LE FILTRE MÉDIAN

IMAGES : LE FILTRE MÉDIAN


 Information sur la source

Note :
Aucune note
Catégorie :Graphique Source .NET ( DotNet ) Classé sous :image, filtre, median Niveau :Initié Date de création :12/04/2005 Date de mise à jour :13/04/2005 13:33:07 Vu / téléchargé :15 592 / 814

Auteur : tkfe

Ecrire un message privé
Commentaire sur cette source (0)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
Le filtre Médian est utilisé en traitement d'images pour palier aux anomalies lors d'une capture d'image.
Si vous avez un vieux scanner, sur vos images peuvent apparaître des pixels n'appartenant pas à celles-ci.

Source

  • using System;
  • using System.Drawing;
  • using System.Drawing.Imaging;
  • namespace Traitimage.Bitmaps.Traitements
  • {
  • public class FormatImage24
  • {
  • public static void Median(Bitmap bitmap, 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);
  • int tailletabdemi = tailletab/2;
  • //tableaux contenant les pixels contenus dans la matrice
  • byte [] tabred = new byte[tailletab];
  • byte [] tabgreen = new byte[tailletab];
  • byte [] tabblue = new byte[tailletab];
  • //Taille de l'image avec le bord en mirroir
  • int widthcloned = imagecloned.Width;
  • int heightcloned = imagecloned.Height;
  • //Taille de l'image à traiter
  • 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 indice = 0;//indice pour tableaux
  • int loc = 0;//localisation du pixel
  • //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++)
  • {
  • //récupération des pixels 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);
  • loc = (k*3)+(l*stridecloned);
  • tabred[indice]= mPixel[loc];
  • tabgreen[indice]= mPixel[loc+1];
  • tabblue[indice]= mPixel[loc+2];
  • }
  • //tri (on prend le median)
  • newPixel[0] = quick_select(tabred,tailletab);
  • newPixel[1] = quick_select(tabgreen,tailletab);
  • newPixel[2] = quick_select(tabblue,tailletab);
  • newPixel+=3;
  • mPixel+=3;
  • }
  • newPixel+=offset;
  • mPixel+=offsetcloned+(2*n*3);
  • }
  • bitmap.UnlockBits(bmpData);
  • imagecloned.UnlockBits(bmpDatacloned);
  • }
  • imagecloned.Dispose();
  • }
  • private static byte quick_select(byte[] tab, int n)
  • {
  • int min, max ; //pointeur sur le min et le max en cours
  • int median;
  • int m, l, h; //milieu, petit, haut
  • min = 0 ;
  • max = n-1 ;
  • median = (min + max) / 2;
  • for (;;)
  • {
  • if (max <= min) // 1 seul élément, plus besoin de trier, on renvoie le median
  • return tab[median] ;
  • if (max == min + 1) // plus que 2 éléments (on trie et on renvoie le median)
  • {
  • if (tab[min] > tab[max])
  • inversion(ref tab[min], ref tab[max]) ; //on inverse
  • return tab[median] ;
  • }
  • //on trie les élements min, milieu et max
  • m = (min + max) / 2;
  • if (tab[m] > tab[max]) inversion(ref tab[m], ref tab[max]) ;
  • if (tab[min] > tab[max]) inversion(ref tab[min], ref tab[max]) ;
  • if (tab[m] > tab[min]) inversion(ref tab[m], ref tab[min]) ;
  • //on inverse le nouveau milieu avec le min+1
  • inversion(ref tab[m], ref tab[min+1]) ;
  • //on va inverser les elements dans la tranche en cours
  • l = min + 1;
  • h = max;
  • for (;;)
  • {
  • do l++; while (tab[min] > tab[l]) ;
  • do h--; while (tab[h] > tab[min]) ;
  • if (h < l)
  • break;
  • inversion(ref tab[l], ref tab[h]) ;
  • }
  • //on mets le milieu en position min à sa bonne place
  • inversion(ref tab[min], ref tab[h]) ;
  • //on réajuste les pointeurs min et max
  • if (h <= median)
  • min = l;
  • if (h >= median)
  • max = h - 1;
  • }
  • }
  • private static void inversion(ref byte a,ref byte b)
  • {
  • byte t=a;
  • a=b;
  • b=t;
  • }
  • /// <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;
  • }
  • }
  • }
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace Traitimage.Bitmaps.Traitements
{
	
	public class FormatImage24 
	{
		
		public static void Median(Bitmap bitmap, 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);
			
			int tailletabdemi = tailletab/2;
			//tableaux contenant les pixels contenus dans la matrice
			byte [] tabred = new byte[tailletab];
			byte [] tabgreen = new byte[tailletab];
			byte [] tabblue = new byte[tailletab];

			//Taille de l'image avec le bord en mirroir
			int widthcloned = imagecloned.Width;
			int heightcloned = imagecloned.Height;

			//Taille de l'image à traiter
			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 indice = 0;//indice pour tableaux
				int loc = 0;//localisation du pixel

				//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++)
					{
						//récupération des pixels 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);
								loc = (k*3)+(l*stridecloned);
								tabred[indice]= mPixel[loc];
								tabgreen[indice]= mPixel[loc+1];
								tabblue[indice]= mPixel[loc+2];
							}
						//tri (on prend le median)
						newPixel[0] = quick_select(tabred,tailletab);
						newPixel[1] = quick_select(tabgreen,tailletab);
						newPixel[2] = quick_select(tabblue,tailletab);
						newPixel+=3;
						mPixel+=3;
					}				
					newPixel+=offset;
					mPixel+=offsetcloned+(2*n*3);
				}
				bitmap.UnlockBits(bmpData);
				imagecloned.UnlockBits(bmpDatacloned);
			}
			imagecloned.Dispose();
		}

		private static byte quick_select(byte[] tab, int n)
		{
			int min, max ; //pointeur sur le min et le max en cours
			int median;
			int m, l, h; //milieu, petit, haut
			min = 0 ;
			max = n-1 ;
			median = (min + max) / 2;
			for (;;) 
			{
				if (max <= min) // 1 seul élément, plus besoin de trier, on renvoie le median
					return tab[median] ;
				if (max == min + 1) // plus que 2 éléments (on trie et on renvoie le median)
				{ 
					if (tab[min] > tab[max])
						inversion(ref tab[min], ref tab[max]) ; //on inverse
					return tab[median] ;
				}
				//on trie les élements min, milieu et max
				m = (min + max) / 2;
				if (tab[m] > tab[max]) inversion(ref tab[m], ref tab[max]) ;
				if (tab[min] > tab[max]) inversion(ref tab[min], ref tab[max]) ;
				if (tab[m] > tab[min]) inversion(ref tab[m], ref tab[min]) ;
				//on inverse le nouveau milieu avec le min+1
				inversion(ref tab[m], ref tab[min+1]) ;
				//on va inverser les elements dans la tranche en cours
				l = min + 1;
				h = max;
				for (;;) 
				{
					do l++; while (tab[min] > tab[l]) ;
					do h--; while (tab[h] > tab[min]) ;
					if (h < l)
						break;
					inversion(ref tab[l], ref tab[h]) ;
				}
				//on mets le milieu en position min à sa bonne place
				inversion(ref tab[min], ref tab[h]) ;
				//on réajuste les pointeurs min et max
				if (h <= median)
					min = l;
				if (h >= median)
					max = h - 1;
			}
		}

		private static void inversion(ref byte a,ref byte  b) 
		{
			byte t=a;
			a=b;
			b=t;
		}

		/// <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;
		}
	}
}


 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Historique

13 avril 2005 13:33:07 :
Requalification en source .NET ;-)

 Sources du même auteur

Source avec Zip Source avec une capture Source .NET (Dotnet) IMAGES : FILTRES PAR CONVOLUTION
Source avec Zip Source avec une capture Source .NET (Dotnet) IMAGES : EFFET PEINTURE
Source avec Zip Source avec une capture Source .NET (Dotnet) IMAGES : FILTRES D'ACCENTUATION
Source avec une capture Source .NET (Dotnet) IMAGES : DITHERING PAR MOTIF

 Sources de la même categorie

Source avec Zip Source avec une capture Source .NET (Dotnet) WINDOWSGADGET LIKE par Frelon
Source avec Zip Source avec une capture Source .NET (Dotnet) USERCONTROL PLANNING / SEMAINE / JOURNÉE par yohan49
Source avec Zip Source avec une capture Source .NET (Dotnet) TEMPLATE MATCHING ET RECONNAISSANCE D'OBJETS AVEC OPENCV (EM... par boutemine
Source .NET (Dotnet) CALCULE D'UNE EXPRESSION MATHEMATIQUE PAR COMPILATION par yohan49
Source avec Zip Source avec une capture Source .NET (Dotnet) CALENDRIER TACTILE, SOUS FORME DE ROUES par Robert33

 Sources en rapport avec celle ci

Source avec Zip Source .NET (Dotnet) BALLON, CAREE ET IMAGE QUI TOURNENT, SE GONFLENT ET SE DGONF... par zertyx
Source avec Zip Source .NET (Dotnet) PUZZLE 4X4 par jrscofield
Source avec Zip Source avec une capture Source .NET (Dotnet) IMAGES : FILTRES D'ACCENTUATION par tkfe
Source avec Zip Source .NET (Dotnet) FILTRES ET MANIPULATION D'IMAGES EN UTILISANT LOCKBITS ET DE... par li9
Source avec Zip Source avec une capture Source .NET (Dotnet) X-PRO, ACDSEE LIKE par Arkko

Commentaires et avis

Aucun commentaire pour le moment.

 Ajouter un commentaire


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&#233; &#224; 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


Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 2,293 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales