我是vb.net和c#的初级程序员,从事一个稍微高级的项目。 在用于图像处理的类库中,我定义了一个用于快速像素访问的类,它将4个通道位图转换为bitmap.width * bitmap.height * 4大小的字节数组(类在代码区#1中再现)。 然后我对字节数组采取措施来改变一些像素值(代码部分#2贝娄是它的一个样本)。该代码非常重复,但我还没有找到一种方法使其更简洁和系统化。我在想这里的可用性。虽然速度非常快,但我希望尽可能不改变方法。为了相同的目的,Emgu.CV.Image设置像素方法和image.data方法都慢得多。此外,Emgu库中没有过滤器可以让我尽可能多地控制我想要实现的更改。 感谢您的时间和想法
第1节:类定义
Private Class BitmapBytesRGB32
Public ImageBytes() As Byte
Public RowSizeBytes As Integer
Public Const bytesPerPixel As Integer = 4
Private m_Bitmap As Bitmap ' Creates a new private bitmap for future operation
Private m_BitmapData As BitmapData ' and a private bitmapData class
Public total_size As Integer
Public Sub New(ByVal bm As Bitmap) ' passes the bitmap ref inside our BitmapBytesRGB32 class
m_Bitmap = bm
End Sub
Public Sub LockBitmap() ' Lock the bitmap's data.
Dim bounds As Rectangle = New Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height)
m_BitmapData = m_Bitmap.LockBits(bounds, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppArgb)
RowSizeBytes = m_BitmapData.Stride
total_size = m_BitmapData.Stride * m_BitmapData.Height ' Allocate room for the data.
ReDim ImageBytes(total_size)
Marshal.Copy(m_BitmapData.Scan0, ImageBytes, 0, total_size) ' Copy the data into the ImageBytes array.
End Sub
Public Sub UnlockBitmap() ' Copy the data back into the bitmap
Dim total_size As Integer = m_BitmapData.Stride * m_BitmapData.Height
Marshal.Copy(ImageBytes, 0, m_BitmapData.Scan0, total_size)
m_Bitmap.UnlockBits(m_BitmapData) ' Unlock the bitmap.
ImageBytes = Nothing ' Release resources.
m_BitmapData = Nothing ' Release resources.
End Sub
End Class
第2节:子方法
Private Sub PixelBasedCorrections(ByRef m_bitmap As Bitmap)
Dim bm_bytes As New BitmapBytesRGB32(m_bitmap)
'1
bm_bytes.LockBitmap()
For Xcount = 1 To m_bitmap.Width - 2
For Ycount = 0 To m_bitmap.Height - 1
Dim pixPosition1 As Integer = (Ycount * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition2 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount - 1) * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition3 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount + 1) * BitmapBytesRGB32.bytesPerPixel)
Dim color1 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition1 + 2), bm_bytes.ImageBytes(pixPosition1 + 1), bm_bytes.ImageBytes(pixPosition1))
Dim color2 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition2 + 2), bm_bytes.ImageBytes(pixPosition2 + 1), bm_bytes.ImageBytes(pixPosition2))
Dim color3 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition3 + 2), bm_bytes.ImageBytes(pixPosition3 + 1), bm_bytes.ImageBytes(pixPosition3))
If color1.ToArgb <> Color.Black.ToArgb AndAlso color1.ToArgb <> color2.ToArgb AndAlso
color2.ToArgb <> Color.Black.ToArgb AndAlso color3.ToArgb = Color.Black.ToArgb Then
bm_bytes.ImageBytes(pixPosition1) = bm_bytes.ImageBytes(pixPosition2)
bm_bytes.ImageBytes(pixPosition1 + 1) = bm_bytes.ImageBytes(pixPosition2 + 1)
bm_bytes.ImageBytes(pixPosition1 + 2) = bm_bytes.ImageBytes(pixPosition2 + 2)
End If
Next
Next
'2
For Xcount = 2 To m_bitmap.Width - 3
For Ycount = 0 To m_bitmap.Height - 1
Dim pixPosition1 As Integer = (Ycount * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition2 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount - 1) * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition3 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount + 2) * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition4 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount - 2) * BitmapBytesRGB32.bytesPerPixel)
Dim color1 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition1 + 2), bm_bytes.ImageBytes(pixPosition1 + 1), bm_bytes.ImageBytes(pixPosition1))
Dim color2 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition2 + 2), bm_bytes.ImageBytes(pixPosition2 + 1), bm_bytes.ImageBytes(pixPosition2))
Dim color3 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition3 + 2), bm_bytes.ImageBytes(pixPosition3 + 1), bm_bytes.ImageBytes(pixPosition3))
Dim color4 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition4 + 2), bm_bytes.ImageBytes(pixPosition4 + 1), bm_bytes.ImageBytes(pixPosition4))
If color1.ToArgb <> Color.Black.ToArgb AndAlso color1.ToArgb <> color2.ToArgb AndAlso
color2.ToArgb <> Color.Black.ToArgb AndAlso color3.ToArgb = Color.Black.ToArgb AndAlso
color4.ToArgb <> Color.Black.ToArgb Then 'xx
bm_bytes.ImageBytes(pixPosition1) = bm_bytes.ImageBytes(pixPosition4)
bm_bytes.ImageBytes(pixPosition1 + 1) = bm_bytes.ImageBytes(pixPosition4 + 1)
bm_bytes.ImageBytes(pixPosition1 + 2) = bm_bytes.ImageBytes(pixPosition4 + 2)
End If
Next
Next
'3
For Xcount = 1 To m_bitmap.Width - 2
For Ycount = 0 To m_bitmap.Height - 1
Dim pixPosition1 As Integer = (Ycount * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition2 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount + 1) * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition3 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount - 1) * BitmapBytesRGB32.bytesPerPixel)
Dim color1 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition1 + 2), bm_bytes.ImageBytes(pixPosition1 + 1), bm_bytes.ImageBytes(pixPosition1))
Dim color2 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition2 + 2), bm_bytes.ImageBytes(pixPosition2 + 1), bm_bytes.ImageBytes(pixPosition2))
Dim color3 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition3 + 2), bm_bytes.ImageBytes(pixPosition3 + 1), bm_bytes.ImageBytes(pixPosition3))
If color1.ToArgb <> Color.Black.ToArgb AndAlso color1.ToArgb <> color2.ToArgb AndAlso
color2.ToArgb <> Color.Black.ToArgb AndAlso color3.ToArgb = Color.Black.ToArgb Then
bm_bytes.ImageBytes(pixPosition1) = bm_bytes.ImageBytes(pixPosition2)
bm_bytes.ImageBytes(pixPosition1 + 1) = bm_bytes.ImageBytes(pixPosition2 + 1)
bm_bytes.ImageBytes(pixPosition1 + 2) = bm_bytes.ImageBytes(pixPosition2 + 2)
End If
Next
Next
'4
For Xcount = 2 To m_bitmap.Width - 3
For Ycount = 0 To m_bitmap.Height - 1
Dim pixPosition1 As Integer = (Ycount * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition2 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount + 1) * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition3 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount - 2) * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition4 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount + 2) * BitmapBytesRGB32.bytesPerPixel)
Dim color1 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition1 + 2), bm_bytes.ImageBytes(pixPosition1 + 1), bm_bytes.ImageBytes(pixPosition1))
Dim color2 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition2 + 2), bm_bytes.ImageBytes(pixPosition2 + 1), bm_bytes.ImageBytes(pixPosition2))
Dim color3 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition3 + 2), bm_bytes.ImageBytes(pixPosition3 + 1), bm_bytes.ImageBytes(pixPosition3))
Dim color4 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition4 + 2), bm_bytes.ImageBytes(pixPosition4 + 1), bm_bytes.ImageBytes(pixPosition4))
If color1.ToArgb <> Color.Black.ToArgb AndAlso color1.ToArgb <> color2.ToArgb AndAlso
color2.ToArgb <> Color.Black.ToArgb AndAlso color3.ToArgb = Color.Black.ToArgb AndAlso
color4.ToArgb <> Color.Black.ToArgb Then 'xx
bm_bytes.ImageBytes(pixPosition1) = bm_bytes.ImageBytes(pixPosition4)
bm_bytes.ImageBytes(pixPosition1 + 1) = bm_bytes.ImageBytes(pixPosition4 + 1)
bm_bytes.ImageBytes(pixPosition1 + 2) = bm_bytes.ImageBytes(pixPosition4 + 2)
End If
Next
Next
'5
For Xcount = 1 To m_bitmap.Width - 2
For Ycount = 0 To m_bitmap.Height - 1
Dim pixPosition1 As Integer = (Ycount * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition2 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount + 1) * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition3 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount - 1) * BitmapBytesRGB32.bytesPerPixel)
Dim color1 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition1 + 2), bm_bytes.ImageBytes(pixPosition1 + 1), bm_bytes.ImageBytes(pixPosition1))
Dim color2 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition2 + 2), bm_bytes.ImageBytes(pixPosition2 + 1), bm_bytes.ImageBytes(pixPosition2))
Dim color3 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition3 + 2), bm_bytes.ImageBytes(pixPosition3 + 1), bm_bytes.ImageBytes(pixPosition3))
If color1.ToArgb <> Color.Black.ToArgb AndAlso color1.ToArgb <> color2.ToArgb AndAlso
color2.ToArgb <> Color.Black.ToArgb AndAlso color3.ToArgb = Color.Black.ToArgb Then
bm_bytes.ImageBytes(pixPosition1) = bm_bytes.ImageBytes(pixPosition2)
bm_bytes.ImageBytes(pixPosition1 + 1) = bm_bytes.ImageBytes(pixPosition2 + 1)
bm_bytes.ImageBytes(pixPosition1 + 2) = bm_bytes.ImageBytes(pixPosition2 + 2)
End If
Next
Next
'6
For Xcount = 0 To m_bitmap.Width - 1
For Ycount = 1 To m_bitmap.Height - 2
Dim pixPosition1 As Integer = (Ycount * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition2 As Integer = ((Ycount - 1) * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition3 As Integer = ((Ycount + 1) * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim color1 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition1 + 2), bm_bytes.ImageBytes(pixPosition1 + 1), bm_bytes.ImageBytes(pixPosition1))
Dim color2 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition2 + 2), bm_bytes.ImageBytes(pixPosition2 + 1), bm_bytes.ImageBytes(pixPosition2))
Dim color3 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition3 + 2), bm_bytes.ImageBytes(pixPosition3 + 1), bm_bytes.ImageBytes(pixPosition3))
If color1.ToArgb <> Color.Black.ToArgb AndAlso color1.ToArgb <> color2.ToArgb AndAlso
color2.ToArgb <> Color.Black.ToArgb AndAlso color3.ToArgb = Color.Black.ToArgb Then
bm_bytes.ImageBytes(pixPosition1) = bm_bytes.ImageBytes(pixPosition2)
bm_bytes.ImageBytes(pixPosition1 + 1) = bm_bytes.ImageBytes(pixPosition2 + 1)
bm_bytes.ImageBytes(pixPosition1 + 2) = bm_bytes.ImageBytes(pixPosition2 + 2)
End If
Next
Next
'7
For Xcount = 0 To m_bitmap.Width - 1
For Ycount = 1 To m_bitmap.Height - 2
Dim pixPosition1 As Integer = (Ycount * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition2 As Integer = ((Ycount + 1) * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition3 As Integer = ((Ycount - 1) * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim color1 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition1 + 2), bm_bytes.ImageBytes(pixPosition1 + 1), bm_bytes.ImageBytes(pixPosition1))
Dim color2 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition2 + 2), bm_bytes.ImageBytes(pixPosition2 + 1), bm_bytes.ImageBytes(pixPosition2))
Dim color3 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition3 + 2), bm_bytes.ImageBytes(pixPosition3 + 1), bm_bytes.ImageBytes(pixPosition3))
If color1.ToArgb <> Color.Black.ToArgb AndAlso color1.ToArgb <> color2.ToArgb AndAlso
color2.ToArgb <> Color.Black.ToArgb AndAlso color3.ToArgb = Color.Black.ToArgb Then
bm_bytes.ImageBytes(pixPosition1) = bm_bytes.ImageBytes(pixPosition2)
bm_bytes.ImageBytes(pixPosition1 + 1) = bm_bytes.ImageBytes(pixPosition2 + 1)
bm_bytes.ImageBytes(pixPosition1 + 2) = bm_bytes.ImageBytes(pixPosition2 + 2)
End If
Next
Next
'8
For Xcount = 2 To m_bitmap.Width - 3
For Ycount = 0 To m_bitmap.Height - 1
Dim pixPosition1 As Integer = (Ycount * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition2 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount + 1) * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition3 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount - 1) * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition4 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount + 2) * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition5 As Integer = (Ycount * bm_bytes.RowSizeBytes) + ((Xcount - 2) * BitmapBytesRGB32.bytesPerPixel)
Dim color1 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition1 + 2), bm_bytes.ImageBytes(pixPosition1 + 1), bm_bytes.ImageBytes(pixPosition1))
Dim color2 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition2 + 2), bm_bytes.ImageBytes(pixPosition2 + 1), bm_bytes.ImageBytes(pixPosition2))
Dim color3 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition3 + 2), bm_bytes.ImageBytes(pixPosition3 + 1), bm_bytes.ImageBytes(pixPosition3))
Dim color4 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition4 + 2), bm_bytes.ImageBytes(pixPosition4 + 1), bm_bytes.ImageBytes(pixPosition4))
Dim color5 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition5 + 2), bm_bytes.ImageBytes(pixPosition5 + 1), bm_bytes.ImageBytes(pixPosition5))
If color1.ToArgb <> Color.Black.ToArgb AndAlso color1.ToArgb <> color2.ToArgb AndAlso
color2.ToArgb <> Color.Black.ToArgb AndAlso color2.ToArgb = color3.ToArgb AndAlso
color4.ToArgb = color3.ToArgb AndAlso color5.ToArgb = color3.ToArgb Then
bm_bytes.ImageBytes(pixPosition1) = bm_bytes.ImageBytes(pixPosition3)
bm_bytes.ImageBytes(pixPosition1 + 1) = bm_bytes.ImageBytes(pixPosition3 + 1)
bm_bytes.ImageBytes(pixPosition1 + 2) = bm_bytes.ImageBytes(pixPosition3 + 2)
End If
Next
Next
'9
For Xcount = 0 To m_bitmap.Width - 1
For Ycount = 2 To m_bitmap.Height - 3
Dim pixPosition1 As Integer = (Ycount * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition2 As Integer = ((Ycount + 1) * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition3 As Integer = ((Ycount - 1) * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition4 As Integer = ((Ycount + 2) * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim pixPosition5 As Integer = ((Ycount - 2) * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
Dim color1 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition1 + 2), bm_bytes.ImageBytes(pixPosition1 + 1), bm_bytes.ImageBytes(pixPosition1))
Dim color2 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition2 + 2), bm_bytes.ImageBytes(pixPosition2 + 1), bm_bytes.ImageBytes(pixPosition2))
Dim color3 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition3 + 2), bm_bytes.ImageBytes(pixPosition3 + 1), bm_bytes.ImageBytes(pixPosition3))
Dim color4 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition4 + 2), bm_bytes.ImageBytes(pixPosition4 + 1), bm_bytes.ImageBytes(pixPosition4))
Dim color5 As Color = Color.FromArgb(255, bm_bytes.ImageBytes(pixPosition5 + 2), bm_bytes.ImageBytes(pixPosition5 + 1), bm_bytes.ImageBytes(pixPosition5))
If color1.ToArgb <> Color.Black.ToArgb AndAlso color1.ToArgb <> color2.ToArgb AndAlso
color2.ToArgb <> Color.Black.ToArgb AndAlso color2.ToArgb = color3.ToArgb AndAlso
color4.ToArgb = color3.ToArgb AndAlso color5.ToArgb = color3.ToArgb Then
bm_bytes.ImageBytes(pixPosition1) = bm_bytes.ImageBytes(pixPosition3)
bm_bytes.ImageBytes(pixPosition1 + 1) = bm_bytes.ImageBytes(pixPosition3 + 1)
bm_bytes.ImageBytes(pixPosition1 + 2) = bm_bytes.ImageBytes(pixPosition3 + 2)
End If
Next
Next
' MORE CODE IN THE SAME VEIN.......
bm_bytes.UnlockBitmap()
答案 0 :(得分:0)
你可以替换像
这样的行Dim pixPosition1 As Integer = (Ycount * bm_bytes.RowSizeBytes) + (Xcount * BitmapBytesRGB32.bytesPerPixel)
带
Dim pixPosition1 As Integer = calcPixelPos(Ycount,0,xCount,0)
Dim color` as Color = CalcColor(pixPosition1,1,2)
添加将每行差异作为参数的函数 - 如下所示: -
Private Function calcPixelpos(pYcount As Integer, pYoffset As Integer, pXcount As Integer, pXoffeset As Integer) As Integer
Return (pYcount + pYoffset * bm_bytes.RowSizeBytes) + (pXcount + pXoffeset * BitmapBytesRGB32.bytesPerPixel)
End Function
Private Function CalcColor(mPxPos As Integer, mOffset1 As Integer, mOffset2 As Integer) As Color
Return Color.FromArgb(255, bm_bytes.ImageBytes(mPxPos + mOffset1), bm_bytes.ImageBytes(mPxPos + mOffset2), bm_bytes.ImageBytes(mPxPos))
End Function
虽然您仍然需要进行一些复制和粘贴,但它会减少编辑每个粘贴所需的时间,因为所有差异都更加接近。您也可以将相同类型的想法应用于每种颜色检查中的三行.End .. Endif Statement