/// <summary> /// 查找像素点 /// </summary> private void OnFindPixel(marginPixel datas_) { int index = 0; marginPixel data_ = null; while (true) { if (data_ == null) { data_ = datas_; } //marginPixel findData = findData = usePixe.Find((marginPixel user) => (user.x == data_.x && user.y == (data_.y + 1) || (user.x == (data_.x + 1) && user.y == (data_.y + 1) || user.x == (data_.x + 1) && user.y == data_.y || user.x == (data_.x + 1) && user.y == (data_.y - 1) || user.x == data_.x && user.y == (data_.y - 1) || user.x == (data_.x - 1) && user.y == (data_.y - 1) || user.x == (data_.x - 1) && user.y == data_.y || user.x == (data_.x - 1) && user.y == (data_.y + 1)))); marginPixel findData = null; if (findData == null) { findData = usePixe.Find((marginPixel user) => user.x == data_.x && user.y == (data_.y + 1)); } if (findData == null) { findData = usePixe.Find((marginPixel user) => user.x == (data_.x + 1) && user.y == (data_.y + 1)); } if (findData == null) { findData = usePixe.Find((marginPixel user) => user.x == (data_.x + 1) && user.y == data_.y); } if (findData == null) { findData = usePixe.Find((marginPixel user) => user.x == (data_.x + 1) && user.y == (data_.y - 1)); } //----------------------- if (findData == null) { findData = usePixe.Find((marginPixel user) => user.x == data_.x && user.y == (data_.y - 1)); } if (findData == null) { findData = usePixe.Find((marginPixel user) => user.x == (data_.x - 1) && user.y == (data_.y - 1)); } if (findData == null) { findData = usePixe.Find((marginPixel user) => user.x == (data_.x - 1) && user.y == data_.y); } if (findData == null) { findData = usePixe.Find((marginPixel user) => user.x == (data_.x - 1) && user.y == (data_.y + 1)); } // 查找八个方向完成 if (findData != null && index < usePixe.Count) { index++; data_ = findData; orderPixe.Add(findData); } else { break; } } }
/// <summary> /// 查找二值化后图像像素边缘(测试) /// </summary> private void OnGetPixelMargin(Texture2D tt_) { Texture2D copyText = new Texture2D(tt_.width, tt_.height); copyText.SetPixels(tt_.GetPixels());//提取text图片的颜色,赋给copyText copyText.Apply(); List <marginPixel> marginList = new List <marginPixel>(); for (int y = 0; y < copyText.height; ++y) { for (int x = 0; x < copyText.width; ++x) { Color curColor = tt_.GetPixel(x, y); Color upColor = new Color(0, 0, 0, 0); Color leftColor = new Color(0, 0, 0, 0); Color rightColor = new Color(0, 0, 0, 0); Color DownColor = new Color(0, 0, 0, 0); Color leftDownColor = new Color(0, 0, 0, 0); Color leftUpColor = new Color(0, 0, 0, 0); Color rightDownColor = new Color(0, 0, 0, 0); Color rightUpColor = new Color(0, 0, 0, 0); if (y < copyText.height) { upColor = copyText.GetPixel(x, y + 1); } if (x > 0) { leftColor = copyText.GetPixel(x - 1, y); } if (x < copyText.width) { rightColor = copyText.GetPixel(x + 1, y); } if (y > 0) { DownColor = copyText.GetPixel(x, y - 1); } if (x > 0 && y > 0) { leftDownColor = copyText.GetPixel(x - 1, y - 1); } if (x > 0 && y < copyText.height) { leftUpColor = copyText.GetPixel(x - 1, y + 1); } if (x < copyText.width && y > 0) { rightDownColor = copyText.GetPixel(x + 1, y - 1); } if (x < copyText.width && y < copyText.height) { rightUpColor = copyText.GetPixel(x + 1, y + 1); } if (curColor.a == 0) { bool ximian = leftDownColor.a != 0 || leftUpColor.a != 0 || rightDownColor.a != 0 || rightUpColor.a != 0; bool zheng = upColor.a != 0 || leftColor.a != 0 || rightColor.a != 0 || DownColor.a != 0; if (zheng) { marginList.Add(new marginPixel(x, y, Color.red)); } } else { marginList.Add(new marginPixel(x, y, new Color(1, 1, 1, 0))); } } } for (int i = 0; i < marginList.Count; ++i) { marginPixel b = marginList[i]; copyText.SetPixel(b.x, b.y, b.color); } copyText.Apply(); img.texture = copyText; //OnCreateModel_1(copyText); }
private void OnCreateMesh_3(Texture2D texture_) { usePixe.Clear(); for (int y = 0; y < texture_.height; ++y) { for (int x = 0; x < texture_.width; ++x) { Color curColor = texture_.GetPixel(x, y); Color upColor = new Color(0, 0, 0, 0); Color leftColor = new Color(0, 0, 0, 0); Color rightColor = new Color(0, 0, 0, 0); Color DownColor = new Color(0, 0, 0, 0); Color leftDownColor = new Color(0, 0, 0, 0); Color leftUpColor = new Color(0, 0, 0, 0); Color rightDownColor = new Color(0, 0, 0, 0); Color rightUpColor = new Color(0, 0, 0, 0); if (y < texture_.height) { upColor = texture_.GetPixel(x, y + 1); } if (x > 0) { leftColor = texture_.GetPixel(x - 1, y); } if (x < texture_.width) { rightColor = texture_.GetPixel(x + 1, y); } if (y > 0) { DownColor = texture_.GetPixel(x, y - 1); } if (x > 0 && y > 0) { leftDownColor = texture_.GetPixel(x - 1, y - 1); } if (x > 0 && y < texture_.height) { leftUpColor = texture_.GetPixel(x - 1, y + 1); } if (x < texture_.width && y > 0) { rightDownColor = texture_.GetPixel(x + 1, y - 1); } if (x < texture_.width && y < texture_.height) { rightUpColor = texture_.GetPixel(x + 1, y + 1); } if (curColor.a == 0) { //bool ximian = leftDownColor.a != 0 || leftUpColor.a != 0 || rightDownColor.a != 0 || rightUpColor.a != 0; bool zheng = upColor.a != 0 || leftColor.a != 0 || rightColor.a != 0 || DownColor.a != 0; if (zheng) { usePixe.Add(new marginPixel(x, y, Color.red)); } } } } for (int i = 0; i < usePixe.Count; ++i) { marginPixel b = usePixe[i]; texture_.SetPixel(b.x, b.y, b.color); } texture_.Apply(); // ---------------------- data_1 = usePixe[0]; orderPixe.Clear(); orderPixe.Add(data_1); OnFindPixel(data_1); Debug.Log(orderPixe.Count); Vector3[] verts = new Vector3[orderPixe.Count * 2]; Vector2[] uvs = new Vector2[orderPixe.Count * 2]; Color[] colors = new Color[orderPixe.Count * 2]; int idx = 0; for (int i = 0; i < orderPixe.Count; ++i) { if (i < orderPixe.Count) { marginPixel data = orderPixe[i]; // 顶点总数除以2,目的是让mesh初始点在mesh中心 verts[idx] = new Vector3(data.x, 0, data.y); colors[idx] = data.color; // UV的值需要归一化0~1之间,所以需要除以总数 uvs[idx] = new Vector2((float)data.x / texture_.width, (float)data.y / texture_.height); idx++; // 顶点总数除以2,目的是让mesh初始点在mesh中心 verts[idx] = new Vector3(data.x, 1, data.y); colors[idx] = data.color; // UV的值需要归一化0~1之间,所以需要除以总数 uvs[idx] = new Vector2((float)data.x / texture_.width, (float)data.y / texture_.height); idx++; } } // 顶点索引信息 int[] indxs = new int[orderPixe.Count * 6]; for (int i = 0; i < indxs.Length; ++i) { int i0 = (i * 6) + 0; int i1 = (i * 6) + 1; int i2 = (i * 6) + 2; int i3 = (i * 6) + 3; int i4 = (i * 6) + 4; int i5 = (i * 6) + 5; if (i3 < indxs.Length) { indxs[i0] = i; indxs[i1] = i + 1; indxs[i2] = i + 2; indxs[i3] = i + 2; indxs[i4] = i + 3; indxs[i5] = i; } } Mesh mesh = new Mesh(); mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; mesh.vertices = verts; mesh.uv = uvs; mesh.colors = colors; mesh.triangles = indxs; mesh.UploadMeshData(false); root.GetComponent <MeshFilter>().mesh = mesh; }
/// <summary> /// 查找彩色图像像素边缘(测试) /// </summary> private void OnGetPixelMarginColorized(Texture2D tt_) { Texture2D copyText = new Texture2D(tt_.width, tt_.height); copyText.SetPixels(tt_.GetPixels());//提取text图片的颜色,赋给copyText copyText.Apply(); List <marginPixel> marginList = new List <marginPixel>(); for (int y = 0; y < copyText.height; ++y) { for (int x = 0; x < copyText.width; ++x) { Color defColor = new Color(0, 0, 0, 0); Color upColor = defColor; Color leftColor = defColor; Color rightColor = defColor; Color DownColor = defColor; Color leftDownColor = defColor; Color leftUpColor = defColor; Color rightDownColor = defColor; Color rightUpColor = defColor; Color curColor = copyText.GetPixel(x, y); if (y < copyText.height) { upColor = copyText.GetPixel(x, y + 1); } if (x > 0) { leftColor = copyText.GetPixel(x - 1, y); } if (x < copyText.width) { rightColor = copyText.GetPixel(x + 1, y); } if (y > 0) { DownColor = copyText.GetPixel(x, y - 1); } if (x > 0 && y > 0) { leftDownColor = copyText.GetPixel(x - 1, y - 1); } if (x > 0 && y < copyText.height) { leftUpColor = copyText.GetPixel(x - 1, y + 1); } if (x < copyText.width && y > 0) { rightDownColor = copyText.GetPixel(x + 1, y - 1); } if (x < copyText.width && y < copyText.height) { rightUpColor = copyText.GetPixel(x + 1, y + 1); } bool ximian = leftDownColor != curColor || leftUpColor != curColor || rightDownColor != curColor || rightUpColor != curColor; bool zheng = upColor != curColor || leftColor != curColor || rightColor != curColor || DownColor != curColor; if (zheng) { marginList.Add(new marginPixel(x, y, curColor)); } else { marginList.Add(new marginPixel(x, y, new Color(0, 0, 0, 0))); } } } for (int i = 0; i < marginList.Count; ++i) { marginPixel b_i = marginList[i]; if (b_i.color.a != 0) { marginPixel model = null; if (b_i.x <= tt_.width / 2) { for (int k = b_i.x + 1; k > b_i.x; --k) { model = marginList.FirstOrDefault(t => (t.x == k && t.y == b_i.y && t.color.a != 0)); } } else { for (int k = b_i.x - 1; k < b_i.x; ++k) { model = marginList.FirstOrDefault(t => (t.x == k && t.y == b_i.y && t.color.a != 0)); } } if (model == null) { copyText.SetPixel(b_i.x, b_i.y, b_i.color); } else { copyText.SetPixel(b_i.x, b_i.y, new Color(0, 0, 0, 0)); } } else { copyText.SetPixel(b_i.x, b_i.y, b_i.color); } } copyText.Apply(); img2.texture = copyText; }