Oppure

Loading
30/03/10 13:59
Federico1976
Ciao A tutti Vengo a Porvi Il mio problema sperando che qualcuno mi possa aiutare.
Sto Facendo un Programma di grafica questo programma a una funzione di conta delle Aree dell'immagine specificando colore e coefficente Trovo le aree, la funzione che uso funzione egregiamente ma in un tempo un po lungo volevo migliorarla vi mostro comè la funzione

Codice Ciclo principale dell'imagine:
   Sub Parser()
                If Immagine Is Nothing Then
                    Exit Sub
                End If
                Dim Larghezza As Integer = Immagine.Width - 1 'Larghezza
                Dim Altezza As Integer = Immagine.Height - 1 'Altezza
                Dim x As Integer
                Dim y As Integer
                Dim ColoreAttuale As Color

                Dim img As Bitmap = DirectCast(Immagine.Clone, Bitmap)

                ' GDI+ still lies to us - the return format is BGR, NOT RGB.
                Dim bmData As BitmapData = img.LockBits(New Rectangle(0, 0, Immagine.Width, Immagine.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)

                Dim stride As Integer = bmData.Stride
                Dim Scan0 As System.IntPtr = bmData.Scan0
                Dim bytes As Integer = img.Width * img.Height * 3
                Dim p(bytes - 1) As Byte
                ' Copy the RGB values into the array.
                System.Runtime.InteropServices.Marshal.Copy(Scan0, p, 0, bytes)



                Dim c As Integer = 0
                Dim nOffset As Integer = stride - bmData.Width * 3
                valueMax = Altezza

                For y = 0 To Altezza - 1
                    value = y
                    For x = 0 To Larghezza

                        If StopRicerca = True Then
                            value = -1
                            Exit Sub
                        End If
                        If PuntiInseriti.IndexOf(c) = -1 Then
                      
                            ColoreAttuale = Color.FromArgb(p(c + 2), p(c + 1), p(c))
                            'Funzione Di Confronto Colore con coefficente
                            If IsNearestColor(ColoreDaValutare, ColoreAttuale, CoefficenteSfondo) = OgettoIpostato Then
                                Oggetto = New OggettiImmagine
                                Oggetto.coefficente = CoefficenteOggetto
                                Oggetto.colore = ColoreAttuale
                                Dim poi As New Point(x, y)
                                Oggetto.point = Area(c, CoefficenteOggetto, poi)
                                If (Oggetto.point.Count / 3) > PuntiPerPallino Then
                                    Dim curGraphic As Graphics = Graphics.FromImage(immagineRed)

                                    curGraphic.FillEllipse(New SolidBrush(Color.Yellow), New Rectangle(poi.X, poi.Y, 6, 6))
                                    _Oggetti.Add(Oggetto)
                                    If Control IsNot Nothing Then
                                        SetImage(immagineRed.Clone, Control)
                                    End If
                                End If
                            End If
                        Else
                            Dim conta As Integer = PuntiInseriti.Count
                            PuntiInseriti.Remove(c)
                            PuntiInseriti.Remove(c + 1)
                            PuntiInseriti.Remove(c + 2)
                        End If
                        c += 3
                    Next
                    c += nOffset
                Next
                ogget = _Oggetti
                'System.Runtime.InteropServices.Marshal.Copy(p, 0, Scan0, bytes)
                img.UnlockBits(bmData)

                If Control IsNot Nothing Then
                    SetImage(immagineRed.Clone, Control)
                    Ricerca.Canvas.OggettoBitmap(immagineRed.Clone)
                End If
                value = -1
            End Sub


Codice ciclo secondario Che mi Trova le aree partendo da un point:

 Public Function Area(ByVal c As Integer, ByVal Coefficente As Integer, ByVal point As Point) As List(Of Integer)
                Dim Points As New List(Of Integer)
                Dim NewPoints As New List(Of Integer)
                Dim ColoreAttuale, SostCol As Color
                Dim Col_Oggetto As Color
                Dim ArrayPoint As New List(Of Integer)
                Dim Punti As New List(Of Point)
                Dim NewPunti As New List(Of Point)

                SostCol = Color.Violet
                '
                Points.Add(c)
                Dim bSrc As Bitmap = DirectCast(Immagine.Clone(), Bitmap)
                Dim imgModificata As Bitmap = DirectCast(immagineRed.Clone, Bitmap)

                ' GDI+ still lies to us - the return format is BGR, NOT RGB.
                Dim bmData As BitmapData = imgModificata.LockBits(New Rectangle(0, 0, immagineRed.Width, immagineRed.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
                Dim bmSrc As BitmapData = bSrc.LockBits(New Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)

                Dim SrcScan0 As System.IntPtr = bmSrc.Scan0
                Dim stride As Integer = bmData.Stride
                Dim Scan0 As System.IntPtr = bmData.Scan0
                Dim bytes As Integer = (immagineRed.Width * immagineRed.Height * 3) - 2
                Dim p2(bytes - 1) As Byte
                Dim bytes1 As Integer = (immagineRed.Width * immagineRed.Height * 3) - 2
                Dim pSrc(bytes1 - 1) As Byte

                ' Copy the RGB values into the array.
                System.Runtime.InteropServices.Marshal.Copy(Scan0, p2, 0, bytes)
                System.Runtime.InteropServices.Marshal.Copy(SrcScan0, pSrc, 0, bytes1)

                Dim nOffset As Integer = stride - bmData.Width * 3
                ' sicurezza che la variabile "Col_Oggetto" venga inpostata manualmente su un colore di 
                'un oggetto per evitare la ripetizione della ricerca dei punti inseriti 

                If Funzione = Funzioni.TrovaOggettiDiUnColore Then
                    If ColoreOggetto = Nothing Then
                        Col_Oggetto = Color.FromArgb(p2(c + 2), p2(c + 1), p2(c))
                    Else
                        Col_Oggetto = ColoreOggetto
                    End If
                Else
                    Col_Oggetto = Color.FromArgb(p2(c + 2), p2(c + 1), p2(c))
                End If

                Oggetto.p_t.Add(point)
                Punti.Add(point)
                Dim i As Int32
                Do
                    For Each K As Integer In Points
                        Dim sin As Integer, dest As Integer, sop As Integer, Sot As Integer

                        Dim J_ As Point, d_ As Point, s_ As Point, ot_ As Point, Score As Point
                        Score = Punti(i)

                        'testa il punto a destra di questo
                        If K > 3 Then
                            sin = K - 3
                            J_ = New Point(Score.X - 1, Score.Y)
                        End If

                        ColoreAttuale = Color.FromArgb(pSrc(sin + 2), pSrc(sin + 1), pSrc(sin))
                        If IsNearestColor(Col_Oggetto, ColoreAttuale, CoefficenteOggetto) = True Then 'Funzione Di Confronto Colore con coefficente

                            If ArrayPoint.IndexOf(sin) = -1 Then
                                p2(sin + 2) = Color.Red.R
                                p2(sin + 1) = Color.Red.G
                                p2(sin) = Color.Red.B

                                Oggetto.p_t.Add(J_)'oggetto che conterra le coordinate dei punti

                                NewPunti.Add(J_)
                                NewPoints.Add(sin)

                                PuntiInseriti.Add(sin)'Array Di integer  Totali 
                                ArrayPoint.Add(sin)

                                PuntiInseriti.Add(sin + 1)
                                ArrayPoint.Add(sin + 1)


                                PuntiInseriti.Add(sin + 2)
                                ArrayPoint.Add(sin + 2)
                            End If
                        Else
                            p2(sin + 2) = SostCol.R
                            p2(sin + 1) = SostCol.G
                            p2(sin) = SostCol.B

                        End If
                        If K < (bytes - 3) Then
                            dest = K + 3
                            d_ = New Point(Score.X + 1, Score.Y)
                        End If

                        ColoreAttuale = Color.FromArgb(pSrc(dest + 2), pSrc(dest + 1), pSrc(dest))
                        If IsNearestColor(Col_Oggetto, ColoreAttuale, CoefficenteOggetto) = True Then

                            If ArrayPoint.IndexOf(dest) = -1 Then
                                p2(dest + 2) = Color.Red.R
                                p2(dest + 1) = Color.Red.G
                                p2(dest) = Color.Red.B

                                Oggetto.p_t.Add(d_)

                                NewPunti.Add(d_)
                                NewPoints.Add(dest)

                                PuntiInseriti.Add(dest)
                                ArrayPoint.Add(dest)

                                PuntiInseriti.Add(dest + 1)
                                ArrayPoint.Add(dest + 1)


                                PuntiInseriti.Add(dest + 2)
                                ArrayPoint.Add(dest + 2)
                            End If
                        Else
                            p2(dest + 2) = SostCol.R
                            p2(dest + 1) = SostCol.G
                            p2(dest) = SostCol.B

                        End If
                        If K > (stride + nOffset) Then
                            sop = K - stride - nOffset
                            s_ = New Point(Score.X, Score.Y - 1)
                        End If

                        ColoreAttuale = Color.FromArgb(pSrc(sop + 2), pSrc(sop + 1), pSrc(sop))
                        If IsNearestColor(Col_Oggetto, ColoreAttuale, CoefficenteOggetto) = True Then

                            If ArrayPoint.IndexOf(sop) = -1 Then
                                p2(sop + 2) = Color.Red.R
                                p2(sop + 1) = Color.Red.G
                                p2(sop) = Color.Red.B

                                Oggetto.p_t.Add(s_)'Oggetto dell'area

                                NewPunti.Add(s_)
                                NewPoints.Add(sop)

                                PuntiInseriti.Add(sop)'Array Di intege Totali
                                ArrayPoint.Add(sop)

                                PuntiInseriti.Add(sop + 1)
                                ArrayPoint.Add(sop + 1)

                                PuntiInseriti.Add(sop + 2)
                                ArrayPoint.Add(sop + 2)
                            End If
                        Else
                            p2(sop + 2) = SostCol.R
                            p2(sop + 1) = SostCol.G
                            p2(sop) = SostCol.B
                        End If

                        If K < (bytes - stride - nOffset) Then
                            Sot = K + stride + nOffset
                            ot_ = New Point(Score.X, Score.Y + 1)
                        End If

                        If Sot < bytes Then
                            ColoreAttuale = Color.FromArgb(pSrc(Sot + 2), pSrc(Sot + 1), pSrc(Sot))
                            If IsNearestColor(Col_Oggetto, ColoreAttuale, CoefficenteOggetto) = True Then

                                If ArrayPoint.IndexOf(Sot) = -1 Then
                                    p2(Sot + 2) = Color.Red.R
                                    p2(Sot + 1) = Color.Red.G
                                    p2(Sot) = Color.Red.B

                                    Oggetto.p_t.Add(ot_)

                                    NewPunti.Add(ot_)
                                    NewPoints.Add(Sot)

                                    PuntiInseriti.Add(Sot)
                                    ArrayPoint.Add(Sot)

                                    PuntiInseriti.Add(Sot + 1)
                                    ArrayPoint.Add(Sot + 1)


                                    PuntiInseriti.Add(Sot + 2)
                                    ArrayPoint.Add(Sot + 2)
                                End If
                            Else
                                p2(Sot + 2) = SostCol.R
                                p2(Sot + 1) = SostCol.G
                                p2(Sot) = SostCol.B
                            End If
                        End If
                        i += 1
                    Next
                    Points.Clear()
                    Points.AddRange(NewPoints)
                    NewPoints.Clear()
                    Punti.Clear()
                    Punti.AddRange(NewPunti)
                    NewPunti.Clear()
                    i = 0

                Loop Until Points.Count = 0

                ' Copy the RGB values back to the bitmap
                System.Runtime.InteropServices.Marshal.Copy(pSrc, 0, SrcScan0, bytes1)
                System.Runtime.InteropServices.Marshal.Copy(p2, 0, Scan0, bytes)

                imgModificata.UnlockBits(bmData)
                bSrc.UnlockBits(bmSrc)

                If (ArrayPoint.Count / 3) > PuntiPerPallino Then
                    immagineRed = imgModificata
                End If

                Return ArrayPoint
            End Function


Ringrazio tutti dell'aiuto che date continumente
aaa