准备灰度图 grayTest.png,放置于Assets下StreamingAssets文件夹中。
 
在场景中添加RawImage用于显示最后的等值线图。
 
生成等值线的过程,使用Marching squares中Isolines方式。
 
首先将顶点数据与阈值比较,对顶点进行标记。根据四个顶点的标记情况,连接各个线段的中点,组成等值线。
 
测试中在texture2d对线段中点进行涂色以显示。
 
脚本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //生成等值线图
public class IsoLinesCreate : MonoBehaviour {
private Texture2D importImage;//输入灰度图
private Texture grayImage;//加载灰度图
private Texture2D outputImage;//输出等值线图
private bool bCreate=false;
[Tooltip("用作显示输出的等高线图")]
public RawImage showImage;
private int tGrayWidth = 0, tGrayHeight = 0;//灰度图的宽和高
void Start () {
StartCoroutine(loadImage("grayTest.png", (t) => grayImage = t));
} void Update () {
if (grayImage!=null)
{
if (bCreate==false)
{
importImage = (Texture2D)grayImage;
outputImage = (Texture2D)grayImage;
tGrayWidth = grayImage.width;
tGrayHeight = grayImage.height;
showImage.rectTransform.sizeDelta = new Vector2(tGrayWidth,tGrayHeight); //测试不同阈值不同颜色等值线绘制
trackIsoline(0.5f,Color.blue,2);
trackIsoline(0.6f, Color.green, 2);
trackIsoline(0.7f, Color.yellow, 2);
trackIsoline(0.8f, Color.red, 2); showImage.texture = outputImage;
bCreate = true;
}
}
} //加载图片
IEnumerator loadImage(string imagePath, System.Action<Texture> action)
{
WWW www = new WWW("file://" + Application.streamingAssetsPath + "/" + imagePath);
yield return www;
if (www.error == null)
{
action(www.texture);
}
} /// <summary>
/// 等值线追踪
/// </summary>
/// <param name="threshold">灰度阈值</param>
/// <param name="colorP">颜色</param>
/// <param name="step">步长 为偶数</param>
private void trackIsoline(float threshold,Color colorP,int step)
{
for (int i = 0; i < tGrayWidth-step; i+=step)
{
for (int j = 0; j < tGrayHeight-step; j+=step)
{
//标记顶点状态
int tempCount = 0;//0000
if (importImage.GetPixel(i,j).grayscale>threshold)
{
tempCount += 1;//0001
}
if (importImage.GetPixel(i+step, j).grayscale > threshold)
{
tempCount += 8;//1000
}
if (importImage.GetPixel(i+step, j+step).grayscale > threshold)
{
tempCount += 4;//0100
}
if (importImage.GetPixel(i, j+step).grayscale > threshold)
{
tempCount += 2;//0010
}
int t = step / 2;
//根据顶点标记情况处理中点连线(此处对中点进行上色)
switch (tempCount)
{
case 0:
case 15:
break;
case 1:
case 14:
outputImage.SetPixel(i+t,j,colorP);
outputImage.SetPixel(i,j+t,colorP);
break;
case 2:
case 13:
outputImage.SetPixel(i , j+t, colorP);
outputImage.SetPixel(i+t, j + 2*t, colorP);
break;
case 3:
case 12:
outputImage.SetPixel(i+t, j, colorP);
outputImage.SetPixel(i + t, j + 2 * t, colorP);
break;
case 4:
case 11:
outputImage.SetPixel(i +2* t, j+t, colorP);
outputImage.SetPixel(i + t, j + 2 * t, colorP);
break;
case 5:
outputImage.SetPixel(i + t, j, colorP);
outputImage.SetPixel(i + 2*t, j + t, colorP); outputImage.SetPixel(i , j + t, colorP);
outputImage.SetPixel(i + t, j + 2 * t, colorP);
break;
case 6:
case 9:
outputImage.SetPixel(i, j + t, colorP);
outputImage.SetPixel(i +2* t, j + t, colorP);
break;
case 7:
case 8:
outputImage.SetPixel(i+t, j, colorP);
outputImage.SetPixel(i + 2 * t, j + t, colorP);
break;
case 10:
outputImage.SetPixel(i + t, j, colorP);
outputImage.SetPixel(i, j + t, colorP); outputImage.SetPixel(i + 2*t, j+t, colorP);
outputImage.SetPixel(i + t, j + 2*t, colorP);
break;
}
}
}
outputImage.Apply();
}
}

本文链接 https://www.cnblogs.com/gucheng/p/10107696.html

 
 

