Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
After converting code from c# to vb.net, pointers and unsafe code there's a problem, and due to the application i need to use them, so i've read about creating an assembuly and referencing it in a vb.net application. i've got so far but im not sure how to go about it now. Pointer (Of Byte) Problem

Public Function Recognize(image As UnmanagedImage, rect As Rectangle, ByRef confidence As Single) As Byte(,)
Dim glyphStartX As Integer = rect.Left
Dim glyphStartY As Integer = rect.Top
Dim glyphWidth As Integer = rect.Width
Dim glyphHeight As Integer = rect.Height
Dim cellWidth As Integer = glyphWidth \ glyphSize
Dim cellHeight As Integer = glyphHeight \ glyphSize
Dim cellOffsetX As Integer = CInt(cellWidth * 0.2)
Dim cellOffsetY As Integer = CInt(cellHeight * 0.2)
Dim cellScanX As Integer = CInt(cellWidth * 0.6)
Dim cellScanY As Integer = CInt(cellHeight * 0.6)
Dim cellScanArea As Integer = cellScanX * cellScanY
Dim cellIntensity As Integer(,) = New Integer(glyphSize - 1, glyphSize - 1) {}
Dim stride As Integer = image.Stride
Dim srcBase As Pointer(Of Byte) = CType(image.ImageData.ToPointer(), Pointer(Of Byte)) + (glyphStartY + cellOffsetY) * stride + glyphStartX + cellOffsetX
Dim srcLine As Pointer(Of Byte)
Dim src As Pointer(Of Byte)
For gi As Integer = 0 To glyphSize - 1
srcLine = srcBase + cellHeight * gi * stride
For y As Integer = 0 To cellScanY - 1
For gj As Integer = 0 To glyphSize - 1
src = srcLine + cellWidth * gj
Dim x As Integer = 0
While x < cellScanX
cellIntensity(gi, gj) += src.Target
x += 1
src += 1
End While
Next
srcLine += stride
Next
Next

' calculate value of each glyph's cell and set
' glyphs' c> 0.5F), 1, 0))
If conf < confidence Then
confidence = conf
End If
Next
Next
Return glyphValues
End Function

So i created a c# project and placed the code original c# part of the code within it namespace CTCAM:

public interface interfaceCTCAM
{
int Recognize(UnmanagedImage image, Rectangle rect, out float confidence);

}
public class Class1
{

public byte[,] Recognize(UnmanagedImage image, Rectangle rect, out float confidence)
{

int glyphSize = 5;
int glyphStartX = rect.Left;
int glyphStartY = rect.Top;
int glyphWidth = rect.Width;
int glyphHeight = rect.Height;
int cellWidth = glyphWidth / glyphSize;
int cellHeight = glyphHeight / glyphSize;
int cellOffsetX = (int)(cellWidth * 0.2);
int cellOffsetY = (int)(cellHeight * 0.2);
int cellScanX = (int)(cellWidth * 0.6);
int cellScanY = (int)(cellHeight * 0.6);
int cellScanArea = cellScanX * cellScanY;
int[,] cellIntensity = new int[glyphSize, glyphSize];
unsafe
{
int stride = image.Stride;
byte* srcBase = (byte*)image.ImageData.ToPointer() +
(glyphStartY + cellOffsetY) * stride +
glyphStartX + cellOffsetX;
byte* srcLine;
byte* src;
for (int gi = 0; gi < glyphSize; gi++)
{
srcLine = srcBase + cellHeight * gi * stride;
for (int y = 0; y < cellScanY; y++)
{
for (int gj = 0; gj < glyphSize; gj++)
{
src = srcLine + cellWidth * gj;
for (int x = 0; x < cellScanX; x++, src++)
{
cellIntensity[gi, gj] += *src;
}
}
srcLine += stride;
}
}
}

// calculate value of each glyph's cell and set
// glyphs' confidence to minim value of cell's confidence
byte[,] glyphValues = new byte[glyphSize, glyphSize];
confidence = 1f;
for (int gi = 0; gi < glyphSize; gi++)
{
for (int gj = 0; gj < glyphSize; gj++)
{
float fullness = (float)
(cellIntensity[gi, gj] / 255) / cellScanArea;
float conf = (float)System.Math.Abs(fullness - 0.5) + 0.5f;
glyphValues[gi, gj] = (byte)((fullness > 0.5f) ? 1 : 0);
if (conf < confidence)
confidence = conf;
}
}
return glyphValues;
}

}


Once i done that i deleted the "recognized" function from the vb code and imported the libary created "Imports CTCAM.Class1".

So in the VB application, Later in my code and error is showing, and this is where im not sure where to go now.

Dim glyphValues As Byte(,) = Recognize(glyphImage, New Rectangle(0, 0, glyphImage.Width, glyphImage.Height), confidence)

The word "Recognize" is highlighted with the following error information: "Reference to a non-shared member requires an object reference."

Any help would be great.

Many Thanks,
Pete
Posted
Updated 23-Sep-14 22:04pm
v3
Comments
Bernhard Hiller 24-Sep-14 9:42am    
By the way, the signatures of the Recognize function differ between the interface and the implementation - the return value was changed to byte[,] from int!

1 solution

"Shared" in VB means you can just call the function without having to have an instance of the class, which is what you are trying to do by just calling Recognize... So, the error is telling you that since Recognize is NOT shared, so you can't call it that way.

You need to declare an instance of the class Recognize is in first.

Something like:

VB
Dim cls AS New Class1
cls.Recognize...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900