Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Conta delle Aree Di un Immagine VB.Net
Forum - C# / VB.NET - Conta delle Aree Di un Immagine VB.Net

Avatar
Federico1976 (Normal User)
Rookie


Messaggi: 42
Iscritto: 23/03/2009

Segnala al moderatore
Postato alle 15:59
Martedė, 30/03/2010
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:
Codice sorgente - presumibilmente VB.NET

  1. Sub Parser()
  2.                 If Immagine Is Nothing Then
  3.                     Exit Sub
  4.                 End If
  5.                 Dim Larghezza As Integer = Immagine.Width - 1 'Larghezza
  6.                 Dim Altezza As Integer = Immagine.Height - 1 'Altezza
  7.                 Dim x As Integer
  8.                 Dim y As Integer
  9.                 Dim ColoreAttuale As Color
  10.  
  11.                 Dim img As Bitmap = DirectCast(Immagine.Clone, Bitmap)
  12.  
  13.                 ' GDI+ still lies to us - the return format is BGR, NOT RGB.
  14.                 Dim bmData As BitmapData = img.LockBits(New Rectangle(0, 0, Immagine.Width, Immagine.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
  15.  
  16.                 Dim stride As Integer = bmData.Stride
  17.                 Dim Scan0 As System.IntPtr = bmData.Scan0
  18.                 Dim bytes As Integer = img.Width * img.Height * 3
  19.                 Dim p(bytes - 1) As Byte
  20.                 ' Copy the RGB values into the array.
  21.                 System.Runtime.InteropServices.Marshal.Copy(Scan0, p, 0, bytes)
  22.  
  23.  
  24.  
  25.                 Dim c As Integer = 0
  26.                 Dim nOffset As Integer = stride - bmData.Width * 3
  27.                 valueMax = Altezza
  28.  
  29.                 For y = 0 To Altezza - 1
  30.                     value = y
  31.                     For x = 0 To Larghezza
  32.  
  33.                         If StopRicerca = True Then
  34.                             value = -1
  35.                             Exit Sub
  36.                         End If
  37.                         If PuntiInseriti.IndexOf(c) = -1 Then
  38.                      
  39.                             ColoreAttuale = Color.FromArgb(p(c + 2), p(c + 1), p(c))
  40.                             'Funzione Di Confronto Colore con coefficente
  41.                             If IsNearestColor(ColoreDaValutare, ColoreAttuale, CoefficenteSfondo) = OgettoIpostato Then
  42.                                 Oggetto = New OggettiImmagine
  43.                                 Oggetto.coefficente = CoefficenteOggetto
  44.                                 Oggetto.colore = ColoreAttuale
  45.                                 Dim poi As New Point(x, y)
  46.                                 Oggetto.point = Area(c, CoefficenteOggetto, poi)
  47.                                 If (Oggetto.point.Count / 3) > PuntiPerPallino Then
  48.                                     Dim curGraphic As Graphics = Graphics.FromImage(immagineRed)
  49.  
  50.                                     curGraphic.FillEllipse(New SolidBrush(Color.Yellow), New Rectangle(poi.X, poi.Y, 6, 6))
  51.                                     _Oggetti.Add(Oggetto)
  52.                                     If Control IsNot Nothing Then
  53.                                         SetImage(immagineRed.Clone, Control)
  54.                                     End If
  55.                                 End If
  56.                             End If
  57.                         Else
  58.                             Dim conta As Integer = PuntiInseriti.Count
  59.                             PuntiInseriti.Remove(c)
  60.                             PuntiInseriti.Remove(c + 1)
  61.                             PuntiInseriti.Remove(c + 2)
  62.                         End If
  63.                         c += 3
  64.                     Next
  65.                     c += nOffset
  66.                 Next
  67.                 ogget = _Oggetti
  68.                 'System.Runtime.InteropServices.Marshal.Copy(p, 0, Scan0, bytes)
  69.                 img.UnlockBits(bmData)
  70.  
  71.                 If Control IsNot Nothing Then
  72.                     SetImage(immagineRed.Clone, Control)
  73.                     Ricerca.Canvas.OggettoBitmap(immagineRed.Clone)
  74.                 End If
  75.                 value = -1
  76.             End Sub



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

Codice sorgente - presumibilmente VB.NET

  1. Public Function Area(ByVal c As Integer, ByVal Coefficente As Integer, ByVal point As Point) As List(Of Integer)
  2.                 Dim Points As New List(Of Integer)
  3.                 Dim NewPoints As New List(Of Integer)
  4.                 Dim ColoreAttuale, SostCol As Color
  5.                 Dim Col_Oggetto As Color
  6.                 Dim ArrayPoint As New List(Of Integer)
  7.                 Dim Punti As New List(Of Point)
  8.                 Dim NewPunti As New List(Of Point)
  9.  
  10.                 SostCol = Color.Violet
  11.                 '
  12.                 Points.Add(c)
  13.                 Dim bSrc As Bitmap = DirectCast(Immagine.Clone(), Bitmap)
  14.                 Dim imgModificata As Bitmap = DirectCast(immagineRed.Clone, Bitmap)
  15.  
  16.                 ' GDI+ still lies to us - the return format is BGR, NOT RGB.
  17.                 Dim bmData As BitmapData = imgModificata.LockBits(New Rectangle(0, 0, immagineRed.Width, immagineRed.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
  18.                 Dim bmSrc As BitmapData = bSrc.LockBits(New Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
  19.  
  20.                 Dim SrcScan0 As System.IntPtr = bmSrc.Scan0
  21.                 Dim stride As Integer = bmData.Stride
  22.                 Dim Scan0 As System.IntPtr = bmData.Scan0
  23.                 Dim bytes As Integer = (immagineRed.Width * immagineRed.Height * 3) - 2
  24.                 Dim p2(bytes - 1) As Byte
  25.                 Dim bytes1 As Integer = (immagineRed.Width * immagineRed.Height * 3) - 2
  26.                 Dim pSrc(bytes1 - 1) As Byte
  27.  
  28.                 ' Copy the RGB values into the array.
  29.                 System.Runtime.InteropServices.Marshal.Copy(Scan0, p2, 0, bytes)
  30.                 System.Runtime.InteropServices.Marshal.Copy(SrcScan0, pSrc, 0, bytes1)
  31.  
  32.                 Dim nOffset As Integer = stride - bmData.Width * 3
  33.                 ' sicurezza che la variabile "Col_Oggetto" venga inpostata manualmente su un colore di
  34.                 'un oggetto per evitare la ripetizione della ricerca dei punti inseriti
  35.  
  36.                 If Funzione = Funzioni.TrovaOggettiDiUnColore Then
  37.                     If ColoreOggetto = Nothing Then
  38.                         Col_Oggetto = Color.FromArgb(p2(c + 2), p2(c + 1), p2(c))
  39.                     Else
  40.                         Col_Oggetto = ColoreOggetto
  41.                     End If
  42.                 Else
  43.                     Col_Oggetto = Color.FromArgb(p2(c + 2), p2(c + 1), p2(c))
  44.                 End If
  45.  
  46.                 Oggetto.p_t.Add(point)
  47.                 Punti.Add(point)
  48.                 Dim i As Int32
  49.                 Do
  50.                     For Each K As Integer In Points
  51.                         Dim sin As Integer, dest As Integer, sop As Integer, Sot As Integer
  52.  
  53.                         Dim J_ As Point, d_ As Point, s_ As Point, ot_ As Point, Score As Point
  54.                         Score = Punti(i)
  55.  
  56.                         'testa il punto a destra di questo
  57.                         If K > 3 Then
  58.                             sin = K - 3
  59.                             J_ = New Point(Score.X - 1, Score.Y)
  60.                         End If
  61.  
  62.                         ColoreAttuale = Color.FromArgb(pSrc(sin + 2), pSrc(sin + 1), pSrc(sin))
  63.                         If IsNearestColor(Col_Oggetto, ColoreAttuale, CoefficenteOggetto) = True Then 'Funzione Di Confronto Colore con coefficente
  64.  
  65.                             If ArrayPoint.IndexOf(sin) = -1 Then
  66.                                 p2(sin + 2) = Color.Red.R
  67.                                 p2(sin + 1) = Color.Red.G
  68.                                 p2(sin) = Color.Red.B
  69.  
  70.                                 Oggetto.p_t.Add(J_)'oggetto che conterra le coordinate dei punti
  71.  
  72.                                 NewPunti.Add(J_)
  73.                                 NewPoints.Add(sin)
  74.  
  75.                                 PuntiInseriti.Add(sin)'Array Di integer  Totali
  76.                                 ArrayPoint.Add(sin)
  77.  
  78.                                 PuntiInseriti.Add(sin + 1)
  79.                                 ArrayPoint.Add(sin + 1)
  80.  
  81.  
  82.                                 PuntiInseriti.Add(sin + 2)
  83.                                 ArrayPoint.Add(sin + 2)
  84.                             End If
  85.                         Else
  86.                             p2(sin + 2) = SostCol.R
  87.                             p2(sin + 1) = SostCol.G
  88.                             p2(sin) = SostCol.B
  89.  
  90.                         End If
  91.                         If K < (bytes - 3) Then
  92.                             dest = K + 3
  93.                             d_ = New Point(Score.X + 1, Score.Y)
  94.                         End If
  95.  
  96.                         ColoreAttuale = Color.FromArgb(pSrc(dest + 2), pSrc(dest + 1), pSrc(dest))
  97.                         If IsNearestColor(Col_Oggetto, ColoreAttuale, CoefficenteOggetto) = True Then
  98.  
  99.                             If ArrayPoint.IndexOf(dest) = -1 Then
  100.                                 p2(dest + 2) = Color.Red.R
  101.                                 p2(dest + 1) = Color.Red.G
  102.                                 p2(dest) = Color.Red.B
  103.  
  104.                                 Oggetto.p_t.Add(d_)
  105.  
  106.                                 NewPunti.Add(d_)
  107.                                 NewPoints.Add(dest)
  108.  
  109.                                 PuntiInseriti.Add(dest)
  110.                                 ArrayPoint.Add(dest)
  111.  
  112.                                 PuntiInseriti.Add(dest + 1)
  113.                                 ArrayPoint.Add(dest + 1)
  114.  
  115.  
  116.                                 PuntiInseriti.Add(dest + 2)
  117.                                 ArrayPoint.Add(dest + 2)
  118.                             End If
  119.                         Else
  120.                             p2(dest + 2) = SostCol.R
  121.                             p2(dest + 1) = SostCol.G
  122.                             p2(dest) = SostCol.B
  123.  
  124.                         End If
  125.                         If K > (stride + nOffset) Then
  126.                             sop = K - stride - nOffset
  127.                             s_ = New Point(Score.X, Score.Y - 1)
  128.                         End If
  129.  
  130.                         ColoreAttuale = Color.FromArgb(pSrc(sop + 2), pSrc(sop + 1), pSrc(sop))
  131.                         If IsNearestColor(Col_Oggetto, ColoreAttuale, CoefficenteOggetto) = True Then
  132.  
  133.                             If ArrayPoint.IndexOf(sop) = -1 Then
  134.                                 p2(sop + 2) = Color.Red.R
  135.                                 p2(sop + 1) = Color.Red.G
  136.                                 p2(sop) = Color.Red.B
  137.  
  138.                                 Oggetto.p_t.Add(s_)'Oggetto dell'area
  139.  
  140.                                 NewPunti.Add(s_)
  141.                                 NewPoints.Add(sop)
  142.  
  143.                                 PuntiInseriti.Add(sop)'Array Di intege Totali
  144.                                 ArrayPoint.Add(sop)
  145.  
  146.                                 PuntiInseriti.Add(sop + 1)
  147.                                 ArrayPoint.Add(sop + 1)
  148.  
  149.                                 PuntiInseriti.Add(sop + 2)
  150.                                 ArrayPoint.Add(sop + 2)
  151.                             End If
  152.                         Else
  153.                             p2(sop + 2) = SostCol.R
  154.                             p2(sop + 1) = SostCol.G
  155.                             p2(sop) = SostCol.B
  156.                         End If
  157.  
  158.                         If K < (bytes - stride - nOffset) Then
  159.                             Sot = K + stride + nOffset
  160.                             ot_ = New Point(Score.X, Score.Y + 1)
  161.                         End If
  162.  
  163.                         If Sot < bytes Then
  164.                             ColoreAttuale = Color.FromArgb(pSrc(Sot + 2), pSrc(Sot + 1), pSrc(Sot))
  165.                             If IsNearestColor(Col_Oggetto, ColoreAttuale, CoefficenteOggetto) = True Then
  166.  
  167.                                 If ArrayPoint.IndexOf(Sot) = -1 Then
  168.                                     p2(Sot + 2) = Color.Red.R
  169.                                     p2(Sot + 1) = Color.Red.G
  170.                                     p2(Sot) = Color.Red.B
  171.  
  172.                                     Oggetto.p_t.Add(ot_)
  173.  
  174.                                     NewPunti.Add(ot_)
  175.                                     NewPoints.Add(Sot)
  176.  
  177.                                     PuntiInseriti.Add(Sot)
  178.                                     ArrayPoint.Add(Sot)
  179.  
  180.                                     PuntiInseriti.Add(Sot + 1)
  181.                                     ArrayPoint.Add(Sot + 1)
  182.  
  183.  
  184.                                     PuntiInseriti.Add(Sot + 2)
  185.                                     ArrayPoint.Add(Sot + 2)
  186.                                 End If
  187.                             Else
  188.                                 p2(Sot + 2) = SostCol.R
  189.                                 p2(Sot + 1) = SostCol.G
  190.                                 p2(Sot) = SostCol.B
  191.                             End If
  192.                         End If
  193.                         i += 1
  194.                     Next
  195.                     Points.Clear()
  196.                     Points.AddRange(NewPoints)
  197.                     NewPoints.Clear()
  198.                     Punti.Clear()
  199.                     Punti.AddRange(NewPunti)
  200.                     NewPunti.Clear()
  201.                     i = 0
  202.  
  203.                 Loop Until Points.Count = 0
  204.  
  205.                 ' Copy the RGB values back to the bitmap
  206.                 System.Runtime.InteropServices.Marshal.Copy(pSrc, 0, SrcScan0, bytes1)
  207.                 System.Runtime.InteropServices.Marshal.Copy(p2, 0, Scan0, bytes)
  208.  
  209.                 imgModificata.UnlockBits(bmData)
  210.                 bSrc.UnlockBits(bmSrc)
  211.  
  212.                 If (ArrayPoint.Count / 3) > PuntiPerPallino Then
  213.                     immagineRed = imgModificata
  214.                 End If
  215.  
  216.                 Return ArrayPoint
  217.             End Function



Ringrazio tutti dell'aiuto che date continumente

PM Quote