0

次のコードは、私がやっていることに対してうまく機能します。ただし、必要以上に時間がかかっています。問題は、大なり/小なり関数ごとに反復処理が行われ、時間がかかることです。いくつかの調査を行いましたが、すべてをスリムにして高速に実行する方法がわかりません。

ピクセルの RGB 値は、次のテストを実行する必要があります。(1) 1 つの値が 250 より大きい場合、他の値は 5 未満でなければなりません (2) 1 つの値が 5 未満の場合、他の値は 250 より大きい必要があります。 (3) 1 つの値がゼロに等しい場合、他の値は 0 より大きくなければなりません (4) 2 つの値の差は 15 (または私が設定したその他のしきい値) 未満でなければなりません (5) 2 つの値がゼロに等しいかどうかを確認します

また、これらの関数のそれぞれの後に「Exit For」を実行すると役に立ちますか?

    ' 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)
4

1 に答える 1

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
于 2012-07-19T10:38:09.687 に答える