Health/Assets/Scripts/Tool/PictureUtility.cs

57 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using OpenCVForUnity.CoreModule;
using UnityEngine;
public class PictureUtility
{
private static Material _flipMaterial;
public static Material FlipMaterial
{
get
{
if (_flipMaterial == null)
_flipMaterial = Resources.Load<Material>("Materials/Unlit_FlipHorizontal");
return _flipMaterial;
}
set => _flipMaterial = value;
}
public static Texture2D HorizontalFlipTexture(Texture2D texture)
{
//得到图片的宽高
Texture2D retVal = new Texture2D(texture.width, texture.height);
RenderTexture tmp = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.ARGB32);
Graphics.Blit(texture, tmp, _flipMaterial);
RenderTexture.active = tmp;
retVal.ReadPixels(new UnityEngine.Rect(0, 0, texture.width, texture.height), 0, 0);
RenderTexture.ReleaseTemporary(tmp);
return retVal;
}
public static Mat HorizontalFlipMat(Mat mat)
{
Mat dst = new Mat();
Core.flip(mat, dst, 1);
return dst;
}
//纠正鱼眼透视畸变
public static Mat Undistort(Mat src)
{
//摄像机的4个畸变系数k1,k2,k3,k4
Mat K = new Mat(3, 3, CvType.CV_64FC1);
K.put(0, 0, 1151.7, 0, 985.6, 0, 1153.4, 573.004, 0, 0, 1);
Mat D = new Mat(5, 1, CvType.CV_64FC1);
//D.put(0, 0, -0.42, 0.2196, 0.0038, 0.013, -0.06);
D.put(0, 0, -0.01, 0.09, 0, 0, -0.19);
Mat retVal = src.clone();
OpenCVForUnity.Calib3dModule.Calib3d.undistort(src, retVal, K, D);
return retVal;
}
}