遇到从C++传出结构体的,结构体里面套了两层结构体的问题,c++中的结构体定义为

struct DefectPoint
{int X;int Y;
};
struct DefectResult
{double Area;int Count;DefectPoint *Points;
};struct DefectResults
{DefectResult *DEFECTRESULT;
};

接口函数定义为

extern "C" _declspec(dllexport) void  DetectTexturingWhite(unsigned char *src, int height, int width,int Stride, int white_area, int texturing_area, int local_area, DefectResults* data);void  DetectTexturingWhite(unsigned char *src, int height, int width,int Stride, int white_area, int texturing_area, int local_area, DefectResults* data)
{//1、读入图像unsigned char *imgIn = new unsigned char[height*width];for (int i = 0; i <width; i++){for (int j = 0; j < height; j++){imgIn[i*width + j] = src[i*Stride + j];//stride图像矩阵行宽}}Mat img(height, width, CV_8UC1, imgIn);//2、图像预处理//Mat img_adapt(height,width,CV_8UC1,Scalar(0));//Mat img_thr(height, width, CV_8UC1, Scalar(0));Mat img_auto(height, width, CV_8UC1, Scalar(0));//int block_size = 3;//int Constant = 0;//cv::adaptiveThreshold(img, img_adapt, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, block_size, Constant);//int threthold_auto = threshold(img, img_auto, 0, 255, CV_THRESH_OTSU);int thr = mean(img)[0];if (thr<225){thr = thr + 30;}cv::threshold(img, img_auto, thr, 255, cv::THRESH_BINARY);//threshold(img, img_thr, 150, 255, cv::THRESH_TOZERO);Mat element = getStructuringElement(MORPH_RECT,Size(10,10));dilate(img_auto, img_auto,element);erode(img_auto, img_auto, element);//3、提取blobvector<vector<Point>> contours;vector<Vec4i> hierachy;findContours(img_auto, contours, hierachy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);sort(contours.begin(), contours.end(), ContoursSortFun);double area_begin = contourArea(contours[0], false);Mat img_display(width,height,CV_8UC1,Scalar(0));drawContours(img_display, contours, -1, Scalar::all(255));//4、判断是制绒还是小白点OUTDEFECT out_defect;int small_white_dot = 0;out_defect.texturing = (TEXTURING *)malloc(sizeof(TEXTURING)*100);                  //结构体指针初始化out_defect.whitedot = (WHITEDOT *)malloc(sizeof(WHITEDOT)*10000);                   //结构体指针初始化out_defect.texturing_num = 0;out_defect.white_num = 0;WHITEDOT *s_dot = new WHITEDOT[10000];int big_white_dot = 0;WHITEDOT *b_dot = new WHITEDOT[10000];int count_blob = contours.size();double current_area = 0;Point2f center;float radius;TEXTURING textur_current;vector<Point> small_point_center;int area_first = 0;DefectResult* p_first=NULL;for (int i = 0; i < count_blob;i++){current_area = contourArea(contours[i], false);minEnclosingCircle(contours[i], center, radius);if (current_area>texturing_area)                                         //制绒{textur_current.center = center;textur_current.area = current_area;out_defect.texturing_num++;out_defect.texturing[out_defect.texturing_num - 1] = textur_current;circle(img,center,radius,Scalar(255));int current_num = out_defect.texturing_num;int delta = 403 * (out_defect.texturing_num-1);//void *p1 = data;data[current_num - 1].DEFECTRESULT = new DefectResult();//void *p2 = (DefectResult*)p1 + (sizeof(DefectPoint)* 200 + sizeof(int)+sizeof(double))*(out_defect.texturing_num - 1) + 1;data[current_num - 1].DEFECTRESULT->Points = new DefectPoint[200];data[current_num - 1].DEFECTRESULT->Area = current_area;data[current_num - 1].DEFECTRESULT->Count = out_defect.texturing_num;int a = sizeof(data[current_num - 1].DEFECTRESULT->Points);//void *p = &(data[out_defect.texturing_num - 1].DEFECTRESULT->Count) + 1;void *p_Area = data + delta;//void *buf = reinterpret_cast<void*> (p);(data[current_num - 1].DEFECTRESULT) = (DefectResult*)(p_Area);DefectResult *p_Area1 = data[current_num - 1].DEFECTRESULT;DefectResult *p_Area2 = p_Area1;int a1 = sizeof(DefectResult);p_Area2->Area = current_area;p_Area2->Count = out_defect.texturing_num;void* p_point = data + delta +3;(p_Area2->Points) = (DefectPoint*)(p_point);DefectPoint *pp = p_Area2->Points;DefectPoint *p_temp =pp;int contours_size = contours[i].size();int contours_size1 = contours_size;if (contours_size > 200) contours_size1 = 200;for (int j = 0; j < contours_size1; j++){p_temp[j].X = contours[i][j].x;p_temp[j].Y = contours[i][j].y;}if (contours_size < 200){for (int j = contours_size; j < 200; j++){p_temp[j].X = contours[i][j].x;p_temp[j].Y = contours[i][j].y;}}if (current_num==1){area_first = current_area;p_first = p_Area2;p_first->Count = 1;}else{p_first->Area = current_area;}}else if (current_area<white_area)                                          //小白点{small_white_dot++;s_dot[small_white_dot - 1].center = center;s_dot[small_white_dot - 1].area = current_area;small_point_center.push_back(center);}else if (current_area>white_area && current_area<=texturing_area)          //大白点{ big_white_dot++;b_dot[big_white_dot - 1].center = center;b_dot[big_white_dot - 1].area = current_area;}}//5、大白点单独分析    if (big_white_dot <= 5 && big_white_dot>0){for (int i = 0; i < big_white_dot;i++){out_defect.whitedot[i] = b_dot[i];}}else{big_white_dot = 0;}//6、小白点单独分析if (small_white_dot <= 15 && small_white_dot>0){if (IsWhiteDot(small_point_center, 15, 6, local_area) == true){for (int i = 0; i < small_white_dot;i++){out_defect.whitedot[big_white_dot + i].area = b_dot[i].area;out_defect.whitedot[big_white_dot + i].center = b_dot[i].center;}}}delete[] b_dot;delete[] s_dot;
}

传入一个结构体的指针data,将data中的嵌套结构体实例化,分别利用空指针,对C#外部开辟的内存赋值

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace BlackSilicon
{public partial class BlackSilicon : Form{static string currentPath = "";static int texturing_area;static int white_area;static int local_area;public BlackSilicon(){InitializeComponent();texturing_area = 8680;            //判断为制绒的最小面积white_area = 3472;                //判断为小白点的临界面积local_area = 17361;               //局部面积}private void button1_Click(object sender, EventArgs e){FolderBrowserDialog filedia = new FolderBrowserDialog();filedia.SelectedPath = "F:\\Project\\001\\白色同轴+8KRGB5.26\\制绒不全\\img_split\\image_cut_src";if (filedia.ShowDialog() == DialogResult.OK){string path = filedia.SelectedPath;System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);System.IO.FileInfo[] fi = dir.GetFiles();string[] names=new string[fi.Count()];listBox1.Items.Clear();for(int i=0;i<fi.Count();i++){names[i]=fi[i].FullName;listBox1.Items.Add(names[i]);}}}private void listBox1_SelectedIndexChanged(object sender, EventArgs e){currentPath = (string)listBox1.SelectedItem;if (pictureBox1.Image != null){pictureBox1.Image.Dispose();}Image img0 = Image.FromFile(currentPath);pictureBox1.Image = img0;}[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]public struct DefectPoint{public int X;public int Y;}[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]public struct DefectResult{[MarshalAs(UnmanagedType.R8)]public double Area;[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 200)]public DefectPoint[] X;}[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]public struct DefectResults{public int Count;[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 8)]public DefectResult[] STRUCT;}//[DllImport("DetectTexturingWhite.dll", CallingConvention = CallingConvention.Cdecl)]//public static extern void DetectTexturingWhite(IntPtr src, int height, int width, int Stride, //int white_area,int local_area, int texturing_area,IntPtr result); private void button3_Click(object sender, EventArgs e){if (currentPath != ""){Bitmap img0 = new Bitmap(currentPath);int height = img0.Height;int width = img0.Width;BitmapData BmData = img0.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, img0.PixelFormat);IntPtr src = BmData.Scan0;DefectResults result = new DefectResults();IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(DefectResults)));Marshal.StructureToPtr(result, ptr, true);Helper.DetectTexturingWhite(src, height, width, BmData.Stride, white_area, local_area, texturing_area, ptr);//接口调用DefectResults obj = (DefectResults)Marshal.PtrToStructure(ptr, typeof(DefectResults));img0.UnlockBits(BmData);img0.Dispose();}}[DllImport("ScourScare.dll", CallingConvention = CallingConvention.Cdecl)]public static extern void ScourScare(IntPtr src,int height,int width,int Stride);private void button4_Click(object sender, EventArgs e){if (currentPath != ""){Bitmap img0 = new Bitmap(currentPath);int height = img0.Height;int width = img0.Width;BitmapData BmData = img0.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, img0.PixelFormat);IntPtr ptr = BmData.Scan0;ScourScare(ptr, height, width, BmData.Stride);img0.UnlockBits(BmData);img0.Dispose();}}}public class Helper{[DllImport("DetectTexturingWhite.dll", CallingConvention = CallingConvention.Cdecl)]public static extern void DetectTexturingWhite(IntPtr src, int height, int width, int Stride,int white_area, int local_area, int texturing_area, IntPtr result); }
}

C++和C#结构体转换的问题相关推荐

  1. C#通过Kernel32.dll动态调用C++生成dll相关接口(结构体转换)相关问题整理

    公司内部服务器端全部采用C++开发,自然而然暴露给客户的肯定是C++提供的API.这里不介绍为何通讯间不采用xml或和其他方式进行传输,因为这是个个别案例. 公司C++高手一大堆,我只是个C++新手, ...

  2. 技巧:Go 结构体如何转换成 map[string]interface{}

    本文介绍了Go语言中将结构体转成map[string]interface{}时你需要了解的"坑",也有你需要知道的若干方法. 我们在Go语言中通常使用结构体来保存我们的数据,例如要 ...

  3. JNI调用c++函数,该函数的参数是结构体(——对象的传递)

    第三方C++函数接口为 int api_get_logfile(Struct fileinfo tfile),参数是个结构体,且套了另一个结构体: struct fileinfo{  char *fu ...

  4. matlab s函数_matlab结构体 rmfield,arrayfun,structfun,struct2cell,cell2struct

    觉得有用的话,请点击右下角 推荐给更多小伙伴 neoken_xuAsurada2015Evacloud 本文为matlab自学笔记的一部分,之所以学习matlab是因为其真的是人工智能无论是神经网络还 ...

  5. linux socket 结构定义 send,Linux下Socket编程中用send发送结构体

    Linux网络通信 Linux下多客户端聊天软件 最近在开发一个Linux下的聊天软件,好久没有做C语言的开发了,感觉到很多东西已经生疏了,这下又碰到用Socket传递结构体的问题,google了一下 ...

  6. 解决go数据表查询结构体对应字段null问题(sqlx converting NULL to string is unsupported)

    方法一:将结构体对应字段类型设为指针类型,一劳永逸,不用担心json序列化与反序列化问题 // User 用户结构体(对应mysql表) type User struct {Id int `db:&q ...

  7. c语言send发送结构体,Socket编程中用send发送结构体

    Socket编程中用send发送结构体 原创 2010年04月28日 19:17:00 标签:socket /编程 /struct /google /string /input 11868 最近在开发 ...

  8. GCC源码分析(十四) — rtx结构体,指令与栈分配

    版权声明:本文为CSDN博主「ashimida@」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明. 原文链接:https://blog.csdn.net/lidan1 ...

  9. 通过response.body()返回的json报文,直接生成对应结构体,实现数据绑定

    作者:非妃是公主 专栏:<Golang> 博客地址:https://blog.csdn.net/myf_666 个性签:顺境不惰,逆境不馁,以心制境,万事可成.--曾国藩 文章目录 序 一 ...

  10. Linux下Socket编程中用send发送结构体

    转自:http://tech.ddvip.com/2008-10/122543769687623.html Linux网络通信 Linux下多客户端聊天软件 Linux程序设计 Linux socke ...

最新文章

  1. Ubuntu 安装 IPython、jupyter notebook
  2. AAAI2021论文:一个高性能3-D目标两步检测法Voxel R-CNN
  3. [Qt教程] 第32篇 网络(二)HTTP
  4. extjs window js引入问题
  5. rtmp官方协议详解
  6. .Net Core2.0下使用Dapper遇到的问题
  7. 使用JDBC连接数据库时,SQL语句中提示:Unable to resolve table...(原因:数据源未配置)
  8. 用PL/SQL Develpoer工具完成导入和导出
  9. 信息学奥赛一本通(1019:浮点数向零舍入)
  10. java获取当月1号 的时间chuo_java获取时间戳的方法
  11. 使用cmake和vs2019进行编译libtorch过程
  12. asp.net mvc5+Echarts3.0+AspNet.SignalR2.0 实时监控cpu占用率推送
  13. 服务器虚拟机装nas,nas虚拟主机(nas为什么要装虚拟机)
  14. 电脑网络连接里多了个计算机,怎么样把几台电脑连接在同一个局域网
  15. python爬虫第7章(1)
  16. 制造业数字孪生四大典型应用展示
  17. word段落间距调整:格式刷/取消对齐网格/分节符/擦除格式都无效的应对方法
  18. 在Python里,用股票案例讲描述性统计分析方法(内容来自我的书)
  19. 百度云网盘资源高速下载免登录网页版教程分享
  20. python魔方方法__call__

热门文章

  1. 定积分(Definite Integral)
  2. Unsupervised Super Resolution Reconstruction of Traffic Surveillance Vehicle Images
  3. 审稿较快的2区计算机sci期刊,哪些SCI期刊的审稿速度快
  4. .native.prevent是什么意思?
  5. SQL:取 分组后 的 按时间倒序 的前5条数据
  6. 自定义video的controls
  7. [SageMath] 关于SageMath本地环境的搭建与基本使用
  8. Prolog编程学习(一)
  9. 各种串口助手工具分享
  10. Ubuntu18.04 iso文件下载地址