07-31-2020 04:13 AM
In Visual studio, I use C# to convert VisionImage into Himage (halcon). I can use Genimage1 method to generate grayscale image, but if I want to use Genimage3 method to generate color image, RGB pointer is required. How can I get RGB value or pointer of color image from VisionImage? Or is there any way to convert a color VisionImage into a color Himage? Thank you!
Here is the code for black and white image conversion:
VisionImage vimage = new VisionImage();//Suppose vimage is a grayscale image
private HImage himage = null;
//
public HImage VImageToHImage(VisionImage vimage)
{
himage = new HImage();
PixelValue2D p2D;
p2D = vimage.ImageToArray();
Byte[] imagedata = null;
imagedata = ByteArrToByte(p2D.U8, vimage.Height, vimage.Width);
System.Runtime.InteropServices.GCHandle thObject = System.Runtime.InteropServices.GCHandle.Alloc(imagedata, System.Runtime.InteropServices.GCHandleType.Pinned);
IntPtr tpObject = thObject.AddrOfPinnedObject();
himage.GenImage1("byte", vimage.Width, vimage.Height, tpObject);
return himage;
}
public static byte[] ByteArrToByte(Byte[,] b, int nNum1, int nNum2)
{
List<byte> list = new List<byte>();
for (int i = 0; i < nNum1; i++)
{
for (int j = 0; j < nNum2; j++)
list.Add(b[i, j]);
}
byte[] newB = list.ToArray();
return newB;
}
08-20-2020 02:23 AM
Don't know whether this issue is still actual for you or not. Anyway here's the sample on interfacing OpenCV with HALCON: https://www.codenong.com/jsa639f1c32a95/ And here's the excerpt from it:
cv::Mat pImageRed, pImageGreen, pImageBlue;
std::vector<cv::Mat> sbgr(3);
cv::split(pImage, sbgr);
int length = pImage.rows * pImage.cols;
uchar *dataBlue = new uchar[length];
uchar *dataGreen = new uchar[length];
uchar *dataRed = new uchar[length];
int height = pImage.rows;
int width = pImage.cols;
for (int row = 0; row < height; row++)
{
uchar* ptr = pImage.ptr<uchar>(row);
for (int col = 0; col < width; col++)
{
dataBlue[row * width + col] = ptr[3 * col];
dataGreen[row * width + col] = ptr[3 * col + 1];
dataRed[row * width + col] = ptr[3 * col + 2];
}
}
GenImage3(&Hobj, "byte", width, height, (Hlong)(dataRed), (Hlong)(dataGreen), (Hlong)(dataBlue));
delete[] dataRed;
delete[] dataGreen;
delete[] dataBlue;
As you can see in the code, the input image is splitted into three arrays (R, G and B) and those arrays are fed into GenImage3 to generate HImage object. You could try to implement the similar thing in your C# code.