准備灰度圖 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
