HImage转Mat:C++
#include <iostream>
#include <opencv2/opencv.hpp>
#include <HalconCpp.h>
using namespace std;
using namespace cv;
using namespace HalconCpp;
Mat HImageToMat(HObject& imgHalcon);
extern "C" __declspec(dllexport)void Writeimage(float* data, int Width, int Height)
{
HObject image;
HTuple channels;
GenImage1(&image, "byte", Width, Height, (Hlong)data);
WriteImage(image, "jpg", 0, "Halcon.jpg");
Mat img = HImageToMat(image);
imwrite("Opencv.jpg", img);
}
Mat HImageToMat(HObject& imgHalcon)
{
HTuple channels;
HString cType;
cv::Mat Image;
ConvertImageType(imgHalcon, &imgHalcon, "byte");
CountChannels(imgHalcon, &channels);
Hlong width = 0;
Hlong height = 0;
if (channels[0].I() == 1)
{
HImage hImg(imgHalcon);
void* ptr = hImg.GetImagePointer1(&cType, &width, &height);
int W = width;
int H = height;
Image.create(H, W, CV_8UC1);
unsigned char* pdata = static_cast<unsigned char*>(ptr);
memcpy(Image.data, pdata, W * H);
}
else if (channels[0].I() == 3)
{
void* Rptr;
void* Gptr;
void* Bptr;
HImage hImg(imgHalcon);
hImg.GetImagePointer3(&Rptr, &Gptr, &Bptr, &cType, &width, &height);
int W = width;
int H = height;
Image.create(H, W, CV_8UC3);
vector<cv::Mat> VecM(3);
VecM[0].create(H, W, CV_8UC1);
VecM[1].create(H, W, CV_8UC1);
VecM[2].create(H, W, CV_8UC1);
unsigned char* R = (unsigned char*)Rptr;
unsigned char* G = (unsigned char*)Gptr;
unsigned char* B = (unsigned char*)Bptr;
memcpy(VecM[2].data, R, W * H);
memcpy(VecM[1].data, G, W * H);
memcpy(VecM[0].data, B, W * H);
merge(VecM, Image);
}
return Image;
}
HImage转Mat:C#
using System;
using System.Runtime.InteropServices;
using HalconDotNet;
namespace Demo
{
class Test
{
[DllImport("Halcondll.dll", EntryPoint = "Writeimage", CallingConvention = CallingConvention.Cdecl)]
public extern static void Writeimage(IntPtr data, int h, int w);
static public void Write(IntPtr data, int w, int h)
{
Writeimage(data, w, h);
}
}
class Program
{
static void Main(string[] args)
{
HOperatorSet.ReadImage(out HObject image, "Demo.jpg");
HOperatorSet.GetImagePointer1(image, out HTuple pointer, out HTuple type, out HTuple width, out HTuple height);
Test.Write(pointer.IP, width.I, height.I);
}
}
}
Bitmap转Mat:C++
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
extern "C" __declspec(dllexport) void WriteImage(unsigned char* data, int height, int width, int stride)
{
Mat img;
if (stride / width == 3) img = Mat(height, width, CV_8UC3, data, 0);
if (stride / width == 1) img = Mat(height, width, CV_8UC1, data, 0);
if (stride / width == 4) img = Mat(height, width, CV_8UC4, data, 0);
imwrite("Result.jpg", img);
}
Bitmap转Mat:C#
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
namespace Demo
{
class Test
{
[DllImport("Createdll.dll", EntryPoint = "WriteImage", CallingConvention = CallingConvention.Cdecl)]
public extern static void WriteImage(IntPtr byte1, int width, int height, int stride);
static public void Write(Bitmap bmp)
{
BitmapData bmpdata = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
int length = bmp.Width * bmp.Height * (bmpdata.Stride / bmp.Width);
byte[] buffer = new byte[length];
Marshal.Copy(bmpdata.Scan0, buffer, 0, length);
bmp.UnlockBits(bmpdata);
WriteImage(bmpdata.Scan0, bmp.Height, bmp.Width, bmpdata.Stride);
}
}
class Program
{
static void Main(string[] args)
{
Bitmap bmp = new Bitmap("Demo.jpg");
Test.Write(bmp);
}
}
}