?? classimageprocessing.cls
字號(hào):
Dim x As Integer
Dim y As Integer
Dim RGBval As Long
Dim maxcol As Long
Dim screenWidth As Single
Dim screenHeight As Single
Dim screenLeft As Single
Dim screenTop As Single
Dim index As Integer
Dim max As Long
Dim redValue As Byte
Dim greenValue As Byte
Dim blueValue As Byte
If (Not IsMissing(left)) And (Not IsMissing(top)) Then
screenLeft = left
screenTop = top
screenWidth = wdth
screenHeight = hght
Else
screenLeft = 0
screenTop = 0
screenWidth = canvas.ScaleWidth
screenHeight = canvas.ScaleHeight
End If
'clear the histogram
For index = 0 To Histogram_levels - 1
ColourHistogram(index) = 0
Next
max = 0
'get the histogram
maxcol = RGB(255, 255, 255)
For x = 0 To screenWidth - 1
For y = 0 To screenHeight - 1
RGBval = canvas.Point(screenLeft + x, screenTop + y)
redValue = getRGBvalue(RGBval, 0)
greenValue = getRGBvalue(RGBval, 1)
blueValue = getRGBvalue(RGBval, 2)
index = Int(getSpectrumValue(redValue, greenValue, blueValue) * Histogram_levels)
ColourHistogram(index) = ColourHistogram(index) + 1
If (ColourHistogram(index) > max) Then
max = ColourHistogram(index)
End If
Next
Next
'normalize the histogram
If (max > 0) Then
For index = 0 To Histogram_levels - 1
ColourHistogram(index) = ColourHistogram(index) / max
Next
End If
End Sub
Public Sub saveColourHistogram(FileNumber As Integer)
Dim i As Integer
Print #FileNumber, Histogram_levels
For i = 0 To Histogram_levels - 1
Print #FileNumber, ColourHistogram(i)
Next
End Sub
Public Sub loadColourHistogram(FileNumber As Integer)
Dim i As Integer
Dim col As Single
Input #FileNumber, Histogram_levels
ReDim ColourHistogram(Histogram_levels)
ReDim Hist(Histogram_levels)
For i = 0 To Histogram_levels - 1
Input #FileNumber, col
ColourHistogram(i) = col
Next
End Sub
Public Sub updateDirect(canvas As PictureBox, Optional left As Variant, Optional top As Variant, Optional wdth As Variant, Optional hght As Variant)
'import a picture pixel-for-pixel without any scaling
'processtype = 0 greyscale
' 1 red
' 2 green
' 3 blue
' 4 edges
' 5 movement
Dim x As Integer
Dim y As Integer
Dim screenX As Integer
Dim screenY As Integer
Dim w As Integer
Dim h As Integer
Dim value As Double
Dim RGBval As Long
Dim pixels As Double
Dim maxcol As Long
Dim edgeValue As Single
Dim screenWidth As Single
Dim screenHeight As Single
Dim screenLeft As Single
Dim screenTop As Single
If (Not IsMissing(left)) And (Not IsMissing(top)) Then
screenLeft = left
screenTop = top
screenWidth = wdth
screenHeight = hght
Else
screenLeft = 0
screenTop = 0
screenWidth = canvas.ScaleWidth
screenHeight = canvas.ScaleHeight
End If
w = CInt(screenWidth / width)
If (w < 1) Then
w = 1
End If
h = CInt(screenHeight / height)
If (h < 1) Then
h = 1
End If
pixels = w * h
maxcol = RGB(255, 255, 255)
For x = 0 To width - 1
For y = 0 To height - 1
edgeTraced(x, y) = False
screenX = screenLeft + ((x / width) * screenWidth)
screenY = screenTop + ((y / height) * screenHeight)
RGBval = canvas.Point(screenX, screenY)
Select Case processType
Case 0 'greyscale
value = (RGBval / maxcol)
Case 1 'red
value = ((RGBval And 255) / 255)
Case 2 'green
value = ((RGBval And 65280) / 65280)
Case 3 'blue
value = ((RGBval And 16711680) / 16711680)
End Select
image(x, y) = value
Next
Next
End Sub
Public Sub getImageEdges(rawImage As classImageProcessing)
'extracts edges from the given image
Dim x As Integer
Dim y As Integer
Dim value As Single
Dim scalex As Single
Dim scaley As Single
Dim xx As Integer
Dim yy As Integer
Dim p1 As Integer
Dim p2 As Integer
Dim avContrast As Double
scalex = rawImage.width / width
scaley = rawImage.height / height
currEdgeVector = 0
maxEdgeVectorIntensity = 0
avContrast = 0
For x = 1 To width - 1
For y = 1 To height - 1
edgeTraced(x, y) = False
xx = x * scalex
yy = y * scaley
If ((xx >= 1) And (yy >= 1)) Then
p1 = rawImage.getPoint(xx, yy)
p2 = rawImage.getPoint(xx - 1, yy)
value = Abs(p1 - p2)
p2 = rawImage.getPoint(xx, yy - 1)
value = value + Abs(p1 - p2)
value = value / (255 * 2)
avContrast = avContrast + value
'If (Abs(value - averageContrast) < EdgeThreshold) Then
If (value < EdgeThreshold) Then
value = 0
Else
value = 255 * value
End If
image(x, y) = value
End If
Next
Next
'calc average contast
avContrast = avContrast / (width * height)
averageContrast = avContrast
If (averageContrast < 0.01) Then
averageContrast = 0.01
End If
'calc threshold used for tracing along edges
TraceEdgesThresh = (averageContrast * 255) * 0.1
'Call diffuseEdges
'Call getEdges
Call traceEdges
End Sub
Public Sub getImageContours(rawImage As classImageProcessing)
'extracts edges from the given image
Dim x As Integer
Dim y As Integer
Dim value As Single
Dim scalex As Single
Dim scaley As Single
Dim xx As Integer
Dim yy As Integer
Dim p1 As Integer
Dim p2 As Integer
Dim value2 As Single
Dim max As Single
scalex = rawImage.width / width
scaley = rawImage.height / height
currEdgeVector = 0
maxEdgeVectorIntensity = 0
max = 1 - EdgeThreshold
For x = 1 To width - 1
For y = 1 To height - 1
edgeTraced(x, y) = False
xx = x * scalex
yy = y * scaley
If ((xx >= 1) And (yy >= 1)) Then
p1 = rawImage.getPoint(xx, yy)
p2 = rawImage.getPoint(xx - 1, yy)
value = Abs(p1 - p2)
p2 = rawImage.getPoint(xx, yy - 1)
value = value + Abs(p1 - p2)
value = value / (255 * 2)
value2 = value - EdgeThreshold
If (value2 < 0) Then
value = 0
Else
value = 255 - (255 * (value2 / max))
End If
image(x, y) = value
End If
Next
Next
End Sub
Public Sub show(canvas As PictureBox, Optional tx As Variant, Optional ty As Variant, Optional subImageWidth As Variant, Optional subImageHeight As Variant)
Dim x As Integer
Dim y As Integer
Dim screenX(2) As Single
Dim screenY(2) As Single
Dim value As Byte
Dim c As Long
Dim i As Integer
Dim showPoint As Boolean
If (processType <> 4) Then
canvas.FillStyle = 0
For x = 0 To width - 1
For y = 0 To height - 1
showPoint = True
If (IsMissing(tx)) Then
value = image(x, y)
Else
If (x >= tx) And (x < tx + subImageWidth) And (y >= ty) And (y < ty + subImageHeight) Then
value = image(x, y)
Else
value = 0
showPoint = False
End If
End If
If (showPoint) Then
Select Case processType
Case 1 'red
c = RGB(value, 0, 0)
Case 2 'green
c = RGB(0, value, 0)
Case 3 'blue
c = RGB(0, 0, value)
Case 4 'edges
value = 255 - value
c = RGB(value, value, value)
Case Else
c = RGB(value, value, value)
End Select
canvas.FillColor = c
screenX(0) = (x / width) * canvas.ScaleWidth
screenY(0) = (y / height) * canvas.ScaleHeight
screenX(1) = ((x + 1) / width) * canvas.ScaleWidth
screenY(1) = ((y + 1) / height) * canvas.ScaleHeight
canvas.Line (screenX(0), screenY(0))-(screenX(1), screenY(1)), c, B
End If
Next
Next
Else
'Call showEdges(canvas)
canvas.Cls
Call showEdgeTraces(canvas)
End If
End Sub
Public Sub showEdgeTraces(canvas As PictureBox)
Dim x As Integer
Dim y As Integer
Dim screenX(2) As Single
Dim screenY(2) As Single
Dim value As Byte
Dim c As Long
Dim i As Integer
'canvas.Cls
canvas.FillStyle = 0
For x = 0 To width - 1
For y = 0 To height - 1
If (edgeTraced(x, y) = True) Then
c = RGB(230, 230, 230)
canvas.FillColor = c
screenX(0) = (x / width) * canvas.ScaleWidth
screenY(0) = (y / height) * canvas.ScaleHeight
screenX(1) = ((x + 1) / width) * canvas.ScaleWidth
screenY(1) = ((y + 1) / height) * canvas.ScaleHeight
canvas.Line (screenX(0), screenY(0))-(screenX(1), screenY(1)), c, B
End If
Next
Next
Call showEdgeVector(canvas)
End Sub
Public Sub showEdgeVector(canvas As PictureBox)
Dim x1 As Integer
Dim y1 As Integer
Dim x2 As Integer
Dim y2 As Integer
Dim screenX(2) As Single
Dim screenY(2) As Single
Dim value As Byte
Dim c As Long
Dim i As Integer
Dim radius As Integer
'canvas.Cls
canvas.FillStyle = 0
canvas.DrawWidth = 1
radius = (canvas.ScaleWidth / width) / 2
For i = 0 To currEdgeVector - 1
x1 = EdgeVector(0, i)
y1 = EdgeVector(1, i)
x2 = EdgeVector(2, i)
y2 = EdgeVector(3, i)
'c = RGB((EdgeVector(4, i) / maxEdgeVectorIntensity) * 255, 0, 0)
c = RGB(i, 0, 0)
canvas.FillColor = c
screenX(0) = (x1 / width) * canvas.ScaleWidth
screenY(0) = (y1 / height) * canvas.ScaleHeight
screenX(1) = (x2 / width) * canvas.ScaleWidth
screenY(1) = (y2 / height) * canvas.ScaleHeight
If (i > 0) Then
canvas.Line -(screenX(0), screenY(0)), c
End If
canvas.Line (screenX(0), screenY(0))-(screenX(1), screenY(1)), c
'canvas.Circle (screenX(0), screenY(0)), radius, c
'canvas.Circle (screenX(1), screenY(1)), radius, c
Next
End Sub
Public Sub showEdges(canvas As PictureBox)
Dim x As Integer
Dim y As Integer
Dim screenX(2) As Single
Dim screenY(2) As Single
Dim edgeType As Byte
Dim c As Long
Dim i As Integer
canvas.Cls
canvas.FillStyle = 0
c = RGB(0, 0, 0)
For x = 0 To edgesWidth - 1
For y = 0 To edgesHeight - 1
screenX(0) = (x / edgesWidth) * canvas.ScaleWidth
screenY(0) = (y / edgesHeight) * canvas.ScaleHeight
screenX(1) = ((x + 1) / edgesWidth) * canvas.ScaleWidth
screenY(1) = ((y + 1) / edgesHeight) * canvas.ScaleHeight
edgeType = Edges(x, y)
Select Case edgeType
Case 1 'horizontal line
canvas.Line (screenX(0), screenY(0))-(screenX(1), screenY(0)), c
Case 2 'vertical line
canvas.Line (screenX(0), screenY(0))-(screenX(0), screenY(1)), c
Case 3 'diagonal /
canvas.Line (screenX(0), screenY(1))-(screenX(1), screenY(0)), c
Case 4 'diagonal \
canvas.Line (screenX(0), screenY(0))-(screenX(1), screenY(1)), c
Case 5 'cross
canvas.Line (screenX(0), screenY(0))-(screenX(1), screenY(0)), c
canvas.Line (screenX(0), screenY(0))-(screenX(0), screenY(1)), c
End Select
Next
Next
End Sub
Public Sub showEdgeHistogram(chart As Object)
'displays edge histogram using MS chart control
Dim i As Integer
Dim estr As String
chart.chartType = 7
chart.RowCount = NO_OF_EDGE_TYPES
chart.ColumnCount = 1
estr = ""
For i = 0 To chart.RowCount - 1
chart.Row = i + 1
chart.Data = EdgeHistogram(i)
estr = estr & EdgeHistogram(i) & ", "
Next
chart.Refresh
'MsgBox estr
End Sub
Public Sub showAngleHistogram(chart As Object)
'displays angle histogram using MS chart control
Dim i As Integer
Dim estr As String
chart.chartType = 7
chart.RowCount = 18
chart.ColumnCount = 1
estr = ""
For i = 0 To chart.RowCount - 1
chart.Row = i + 1
chart.Data = angleHistogram(i)
estr = estr & angleHistogram(i) & ", "
Next
chart.Refresh
'MsgBox estr
End Sub
Private Function getRGBvalue(RGBColour As Long, ColourIndex As Integer) As Byte
'returns either the red green or blue component of the given colour
Dim rgbsource As RGBthingy
Dim rgbdest As RGBpoint
rgbsource.value = RGBColour
Call CopyMemory(rgbdest, rgbsource, 3)
Select Case ColourIndex
Case 0 'red
getRGBvalue = rgbdest.red
Case 1 'green
getRGBvalue = rgbdest.Green
Case 2 'blue
getRGBvalue = rgbdest.Blue
End Select
End Function
Private Function getSpectrumValue(red As Byte, Green As Byte, Blue As Byte) As Double
'returns a value between 0 and 1 indicating a point in a continuous colour spectrum
Dim r As Single
Dim g As Single
Dim b As Single
r = red
g = Green
b = Blue
getSpectrumValue = ((r * r) + (g * g) + (b * b)) / 195075
End Function
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -