C# 指针遍历+for循环多线程
public unsafe static HObject PointXYZ_To_GrayColorHobject(ref PointXYZ[] PointCloudData, int ImgWidth, int ImgHeight, float ZMin, float ZMax,out HObject colorImg)
{
try
{
HObject ImgHobject = null;
colorImg = null;
if (PointCloudData == null) return ImgHobject;
//归一化后按 0-1/3 1/3-2/3 2/3-1 进行颜色映射
float cut1 = 1.0f / 3.0f;
float cut2 = 2.0f / 3.0f;
int numPoints = PointCloudData.Length;
float RangeZ = ZMax - ZMin;//点云数据范围
float coefficient = RangeZ > 0 ? 255 * 1.0f / RangeZ : 0;//灰度系数
//byte[] grayArr = new byte[numPoints];
IntPtr colorPtr = Marshal.AllocHGlobal(numPoints * 3 * sizeof(byte));//分配伪彩图RGB内存
IntPtr grayPtr = Marshal.AllocHGlobal(numPoints * sizeof(byte));//分配灰度图内存
int numProcessedPoints = ImgWidth - (ImgWidth % 4); // 可以处理的点的数量(4的倍数)
fixed (PointXYZ* pData = PointCloudData)
{
float* pHeight = (float*)pData;
byte* colorPtrTemp = (byte*)colorPtr;
byte* grayPtrTemp = (byte*)grayPtr;
Parallel.For(0, ImgHeight, Row =>
{
float* PRowIndexPointData = pHeight + Row * ImgWidth * 3;//点云指针
byte* pDest = colorPtrTemp + Row * 3 * ImgWidth;//伪彩图图像遍历指针
byte* pGrayDest = grayPtrTemp + Row * ImgWidth;//灰度图图像遍历指针
for (int Col = 0; Col < numProcessedPoints; Col += 4)
{
float height1 = PRowIndexPointData[2];
float height2 = PRowIndexPointData[5];
float height3 = PRowIndexPointData[8];
float height4 = PRowIndexPointData[11];
#region gray灰度映射
pGrayDest[0] = (byte)((height1 >= ZMin && height1 <= ZMax) ? (height1 - ZMin) * coefficient : 0);
pGrayDest[1] = (byte)((height2 >=

最低0.47元/天 解锁文章
661

被折叠的 条评论
为什么被折叠?



