Images : filtres d'accentuation

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 / Exemple :


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

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.