C#环境下通过Dll传递数据给C++处理

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);
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值