lio-12 | Bonsoir,
je viens vous poser une petite question.
Je veux faire un traitement d'image. Je passe mon image depuis une picture1, dans une matrice, puis, après modification (passage en niveau de gris) , je veux vérifier le travail en réimportant la matrice dans une image.
Mais j'ai pas mal de soucis. Cela me renvoie l'erreur '9' dépassement de la plage, lors de l'attribution
Code :
- Matrice2(i, j).Blue = Gris(Matrice(i, j))
|
Voyez une solution à ce problème? Je sent bien qu'il y a une problème au niveau de ma matrice 2... faut t'il faire un redim? pour la mettre à la même taille que la matrice 1.
Ensuite, est ce que mettre la fonction "Gris" comme byte est acceptable?
Je bosse sous VB6
Merci d'avance
Voici mon code
Code :
- Option Explicit
- Option Base 1
- Private Type BITMAP
- bmType As Long
- bmWidth As Long
- bmHeight As Long
- bmWidthBytes As Long
- bmPlanes As Integer
- bmBitsPixel As Integer
- bmBits As Long
- End Type
- Private Type Pixel
- Red As Byte
- Green As Byte
- Blue As Byte
- End Type
- Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
- Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
- Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
- Dim Matrice() As Pixel
- Dim Matrice2() As Pixel
- Dim Rvb As Long
- Dim NHeight, MWidth As Integer
- 'renvoi le niveau de gris du Rvb
- Private Function Gris(bloc As Pixel) As Long
- Dim Moyenne As Long
- Moyenne = (bloc.Red + (bloc.Green / 256) + (bloc.Blue / 65536)) / 3
- Gris = RGB(Moyenne, Moyenne, Moyenne)
- End Function
- '***************************************************************************
- 'Procedure qui copie une image PictureBox vers une matrice de pixels
- Private Sub MatrixFromImage(Picture As PictureBox, Matrice() As Pixel)
-
- Dim PicBits() As Byte, PicInfo As BITMAP
- Dim Size As Long
- Dim i, j As Integer
- Dim Z As Long
-
- GetObject Picture.Image, Len(PicInfo), PicInfo
-
- Size = PicInfo.bmWidth * PicInfo.bmBitsPixel * PicInfo.bmHeight / 8
-
- ReDim PicBits(Size) As Byte
- ReDim Matrice(PicInfo.bmHeight, PicInfo.bmWidth) As Pixel
-
-
- GetBitmapBits Picture.Image, Size, PicBits(1)
-
- For i = 1 To PicInfo.bmHeight
- For j = 1 To PicInfo.bmWidth
- Z = (i - 1) * PicInfo.bmWidth * 4 + (j - 1) * 4 + 1
- Matrice(i, j).Blue = PicBits(Z)
- Matrice(i, j).Green = PicBits(Z + 1)
- Matrice(i, j).Red = PicBits(Z + 2)
- Next j
- Next i
-
- NHeight = PicInfo.bmHeight
- MWidth = PicInfo.bmWidth
-
- End Sub
- Private Sub ImageFromMatrix(Picture As PictureBox, Matrice() As Pixel)
- Dim PicBits() As Byte
- Dim i, j As Integer
- Dim Z As Long
-
- ReDim PicBits(UBound(Matrice(), 1) * UBound(Matrice(), 2) * 4)
-
-
-
- For i = 1 To UBound(Matrice(), 1)
- For j = 1 To UBound(Matrice(), 2)
- Z = Z + 1
-
- PicBits(Z) = Matrice(i, j).Blue
- PicBits(Z + 1) = Matrice(i, j).Green
- PicBits(Z + 2) = Matrice(i, j).Red
- PicBits(Z + 3) = 0
-
- Z = Z + 3
- Next j
- Next i
-
- SetBitmapBits Picture.Image, UBound(PicBits), PicBits(1)
- Picture.Refresh
- End Sub
- Private Sub Command1_Click()
- Dim chemin As String
- chemin = App.Path
- If Right(chemin, 1) <> "\" Then
- chemin = chemin & "\" & "Images-test\"
- End If
- chemin = chemin & "test-niveau-de-gris.jpg"
- Picture1.Picture = LoadPicture(chemin)
- End Sub
- Private Sub Command2_Click()
- Dim i, j As Integer
- Call MatrixFromImage(Picture1, Matrice())
- Picture2.Width = Picture1.Width
- Picture2.Height = Picture1.Height
-
- For i = 1 To NHeight
- For j = 1 To MWidth
- Matrice2(i, j).Blue = Gris(Matrice(i, j))
- Matrice2(i, j).Green = Gris(Matrice(i, j))
- Matrice2(i, j).Red = Gris(Matrice(i, j))
- Next j
- Next i
-
- Call ImageFromMatrix(Picture2, Matrice2())
-
-
-
- End Sub
|
|