unity读取灰度图生成等值线图的更多相关文章

  1. unity读取灰度图生成三维地形mesh

    准备灰度图 IGray.png及草地贴图 IGrass.jpg ,放入Assets下StreamingAssets文件夹中.     创建空材质,用作参数传入脚本.   脚本如下,挂载并传入材质球即可 ...

  2. unity 读取灰度图生成按高程分层设色地形模型

    准备灰度图 1.高程按比例对应hue色相(hsv)生成mesh效果 o.color = float4(hsv2rgb(float3(v.vertex.y/100.0, 0.5, 0.75)), 1.0 ...

  3. unity 读取灰度图生成三维地形并贴图卫星影像

    从 https://earthexplorer.usgs.gov/ 下载高程数据 从谷歌地球上保存对应地区卫星图像 从灰度图创建地形模型,并将卫星影像作为贴图 using System.Collect ...

  4. opengl读取灰度图生成三维地形并添加光照

    转自:https://www.cnblogs.com/gucheng/p/10152889.html 准备第三方库 glew.freeglut.glm.opencv 准备一张灰度图 最终效果 代码如下 ...

  5. ue4读取灰度图生成三维地形mesh

    转自:https://www.cnblogs.com/gucheng/p/10116857.html 新建ue c++工程. 在Build.cs中添加"ProceduralMeshCompo ...

  6. opengl读取灰度图生成三维地形

    准备第三方库 glew.freeglut.glm.opencv 准备灰度图片和草地贴图 最终效果 代码包括主程序源文件mainApp.cpp.顶点着色器shader.vs.片元着色器shader.fs ...

  7. blender导入灰度图生成地形模型

    安装软件 在此处下载blender并安装. 添加平面 1.打开blender,右键删除初始的立方体. 2.shift+a选择平面添加进场景: 3.按下s键鼠标拖动调节平面大小确定后按下鼠标左键: 4. ...

  8. unity 读取excel表 生成asset资源文件

    做unity 项目也有一段时间了,从unity项目开发和学习中也遇到了很多坑,并且也从中学习到了很多曾经未接触的领域.项目中的很多功能模块,从今天开始把自己的思路和代码奉上给学渣们作为一份学习的资料. ...

  9. c语言实现灰度图转换为二值图

    将上篇得到的灰度图转换为二值图,读取像素数据,低于某一值置0,否则设置为255,为得到更好的效果不同图片应采用不同的值 /* 2015年6月2日11:16:22 灰度图转换为二值图 blog:http ...

随机推荐

  1. 关键字final 修饰类、方法、属性、参数类型

    笔记: /** 关键字final(最终的) 标记的类不能被继承, 提高安全性,提高程序的可读性 * 1.final 修饰类,这个类就不能被继承: 如:String类.StringBuffer类.Sys ...

  2. JAVA遇见HTML——JSP篇(JSP状态管理)

    案例:Cookie在登录中的应用 URL编码与解码的工具类解决中文乱码的问题,这个工具类在java.net.*包里 编码:URLEncoder.encode(String s,String enc)/ ...

  3. 使用python批量造测试数据

    # -*- coding:utf-8 -*- import json import os import time class Virtual_Data: def __init__(self): sel ...

  4. css选择器学习(二)属性选择器

    属性选择器 /*******************************************css2中的属性选择器*************************************** ...

  5. [WebMethod]参数介绍

    一.WebService的调试 net 2.0新建webservice为了安全考虑,默认关闭了Post和Get方法 .让其打开,可在Web.config文件的<system.web>下增加 ...

  6. IAR添加debug和release选项

    在IAR的Workspace窗口顶部的下拉菜单中有两个选项,Debug和Release. 名字和数量可以在菜单栏的Project-->Edit Configuration中增删修改 每个选项都对 ...

  7. hibernate配置和映射文件

    映射文件 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping ...

  8. 004_linuxC++之_函数的重载

    (一)源码下载 (一) 函数的重载:同一个命名函数,通过传入参数的不同,调用不一样的函数 上面程序的运行结果: (二)函数只能通过参数的不一样重载函数,不能通过返回参数的不一样重载函数 运行结果报错 ...

  9. [51Nod] 配对

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1737 求出树的重心,跑spfa #include <iostre ...

  10. error while loading shared libraries: libmysqlclient.so.20 问题小结

    问题:安装完成sysbench之后,查看sysbench版本号时出现下面问题.这种报错很常见, [root@zero01 sysbench]# /usr/local/sysbench/bin/sysb ...