jhylands

Image colour summarisation.

Aug 8th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.33 KB | None | 0 0
  1. Public Class Form1
  2.     Structure SColour
  3.         Dim da() As Boolean
  4.     End Structure
  5.     Structure NotRGB
  6.         Dim red As Int16
  7.         Dim green As Int16
  8.         Dim blue As Int16
  9.     End Structure
  10.  
  11.     Private _rGB As Integer
  12.  
  13.  
  14.     Function toRGB(ByVal Red, ByVal Green, ByVal Blue) As NotRGB
  15.         toRGB.red = Red
  16.         toRGB.blue = Blue
  17.         toRGB.green = Green
  18.  
  19.     End Function
  20.     Function score(ByVal colour1, ByVal colour)
  21.         Dim total As Integer
  22.         total = ((colour1.red - colour.red) ^ 2) ^ 0.5
  23.         total += ((colour1.green - colour.green) ^ 2) ^ 0.5
  24.         total += ((colour1.blue - colour.blue) ^ 2) ^ 0.5
  25.         score = total
  26.     End Function
  27.     Function ifgray(ByVal colour) As Boolean
  28.         colour.red = Math.Round(colour.red / 10)
  29.         colour.green = Math.Round(colour.green / 10)
  30.         colour.blue = Math.Round(colour.blue / 10)
  31.         If colour.red > colour.green Then 'R>G
  32.             If colour.blue > colour.green Then 'R>G B>G
  33.                 If colour.blue > colour.red Then 'B>R>G
  34.                     If (colour.blue = colour.red Or colour.red + 1 = colour.blue) And (colour.red = colour.green Or colour.green + 1 = colour.red) Then
  35.                         Return True 'gray
  36.                     Else
  37.                         Return False 'not gray
  38.                     End If
  39.                     Exit Function
  40.                 Else 'R>B>G
  41.                     If (colour.blue = colour.red Or colour.red - 1 = colour.blue) And (colour.blue = colour.green Or colour.green + 1 = colour.blue) Then
  42.                         Return True 'gray
  43.                     Else
  44.                         Return False 'not gray
  45.                     End If
  46.                     Exit Function
  47.                 End If
  48.             Else 'R>G>B
  49.  
  50.             End If
  51.         Else 'G>R
  52.             If colour.blue > colour.green Then 'B>G>R
  53.                 If (colour.blue = colour.green Or colour.blue - 1 = colour.green) And (colour.red = colour.green Or colour.green - 1 = colour.red) Then
  54.                     Return True 'gray
  55.                 Else
  56.                     Return False 'not gray
  57.                 End If
  58.                 Exit Function
  59.             Else 'G>R G>B
  60.                 If colour.red > colour.blue Then 'G>R>B
  61.                     If (colour.blue = colour.red Or colour.red + 1 = colour.blue) And (colour.red = colour.green Or colour.green + 1 = colour.red) Then
  62.                         Return True 'gray
  63.                     Else
  64.                         Return False 'not gray
  65.                     End If
  66.                     Exit Function
  67.                 Else 'G>B>R
  68.                     If (colour.blue = colour.red Or colour.red + 1 = colour.blue) And (colour.blue = colour.green Or colour.green - 1 = colour.blue) Then
  69.                         Return True 'gray
  70.                     Else
  71.                         Return False 'not gray
  72.                     End If
  73.                     Exit Function
  74.                 End If
  75.             End If
  76.         End If
  77.  
  78.     End Function
  79.     Function ToSColour(ByVal colour)
  80.         Dim scores As Int16 = 1000 'The largest score
  81.         Dim cur As Int16 ' a hold for the current score
  82.         Dim col As Int16 ' The colour as number
  83.  
  84.         If colour.red < 30 And colour.blue < 30 And colour.green < 30 Then
  85.             col = 0 ' black
  86.         ElseIf colour.red > 230 And colour.blue > 230 And colour.green > 230 Then
  87.             col = 1 'white
  88.         ElseIf ifgray(colour) Then
  89.             col = 2 ' gray - This is because all of the primary colours are within 10 of each other
  90.         ElseIf colour.red + colour.blue <= colour.green - 10 Then
  91.             col = 6 'green
  92.         ElseIf colour.green < 40 And colour.red > 100 And colour.red < 200 And colour.blue < 100 And colour.blue > 50 Then
  93.             col = 3 'brown - to stop it making green brown
  94.         Else
  95.             For i = 4 To 7
  96.                 cur = score(colour, ASColour(i))
  97.                 If cur < scores Then
  98.                     scores = cur
  99.                     col = i
  100.                 End If
  101.                 'MsgBox("R:" & colour.red & "G:" & colour.green & "B:" & colour.blue & "I:" & i & "cur:" & cur & "Col:" & col)
  102.             Next
  103.         End If
  104.         Return col
  105.     End Function 'which clour is closest
  106.     Dim ASColour(10) As NotRGB
  107.     Private Sub BTranslate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTranslate.Click
  108.         Dim image As Bitmap = PictureBox1.Image
  109.         Dim colourHold As NotRGB 'holds the current pixles colour
  110.         For h = 0 To image.Height - 1
  111.             For w = 0 To image.Width - 1
  112.                 'get the colours
  113.                 colourHold.red = image.GetPixel(w, h).R
  114.                 colourHold.green = image.GetPixel(w, h).G
  115.                 colourHold.blue = image.GetPixel(w, h).B
  116.                 'prosses
  117.                 colourHold = ASColour(ToSColour(colourHold))
  118.                 'MsgBox(colourHold.red)
  119.                 'set colours
  120.                 image.SetPixel(w, h, ColorTranslator.FromOle(RGB(colourHold.red, colourHold.green, colourHold.blue)))
  121.             Next
  122.  
  123.  
  124.         Next
  125.         PictureBox2.Image = image
  126.  
  127.  
  128.     End Sub
  129.  
  130.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  131.         ASColour(0) = toRGB(255, 255, 255) 'white
  132.         ASColour(1) = toRGB(0, 0, 0) 'black
  133.         ASColour(2) = toRGB(128, 128, 128) 'gray
  134.         ASColour(3) = toRGB(150, 75, 0) 'brown
  135.         ASColour(4) = toRGB(255, 0, 0) 'red
  136.         ASColour(5) = toRGB(255, 255, 0) 'yellow
  137.         ASColour(6) = toRGB(0, 255, 0) 'green
  138.         ASColour(7) = toRGB(0, 0, 255)
  139.  
  140.     End Sub
  141.  
  142.     Private Sub fetch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fetch.Click
  143.         Try
  144.             Dim coordinates() As String = Split(TextBox1.Text, ":")
  145.             Dim imagean As Bitmap = PictureBox1.Image
  146.             MsgBox(imagean.GetPixel(Int(coordinates(0)), Int(coordinates(1))).R & ":" & imagean.GetPixel(Int(coordinates(0)), Int(coordinates(1))).B & ":" & imagean.GetPixel(Int(coordinates(0)), Int(coordinates(1))).G)
  147.         Catch
  148.             MsgBox("Error!!!")
  149.         End Try
  150.  
  151.  
  152.     End Sub
  153.  
  154.     Private Sub BLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BLoad.Click
  155.         OpenFileDialog1.ShowDialog()
  156.         Dim path As String = OpenFileDialog1.FileName
  157.         Dim Bitload As New Bitmap(path)
  158.         PictureBox1.Image = Bitload
  159.  
  160.     End Sub
  161. End Class
Advertisement
Add Comment
Please, Sign In to add comment