准备灰度图 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. Java&Selenium智能等待方法封装

    Java&Selenium智能等待方法封装 ExpectedConditions方法还有很多,自然也可以继续扩展很多 package util; import org.openqa.selen ...

  2. Java&Selenium数据驱动【DataProvider+TestNG+Mysql】

    Java&Selenium数据驱动[DataProvider+TestNG+Mysql] package testNGWithDataDriven; import java.io.IOExce ...

  3. Python+request 登录接口reponse的返回值token跨py文件的传递《二》

    主要使用场景: 一般我们在进行接口测试时,依赖登录接口后reponse中的某些返回值,因此需要将login接口单独写一个py文件,另外的py文件均可调用login的reponse返回值使用.共用登录接 ...

  4. 基于 Go 的可嵌入脚本语言 zygomys

    zygomys zygomys 是一种可嵌入的脚本语言. 它是一个具有面向对象风格的现代化 Lisp,提供了一个解释器和 REPL(Read-Eval-Print-Loop:也就是说,它带有一个命令行 ...

  5. [2019HDU多校第三场][HDU 6603][A. Azshara's deep sea]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6603 题目大意:给出一个凸包,凸包内有若干个圆,要求画尽可能多的对角线使得他们两两不在凸包内相交且不与 ...

  6. C语言学习系列(五)变量和常量

    一.常量 定义:在程序运行中,其值不能改变的量称为常量 分类:常量可以是任何的基本数据类型,比如整数常量.浮点常量.字符常量,或字符串字面值,也有枚举常量. 在 C 中,有两种简单的定义常量的方式: ...

  7. Vivado RAM使用

    RAM使用的几点说明: 1,RAM的读写位宽可以不同,举例:写的位宽为8(1Byte),读的位宽为1(1bit),那么读的地址就变成了写地址的8倍,即位宽增加3bit.

  8. java web项目为什么我们要放弃jsp?(转)

    前戏: 以前的项目大多数都是java程序猿又当爹又当妈,又搞前端(ajax/jquery/js/html/css等等),又搞后端(java/mysql/Oracle等等). 随着时代的发展,渐渐的许多 ...

  9. controller层直接通过server类调用mapper的通用方法

    自己写的方法没有,但是逆向生成的server类会有继承maybatis-plus的框架 与下图的配置有关

  10. Bzoj 2134: [国家集训队2011]单选错位(期望)

    2134: 单选错位 Time Limit: 10 Sec Memory Limit: 259 MB Description Input n很大,为了避免读入耗时太多,输入文件只有5个整数参数n, A ...