opencvsharp + unity进行轮廓凸出(ConvexityHull2)

来源:互联网 发布:js获取当前日期 编辑:程序博客网 时间:2024/04/29 16:21
using UnityEngine;using System.Collections;using OpenCvSharp;using System.Runtime.InteropServices;using System.Linq;public class CovextityHull : MonoBehaviour {    Texture2D img;    public GameObject plane;    string imagename = "Assets/Resources/hand.jpg";    public IplImage image;    // Use this for initialization    void Start()    {          image = Cv.LoadImage(imagename, LoadMode.AnyColor);        img = new Texture2D(image.Width, image.Height, TextureFormat.RGBA32, false);        System.Random rand = new System.Random();        //for (; ; )        //{        int count = rand.Next() % 100 + 1;        CvPoint[] ptseq = new CvPoint[count];        for (int i = 0; i < ptseq.Length; i++)        {            ptseq[i] = new CvPoint            {                X = rand.Next() % (image.Width / 2) + image.Width / 4,                Y = rand.Next() % (image.Height / 2) + image.Height / 4            };        }        Cv.Zero(image);        foreach (CvPoint pt in ptseq)        {            Cv.Circle(image, pt, 2, new CvColor(255, 0, 0), -1);        }        CvPoint[] hull;        Cv.ConvexHull2(ptseq, out hull, ConvexHullOrientation.Clockwise);        CvPoint pt0 = hull.Last();        foreach (CvPoint pt in hull)        {            Cv.Line(image, pt0, pt, CvColor.Green);            pt0 = pt;        }    }    // Update is called once per frame    void Update()    {        Color[] cols = new Color[img.width * img.height];        for (int y = 0; y < img.height; y++)            for (int x = 0; x < img.width; x++)            {                CvColor col = image.Get2D(y, x);                cols[y * img.width + x] = new Color(col.R / 255.0f, col.G / 255.0f, col.B / 255.0f, 1.0f);            }        img.SetPixels(cols);        img.Apply();        plane.renderer.material.mainTexture = img;    }}
下载完全代码地址:http://download.csdn.net/detail/u011402444/5961627
原创粉丝点击