2012-07-10 2 views
0

다음 코드는 현재 수행하고있는 작업에 적합합니다. 그러나, 그것은 내가 필요로하는 것보다 오래 걸리고 있습니다. 문제는 함수보다 큼 /보다 작음을 반복하며 시간이 오래 걸린다는 것입니다. 나는 약간의 연구를 해왔지만 더 빨리 실행되도록 모든 것을 줄이는 방법을 알아낼 수는 없습니다.VB.Net - Lockbits - Greater Than/Less Than functions

화소의 RGB 값을 실행해야 할 다음 tests- (1) 하나 개의 값이 250보다 크면, 다른 5 미만 (2) 하나 개의 값 미만인 경우 (5), 다른해야 250보다 커야합니다. (3) 한 값이 0이면 다른 값은 0보다 커야합니다. (2) 두 값의 차이가 15보다 작아야합니다 (또는 내가 설정 한 다른 임계 값) (5) 두 값이 0인지 확인하십시오.

또한 각각의 기능을 수행 한 후에 '종료'를 수행 할 예정입니까?

' Create new bitmap from filepath in TextBox1 
    Dim bmp As New Bitmap(TextBox1.Text) 

    ' Lock the bitmap's pixels 
    Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height) 
    Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, _ 
      Drawing.Imaging.ImageLockMode.ReadWrite, _ 
      Imaging.PixelFormat.Format24bppRgb) 

    ' Get the address of the first line 
    Dim ptr As IntPtr = bmpData.Scan0 

    ' Declare an array to hold the bytes of the bitmap 
    Dim bytes As Integer = Math.Abs(bmpData.Stride) * bmp.Height 
    Dim rgbValues(bytes - 1) As Byte 
    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes) 

    ' Retrieve RGB values 
    Dim RedValue As Int32 
    Dim GreenValue As Int32 
    Dim BlueValue As Int32 
    Dim l As Integer = 0     
    For x = 0 To bmp.Width 
     For y = 0 To bmp.Height - 1 
      l = ((bmp.Width * 3 * y) + (x * 3)) 
      RedValue = rgbValues(l) 
      GreenValue = rgbValues(l + 1) 
      BlueValue = rgbValues(l + 2) 

      If RedValue < 5 AndAlso GreenValue < 5 AndAlso BlueValue > 250 Then 
      ElseIf RedValue < 5 AndAlso GreenValue > 250 AndAlso BlueValue < 5 Then 
      ElseIf RedValue > 250 AndAlso GreenValue < 5 AndAlso BlueValue < 5 Then 
      ElseIf RedValue > 250 AndAlso GreenValue > 250 AndAlso BlueValue < 5 Then 
      ElseIf RedValue > 250 AndAlso GreenValue < 5 AndAlso BlueValue > 250 Then 
      ElseIf RedValue < 5 AndAlso GreenValue > 250 AndAlso BlueValue > 250 Then 
      ElseIf RedValue > 0 AndAlso GreenValue > 0 AndAlso BlueValue.Equals(0) Then 
      ElseIf RedValue > 0 AndAlso GreenValue.Equals(0) AndAlso BlueValue > 0 Then 
      ElseIf RedValue.Equals(0) AndAlso GreenValue > 0 AndAlso BlueValue > 0 Then 
      ElseIf (RedValue - GreenValue) < 15 AndAlso (RedValue - BlueValue) < 15 AndAlso _ 
       (GreenValue - RedValue) < 15 AndAlso (GreenValue - BlueValue) < 15 AndAlso _ 
       (BlueValue - RedValue) < 15 AndAlso (BlueValue - GreenValue) < 15 Then 
      ElseIf RedValue.Equals(GreenValue) Then 
      ElseIf RedValue.Equals(BlueValue) Then 
      ElseIf GreenValue.Equals(BlueValue) Then 
      ElseIf RedValue.Equals(BlueValue) AndAlso RedValue.Equals(GreenValue) _ 
       AndAlso BlueValue.Equals(GreenValue) Then 
      Else 
       MsgBox("Image is color.") 
       Exit Sub 
      End If 
     Next 
    Next 
    MsgBox("Image is grayscale.") 

    ' Unlock the bitmap 
    bmp.UnlockBits(bmpData) 
+0

는 I는 로직 혼란입니다. (1)이 참이면, (2)는 결코 참일 수 없다. 그리고 (1) 또는 (2)가 참이라면 (4)는 사실 일 수 없다. 이것은 일련의 검사입니까, 아니면 모든 RGB 값이 모든 규칙을 준수해야합니까? 시퀀스 인 경우 중첩 된 If는 약간 더 나은 성능을 제공 할 수 있습니다. – APrough

+0

각 테스트가 배타적이므로 일련의 검사입니다. 각 RGB 값이 각 테스트를 따르는 것은 아닙니다. 정확성 (255 : 255 : 0, 0 : 0 : 255 등)을 위해 극단적 인 차이를 제거하는 것을 포함하여 색상을 지정하지 않는 모든 픽셀을 제거하려고합니다. 나는 If 한 쌍을 보금 자리로 만들려고 노력할 것이고 그것이 도움이되는지 보게 될 것이다. 생각하지 않았어. 감사. –

+0

@Jerry 두 계정이 연결되어 있습니까? (Jerry Horak와 당신). 그렇다면 계정을 통합 할 수 있도록 알려주십시오. –

답변

0

그레이 컬러는 항상 본 명세서 (R = G = B)있다. 예. #CCCCCC = [R : 204 G : 204 B : 204]

If Not R = G And G = B Then 
MsgBox("colored") 
Exit Sub 
End If