展示了如何使用OpenCVForUnity库在Unity中实现物理绿幕效果。它使用相机捕捉视频流,并使用OpenCVForUnity库中的图像处理算法,将视频流中的绿色背景(可调正HSV三个参或点击采样以切换替换颜色)替换为其他背景。该示例程序还提供了许多自定义参数,可以用于调整绿幕效果的不同方面,例如颜色范围、背景图像和阈值等。如果您想了解更多关于这个示例程序的信息,可以查阅OpenCVForUnity的官方文档。
#if !(PLATFORM_LUMIN && !UNITY_EDITOR)
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.ImgprocModule;
using OpenCVForUnity.UnityUtils.Helper;
using OpenCVForUnity.UnityUtils;
namespace OpenCVForUnityExample
{
/// <summary>
/// Physical Green Screen Example
/// An example of creating a chromakey mask and compositing background image. (aka green-screen compositing)
/// </summary>
[RequireComponent(typeof(WebCamTextureToMatHelper))]
public class PhysicalGreenScreenExample : MonoBehaviour
{
/// <summary>
/// The background image texture.
/// 背景图像纹理。
/// </summary>
public Texture2D backGroundImageTexture;
/// <summary>
/// The radius range sliders.
/// 半径范围滑块。
/// </summary>
public Slider hRadiusRangeSlider;
public Slider sRadiusRangeSlider;
public Slider vRadiusRangeSlider;
/// <summary>
/// The spectrum image UI.
/// 光谱图像用户界面。
/// </summary>
public RawImage spectrumImage;
/// <summary>
/// The hsv mat.
/// hsv矩阵。
/// </summary>
Mat hsvMat;
/// <summary>
/// The chroma key mask mat.
/// 色度键掩码矩阵。
/// </summary>
Mat chromaKeyMaskMat;
/// <summary>
/// The background image mat.
/// 背景图像矩阵。
/// </summary>
Mat backGroundImageMat;
// Lower and Upper bounds for range checking in HSV color space
// HSV 颜色空间中范围检查的下限和上限
Scalar lowerBound = new Scalar(0);
Scalar upperBound = new Scalar(0);
// Color radius for range checking in HSV color space
// 在 HSV 颜色空间中进行范围检查的颜色半径
Scalar colorRadiusRange = new Scalar(25, 50, 50, 0);
/// <summary>
/// The BLOB color hsv.
/// BLOB 颜色 hsv。
/// </summary>
Scalar blobColorHsv = new Scalar(99, 255, 177, 255);
/// <summary>
/// The spectrum mat.
/// 频谱矩阵。
/// </summary>
Mat spectrumMat;
/// <summary>
/// The spectrum texture.
/// 频谱贴图。
/// </summary>
Texture2D spectrumTexture;
/// <summary>
/// The stored touch point.
/// 存储的接触点。
/// </summary>
Point storedTouchPoint;
/// <summary>
/// The texture.
/// 贴图
/// </summary>
Texture2D texture;
/// <summary>
/// The webcam texture to mat helper.
/// OpenCVForUnity库中提供的一个帮助类,用于在Unity中将WebCamTexture对象转换为OpenCV中的Mat对象。
/// </summary>
WebCamTextureToMatHelper webCamTextureToMatHelper;
/// <summary>
/// The FPS monitor.
/// FPS监视器。
/// </summary>
FpsMonitor fpsMonitor;
// Use this for initialization
void Start()
{
fpsMonitor = GetComponent<FpsMonitor>();
webCamTextureToMatHelper = gameObject.GetComponent<WebCamTextureToMatHelper>();
#if UNITY_ANDROID && !UNITY_EDITOR
// Avoids the front camera low light issue that occurs in only some Android devices (e.g. Google Pixel, Pixel2).
webCamTextureToMatHelper.avoidAndroidFrontCameraLowLightIssue = true;
#endif
webCamTextureToMatHelper.Initialize();
}
/// <summary>
/// Raises the web cam texture to mat helper initialized event.
/// 将网络摄像头纹理提升到矩阵辅助对象初始化事件。
/// 该函数是WebCamTextureToMatHelper类中的回调函数,用于在WebCamTextureToMatHelper初始化完成后进行一些处理。
/// </summary>
public void OnWebCamTextureToMatHelperInitialized()
{
Debug.Log("OnWebCamTextureToMatHelperInitialized");
// 通过webCamTextureToMatHelper.GetMat()方法获取相机捕获的图像数据
Mat webCamTextureMat = webCamTextureToMatHelper.GetMat();
// 将OpenCV Mat格式的webCamTextureMat转换为Unity的Texture2D格式,并将其赋值给变量texture。
texture = new Texture2D(webCamTextureMat.cols(), webCamTextureMat.rows(), TextureFormat.RGBA32, false);
Utils.matToTexture2D(webCamTextureMat, texture);
// 通过将Texture2D对象设置为GameObject的主纹理,可以将图像显示在屏幕上。
gameObject.GetComponent<Renderer>().material.mainTexture = texture;
gameObject.transform.localScale = new Vector3(webCamTextureMat.cols(), webCamTextureMat.rows(), 1);
Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
if (fpsMonitor != null)
{
fpsMonitor.Add("width", webCamTextureMat.width().ToString());
fpsMonitor.Add("height", webCamTextureMat.height().ToString());
fpsMonitor.Add("orientation", Screen.orientation.ToString());
fpsMonitor.Add("blobColorHsv", "\n" + blobColorHsv.ToString());
fpsMonitor.Add("colorRadiusRange", "\n" + colorRadiusRange.ToString());
fpsMonitor.Toast("Touch the screen to specify the chromakey color.", 360);
}
/// <summary>
/// 根据屏幕和图像的比例关系来调整相机的正交大小。
/// </summary>
//取displayMat矩阵的宽度和高度,并计算屏幕与图像宽度和高度的比例(widthScale和heightScale)。
float width = webCamTextureMat.width();
float height = webCamTextureMat.height();
float widthScale = (float)Screen.width / width;
float heightScale = (float)Screen.height / height;
if (widthScale < heightScale)
{
// 如果宽度比例更小,则将屏幕高度与屏幕宽度的比例作为参考,根据图像宽度来计算相机正交大小,使得图像的宽度可以完全显示在屏幕上。
Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
}
else
{
// 如果高度比例更小,则将屏幕宽度与屏幕高度的比例作为参考,根据图像高度来计算相机正交大小,使得图像的高度可以完全显示在屏幕上。
Camera.main.orthographicSize = height / 2;
}
hsvMat = new Mat((int)height, (int)width, CvType.CV_8UC3);// 存储HSV颜色空间的图像。
chromaKeyMaskMat = new Mat(hsvMat.size(), CvType.CV_8UC1);// 用于存储绿幕掩膜(即将绿色背景替换为其他背景时,需要将绿色区域设为1,其他区域设为0的二值图像)。
backGroundImageMat = new Mat(hsvMat.size(), CvType.CV_8UC4, new Scalar(39, 255, 86, 255));// 用于存储替换绿幕后的背景图像,这里将其初始化为一个绿色的背景图像
/// <summary>
/// 将Unity中的Texture2D类型的backGroundImageTexture转换为OpenCV中的Mat类型,并将其赋值给backGroundImageMat。
/// 如果backGroundImageTexture为null,则跳过此代码块。
/// </summary>
if (backGroundImageTexture != null)
{
using (Mat bgMat = new Mat(backGroundImageTexture.height, backGroundImageTexture.width, CvType.CV_8UC4))
{
Utils.texture2DToMat(backGroundImageTexture, bgMat);//将backGroundImageTexture转换为Mat格式,并将其赋值给bgMat。该方法将Texture2D对象中的像素数据复制到Mat对象中。
Imgproc.resize(bgMat, backGroundImageMat, backGroundImageMat.size());//使用OpenCV中的Imgproc.resize方法,将bgMat调整为backGroundImageMat的大小。
}
}
/// <summary>
/// 创建一个OpenCV Mat对象spectrumMat和一个Unity Texture2D对象spectrumTexture,用于存储频谱图像。
/// </summary>
spectrumMat = new Mat(100, 100, CvType.CV_8UC4, new Scalar(255, 255, 255, 255));// 初始化为一个白色图像
spectrumTexture = new Texture2D(spectrumMat.cols(), spectrumMat.rows(), TextureFormat.RGBA32, false);
// Set default chromakey color.设置默认的chromakey颜色。
blobColorHsv = new Scalar(99, 255, 177, 255); // = R:39 G:255 B:86 (Green screen)用于表示绿幕的颜色,即RGB颜色空间中的(39, 255, 86)
SetHsvColor(blobColorHsv);//将这个颜色值用于后续的图像处理操作,例如创建掩膜和提取绿幕部分的图像。
}
/// <summary>
/// Raises the web cam texture to mat helper disposed event.
/// 用于在webCamTextureToMatHelper对象被销毁时清理资源。
/// </summary>
public void OnWebCamTextureToMatHelperDisposed()
{
Debug.Log("OnWebCamTextureToMatHelperDisposed");
if (hsvMat != null)
{
hsvMat.Dispose();
hsvMat = null;
}
if (chromaKeyMaskMat != null)
{
chromaKeyMaskMat.Dispose();
chromaKeyMaskMat = null;
}
if (backGroundImageMat != null)
{
backGroundImageMat.Dispose();
backGroundImageMat = null;
}
if (spectrumMat != null)
{
spectrumMat.Dispose();
spectrumMat = null;
}
if (texture != null)
{
Texture2D.Destroy(texture);
texture = null;
}
if (spectrumTexture != null)
{
Texture2D.Destroy(spectrumTexture);
texture = null;
}
}
/// <summary>
/// Raises the web cam texture to mat helper error occurred event.
/// 将网络摄像头纹理提升到矩阵辅助对象发生错误事件。
/// </summary>
/// <param name="errorCode">Error code.</param>
public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode)
{
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
}
// Update is called once per frame
void Update()
{
/// <summary>
/// 检测用户在屏幕上点击了哪个位置,并将该位置的坐标存储在变量storedTouchPoint中。
/// 该代码基于平台的不同使用不同的输入方式:在Unity编辑器中使用鼠标,而在Android和iOS设备上使用触摸屏。
/// </summary>
// 它将下面的代码块仅在Android或iOS平台且不在Unity编辑器中运行时编译。
#if ((UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR)
//Touch
// 如果条件成立,代码将检查是否有一个触摸点(即用户是否点击了屏幕)。
如果有且该触摸点已经结束(即用户已经松开触摸屏),则获取该触摸点的位置,并将其作为Point对象存储在变量storedTouchPoint中。
int touchCount = Input.touchCount;
if (touchCount == 1)
{
Touch t = Input.GetTouch (0);
if(t.phase == TouchPhase.Ended && !EventSystem.current.IsPointerOverGameObject (t.fingerId)) {
storedTouchPoint = new Point (t.position.x, t.position.y);
//Debug.Log ("touch X " + t.position.x);
//Debug.Log ("touch Y " + t.position.y);
}
}
#else
// 代码将检查鼠标左键是否被松开,并且当前没有任何UI元素被指针覆盖(即没有点击在UI上)。
// 如果这些条件都满足,代码将获取鼠标位置,并将其作为Point对象存储在变量storedTouchPoint中。
//Mouse
if (Input.GetMouseButtonUp(0) && !EventSystem.current.IsPointerOverGameObject())
{
storedTouchPoint = new Point(Input.mousePosition.x, Input.mousePosition.y);
//Debug.Log ("mouse X " + Input.mousePosition.x);
//Debug.Log ("mouse Y " + Input.mousePosition.y);
}
#endif
// 实时处理视频帧
if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame())//检查摄像头是否正在运行并且这帧视频已经更新
{
Mat rgbaMat = webCamTextureToMatHelper.GetMat();//获取当前帧的Mat对象。
if (storedTouchPoint != null)// 代码检查是否有存储在变量storedTouchPoint中的触摸点。
{
ConvertScreenPointToTexturePoint(storedTouchPoint, storedTouchPoint, gameObject, rgbaMat.cols(), rgbaMat.rows());//将该点的位置转换为Mat对象中的纹理坐标
OnTouch(rgbaMat, storedTouchPoint);//调用OnTouch函数进行处理
storedTouchPoint = null;// 将变量storedTouchPoint设置为null。
}
// Convert the color space from RGBA to HSV_FULL.将颜色空间从 RGBA 转换为 HSV_FULL。
// HSV_FULL is HSV with H elements scaled from 0 to 255.HSV_FULL 是 H 元素从 0 缩放到 255 的 HSV。
Imgproc.cvtColor(rgbaMat, hsvMat, Imgproc.COLOR_RGB2HSV_FULL);
// Create a chromakey mask from extracting the lower and upper limits range of values in the HSV color space.
// 通过提取 HSV 颜色空间中值的下限和上限范围来创建色度键蒙版。
Core.inRange(hsvMat, lowerBound, upperBound, chromaKeyMaskMat);
// Compose the background image.
// 组成背景图像。
// 将背景图像的Mat对象(backGroundImageMat)复制到前景图像的Mat对象(rgbaMat)上,但仅在掩膜Mat对象(chromaKeyMaskMat)的对应像素处的值为非零时才会进行复制。
backGroundImageMat.copyTo(rgbaMat, chromaKeyMaskMat);
Utils.matToTexture2D(rgbaMat, texture);
}
}
/// <summary>
/// 该函数用于处理触摸事件,并计算触摸点附近区域的平均颜色,以便后续的背景替换过程中将背景与前景融合更自然。
/// </summary>
/// <param name="img"></param>当前帧的图像
/// <param name="touchPoint"></param>触摸点的位置
private void OnTouch(Mat img, Point touchPoint)
{
int cols = img.cols();
int rows = img.rows();
int x = (int)touchPoint.x;
int y = (int)touchPoint.y;
//Debug.Log ("Touch image coordinates: (" + x + ", " + y + ")");
//首先检查触摸点是否在图像的范围内,如果不是,则直接返回。
if ((x < 0) || (y < 0) || (x > cols) || (y > rows))
return;
// 接下来,函数使用指定的触摸点位置和一个10x10的矩形框来选取触摸点周围的一个区域,称之为“touchedRegion”。
OpenCVForUnity.CoreModule.Rect touchedRect = new OpenCVForUnity.CoreModule.Rect();
touchedRect.x = (x > 5) ? x - 5 : 0;
touchedRect.y = (y > 5) ? y - 5 : 0;
touchedRect.width = (x + 5 < cols) ? x + 5 - touchedRect.x : cols - touchedRect.x;
touchedRect.height = (y + 5 < rows) ? y + 5 - touchedRect.y : rows - touchedRect.y;
using (Mat touchedRegionRgba = img.submat(touchedRect))
using (Mat touchedRegionHsv = new Mat())
{
// 将touchedRegion的RGB颜色空间的Mat对象转换为HSV_FULL颜色空间的Mat对象
Imgproc.cvtColor(touchedRegionRgba, touchedRegionHsv, Imgproc.COLOR_RGB2HSV_FULL);
// Calculate average color of touched region.
// 计算touchedRegion颜色的平均值
// 平均值用于设置blobColorHsv变量的值,blobColorHsv是一个HSV_FULL颜色空间的Scalar对象,用于指定颜色范围。
blobColorHsv = Core.sumElems(touchedRegionHsv);//计算指定的HSV_FULL颜色空间的Mat对象(touchedRegionHsv)中所有像素的和,将结果存储在一个Scalar对象(blobColorHsv)中。blobColorHsv将包含该区域内所有像素的颜色和。
//blobColorHsv被用于计算平均颜色,以便在背景替换过程中指定绿幕的HSV颜色范围。
int pointCount = touchedRect.width * touchedRect.height;
for (int i = 0; i < blobColorHsv.val.Length; i++)
blobColorHsv.val[i] /= pointCount;
SetHsvColor(blobColorHsv);
}
}
/// <summary>
/// 定义了一个名为SetHsvColor的函数,该函数用于设置绿幕的HSV颜色范围。
/// </summary>
/// <param name="hsvColor"></param>包含了要设置的颜色范围的平均值
public void SetHsvColor(Scalar hsvColor)
{
// Calculate lower and Upper bounds.
// 计算下限和上限。
// 函数将hsvColor的每个通道值减去或加上相应的颜色半径值,得到下限和上限
double minH = (hsvColor.val[0] >= colorRadiusRange.val[0]) ? hsvColor.val[0] - colorRadiusRange.val[0] : 0;
double maxH = (hsvColor.val[0] + colorRadiusRange.val[0] <= 255) ? hsvColor.val[0] + colorRadiusRange.val[0] : 255;
//下限和上限被存储在两个Scalar对象(lowerBound和upperBound)中,并在后续的背景替换过程中使用。
lowerBound.val[0] = minH;
upperBound.val[0] = maxH;
lowerBound.val[1] = hsvColor.val[1] - colorRadiusRange.val[1];
lowerBound.val[1] = (lowerBound.val[1] >= 0) ? lowerBound.val[1] : 0;
upperBound.val[1] = hsvColor.val[1] + colorRadiusRange.val[1];
upperBound.val[1] = (upperBound.val[1] <= 255) ? upperBound.val[1] : 255;
lowerBound.val[2] = hsvColor.val[2] - colorRadiusRange.val[2];
lowerBound.val[2] = (lowerBound.val[2] >= 0) ? lowerBound.val[2] : 0;
upperBound.val[2] = hsvColor.val[2] + colorRadiusRange.val[2];
upperBound.val[2] = (upperBound.val[2] <= 255) ? upperBound.val[2] : 255;
lowerBound.val[3] = 0;
upperBound.val[3] = 255;
// Generate a spectrum chart.
// 生成了一个HSV颜色空间的Mat对象(spectrumHsv),用于绘制颜色范围的频谱图。
using (Mat spectrumHsv = new Mat((int)(upperBound.val[1] - lowerBound.val[1]), (int)(maxH - minH), CvType.CV_8UC3))
using (Mat spectrumRgba = new Mat((int)(upperBound.val[1] - lowerBound.val[1]), (int)(maxH - minH), CvType.CV_8UC4))
{
//使用for循环遍历spectrumHsv的所有像素,并设置每个像素的值,以便生成频谱图。
for (int i = 0; i < upperBound.val[1] - lowerBound.val[1]; i++)
{
for (int j = 0; j < maxH - minH; j++)
{
byte[] tmp = { (byte)(minH + j), (byte)(lowerBound.val[1] + i), (byte)hsvColor.val[2] };
spectrumHsv.put(i, j, tmp);
}
}
// 将spectrumHsv转换为RGB颜色空间的Mat对象(spectrumRgba),并将其调整大小以适应屏幕。
Imgproc.cvtColor(spectrumHsv, spectrumRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);
//该函数将spectrumRgba转换为Texture2D对象,并将其设置为spectrumImage的纹理。
Imgproc.resize(spectrumRgba, spectrumMat, spectrumMat.size());
Utils.matToTexture2D(spectrumMat, spectrumTexture);
spectrumImage.texture = spectrumTexture;
}
// 向其添加一些调试信息
if (fpsMonitor != null)
{
fpsMonitor.Add("blobColorHsv", "\n" + blobColorHsv.ToString());
fpsMonitor.Add("colorRadiusRange", "\n" + colorRadiusRange.ToString());
}
//Debug.Log("blobColorHsv: " + blobColorHsv);
//Debug.Log("lowerBound: " + lowerBound);
//Debug.Log("upperBound: " + upperBound);
//Debug.Log("blobColorRgba: " + ConverScalarHsv2Rgba(blobColorHsv));
}
/// <summary>
/// 用于将指定的HSV颜色转换为RGBA颜色
/// </summary>
/// <param name="hsvColor"></param>要转换的颜色
/// <returns></returns>返回一个Scalar对象,该对象表示转换后的颜色
private Scalar ConverScalarHsv2Rgba(Scalar hsvColor)
{
Scalar rgbaColor;
using (Mat pointMatRgba = new Mat())//pointMatRgba将用于存储RGBA颜色
using (Mat pointMatHsv = new Mat(1, 1, CvType.CV_8UC3, hsvColor))//pointMatHsv将用于存储HSV颜色
{
Imgproc.cvtColor(pointMatHsv, pointMatRgba, Imgproc.COLOR_HSV2RGB_FULL, 4);//使用Imgproc.cvtColor函数将pointMatHsv从HSV颜色空间转换为RGB颜色空间,并将结果存储在pointMatRgba中
rgbaColor = new Scalar(pointMatRgba.get(0, 0));//从pointMatRgba中提取颜色值,并将其存储在一个Scalar对象(rgbaColor)中
}
return rgbaColor;
}
/// <summary>
/// Converts the screen point to texture point.
/// 将屏幕点转换为纹理点。
/// </summary>
/// <param name="screenPoint">Screen point.</param>要转换的屏幕点。
/// <param name="dstPoint">Dst point.</param>计算得到的纹理点将存储在这个变量中。
/// <param name="texturQuad">Texture quad.</param>包含纹理的GameObject。
/// <param name="textureWidth">Texture width.</param>纹理的宽度
/// <param name="textureHeight">Texture height.</param>纹理的高度
/// <param name="camera">Camera.</param>用于从屏幕坐标系转换到世界坐标系的相机
private void ConvertScreenPointToTexturePoint(Point screenPoint, Point dstPoint, GameObject textureQuad, int textureWidth = -1, int textureHeight = -1, Camera camera = null)
{
// 如果纹理的宽度和高度为负数,则从textureQuad的Renderer组件或缩放中获取它们。
if (textureWidth < 0 || textureHeight < 0)
{
Renderer r = textureQuad.GetComponent<Renderer>();
if (r != null && r.material != null && r.material.mainTexture != null)
{
textureWidth = r.material.mainTexture.width;
textureHeight = r.material.mainTexture.height;
}
else
{
textureWidth = (int)textureQuad.transform.localScale.x;
textureHeight = (int)textureQuad.transform.localScale.y;
}
}
// 如果未提供相机,则使用主相机。
if (camera == null)
camera = Camera.main;
//获取纹理Quad的位置和缩放
Vector3 quadPosition = textureQuad.transform.localPosition;
Vector3 quadScale = textureQuad.transform.localScale;
// 使用相机将四个角点坐标从世界坐标系转换为屏幕坐标系
Vector2 tl = camera.WorldToScreenPoint(new Vector3(quadPosition.x - quadScale.x / 2, quadPosition.y + quadScale.y / 2, quadPosition.z));
Vector2 tr = camera.WorldToScreenPoint(new Vector3(quadPosition.x + quadScale.x / 2, quadPosition.y + quadScale.y / 2, quadPosition.z));
Vector2 br = camera.WorldToScreenPoint(new Vector3(quadPosition.x + quadScale.x / 2, quadPosition.y - quadScale.y / 2, quadPosition.z));
Vector2 bl = camera.WorldToScreenPoint(new Vector3(quadPosition.x - quadScale.x / 2, quadPosition.y - quadScale.y / 2, quadPosition.z));
//
using (Mat srcRectMat = new Mat(4, 1, CvType.CV_32FC2))
using (Mat dstRectMat = new Mat(4, 1, CvType.CV_32FC2))
{
// 使用四个屏幕坐标点(tl,tr,br,bl)和纹理Quad的缩放(quadScale)定义了两个Mat对象
srcRectMat.put(0, 0, tl.x, tl.y, tr.x, tr.y, br.x, br.y, bl.x, bl.y);//存储了四个屏幕坐标点的坐标
dstRectMat.put(0, 0, 0, 0, quadScale.x, 0, quadScale.x, quadScale.y, 0, quadScale.y);//存储了对应的纹理坐标点的坐标
using (Mat perspectiveTransform = Imgproc.getPerspectiveTransform(srcRectMat, dstRectMat))//计算一个透视变换矩阵,该变换矩阵将srcRectMat中的坐标点映射到dstRectMat中的坐标点
using (MatOfPoint2f srcPointMat = new MatOfPoint2f(screenPoint))//存储了要转换的屏幕点的坐标
using (MatOfPoint2f dstPointMat = new MatOfPoint2f())//用于存储计算得到的纹理点的坐标
{
Core.perspectiveTransform(srcPointMat, dstPointMat, perspectiveTransform);//使用Core.perspectiveTransform函数将srcPointMat中的点应用透视变换,并将结果存储在dstPointMat中
// 使用dstPointMat中的坐标计算纹理点的坐标,并将其存储在dstPoint中
// dstPointMat.get(0, 0)获取了计算得到的矩阵坐标点的x和y坐标
// 然后将其分别乘以textureWidth/quadScale.x和textureHeight/quadScale.y,以将坐标点的单位从Quad的大小转换为纹理的大小
dstPoint.x = dstPointMat.get(0, 0)[0] * textureWidth / quadScale.x;
dstPoint.y = dstPointMat.get(0, 0)[1] * textureHeight / quadScale.y;
}
}
}
/// <summary>
/// Raises the destroy event.
/// </summary>
void OnDestroy()
{
webCamTextureToMatHelper.Dispose();
}
/// <summary>
/// Raises the back button click event.
/// </summary>
public void OnBackButtonClick()
{
SceneManager.LoadScene("OpenCVForUnityExample");
}
/// <summary>
/// Raises the play button click event.
/// </summary>
public void OnPlayButtonClick()
{
webCamTextureToMatHelper.Play();
}
/// <summary>
/// Raises the pause button click event.
/// </summary>
public void OnPauseButtonClick()
{
webCamTextureToMatHelper.Pause();
}
/// <summary>
/// Raises the stop button click event.
/// </summary>
public void OnStopButtonClick()
{
webCamTextureToMatHelper.Stop();
}
/// <summary>
/// Raises the change camera button click event.
/// </summary>
public void OnChangeCameraButtonClick()
{
webCamTextureToMatHelper.requestedIsFrontFacing = !webCamTextureToMatHelper.requestedIsFrontFacing;
}
/// <summary>
/// Raises the radius range slider value changed event.
/// </summary>
public void OnRadiusRangeSliderValueChanged()
{
colorRadiusRange = new Scalar(hRadiusRangeSlider.value, sRadiusRangeSlider.value, vRadiusRangeSlider.value, 255);
SetHsvColor(blobColorHsv);
}
}
}
#endif