using OpenCVCompact; using UnityEngine; public class PictureUtility { private static Material _flipMaterial; public static Material FlipMaterial { get { if (_flipMaterial == null) _flipMaterial = Resources.Load("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 OpenCVForUnity.CoreModule.Mat HorizontalFlipMat(OpenCVForUnity.CoreModule.Mat mat) { OpenCVForUnity.CoreModule.Mat dst = new OpenCVForUnity.CoreModule.Mat(); OpenCVForUnity.CoreModule.Core.flip(mat, dst, 1); return dst; } //纠正鱼眼透视畸变 public static OpenCVForUnity.CoreModule.Mat Undistort(OpenCVForUnity.CoreModule.Mat src) { //摄像机的4个畸变系数:k1,k2,k3,k4 OpenCVForUnity.CoreModule.Mat K = new OpenCVForUnity.CoreModule.Mat(3, 3, OpenCVForUnity.CoreModule.CvType.CV_64FC1); K.put(0, 0, 1151.7, 0, 985.6, 0, 1153.4, 573.004, 0, 0, 1); OpenCVForUnity.CoreModule.Mat D = new OpenCVForUnity.CoreModule.Mat(5, 1, OpenCVForUnity.CoreModule.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); OpenCVForUnity.CoreModule.Mat retVal = src.clone(); OpenCVForUnity.Calib3dModule.Calib3d.undistort(src, retVal, K, D); return retVal; } public static OpenCVCompact.Mat GetROIRect(OpenCVCompact.Mat bgrMat, out int[] roiRegion) { OpenCVCompact.Rect roiRect; roiRegion = GetPositionRect(bgrMat.width(), bgrMat.height()); roiRect = new OpenCVCompact.Rect( (int)roiRegion[0], (int)roiRegion[1], Mathf.Abs((int)(roiRegion[2] - roiRegion[0])), Mathf.Abs((int)(roiRegion[3] - roiRegion[1]))); OpenCVCompact.Mat personRectImg = new OpenCVCompact.Mat(bgrMat, roiRect); return personRectImg; } public static int[] GetPositionRect(int width, int height) { int[] retVal = new int[4]; int shift = (int)(width / 2 * 0.2f); if (!GlobalData.Instance.IsDriverPosition) //副驾驶 { retVal[0] = 0; //left retVal[1] = 0; //top retVal[2] = (width / 2) - shift; //right retVal[3] = height; //bottom } else { retVal[0] = (width / 2) + shift; //left retVal[1] = 0; //top retVal[2] = width; //right retVal[3] = height; //bottom } return retVal; } public static void DrawAimBox(OpenCVForUnity.CoreModule.Mat img, float[] box, float shift, int lineLength) { float left = box[0] + shift; float top = box[1] + shift; float right = box[2] - shift; float bottom = box[3] - shift; OpenCVForUnity.ImgprocModule.Imgproc.cvtColor(img, img, OpenCVForUnity.ImgprocModule.Imgproc.COLOR_RGBA2RGB); //界面蓝 RGB = 19, 115, 230 OpenCVForUnity.ImgprocModule.Imgproc.rectangle(img, new OpenCVForUnity.CoreModule.Point(left, top), new OpenCVForUnity.CoreModule.Point(right, bottom), new OpenCVForUnity.CoreModule.Scalar(255, 255, 255, 50), 2, 3); //右下角 OpenCVForUnity.ImgprocModule.Imgproc.line(img, new OpenCVForUnity.CoreModule.Point(right, bottom), new OpenCVForUnity.CoreModule.Point(right - lineLength, bottom), new OpenCVForUnity.CoreModule.Scalar(255, 255, 255, 255), 10, 3); OpenCVForUnity.ImgprocModule.Imgproc.line(img, new OpenCVForUnity.CoreModule.Point(right, bottom), new OpenCVForUnity.CoreModule.Point(right, bottom - lineLength), new OpenCVForUnity.CoreModule.Scalar(255, 255, 255, 255), 10, 3); //左下角 OpenCVForUnity.ImgprocModule.Imgproc.line(img, new OpenCVForUnity.CoreModule.Point(left, bottom), new OpenCVForUnity.CoreModule.Point(left + lineLength, bottom), new OpenCVForUnity.CoreModule.Scalar(255, 255, 255, 255), 10, 3); OpenCVForUnity.ImgprocModule.Imgproc.line(img, new OpenCVForUnity.CoreModule.Point(left, bottom), new OpenCVForUnity.CoreModule.Point(left, bottom - lineLength), new OpenCVForUnity.CoreModule.Scalar(255, 255, 255, 255), 10, 3); //右上角 OpenCVForUnity.ImgprocModule.Imgproc.line(img, new OpenCVForUnity.CoreModule.Point(right, top), new OpenCVForUnity.CoreModule.Point(right - lineLength, top), new OpenCVForUnity.CoreModule.Scalar(255, 255, 255, 255), 10, 3); OpenCVForUnity.ImgprocModule.Imgproc.line(img, new OpenCVForUnity.CoreModule.Point(right, top), new OpenCVForUnity.CoreModule.Point(right, top + lineLength), new OpenCVForUnity.CoreModule.Scalar(255, 255, 255, 255), 10, 3); //左上角 OpenCVForUnity.ImgprocModule.Imgproc.line(img, new OpenCVForUnity.CoreModule.Point(left, top), new OpenCVForUnity.CoreModule.Point(left + lineLength, top), new OpenCVForUnity.CoreModule.Scalar(255, 255, 255, 255), 10, 3); OpenCVForUnity.ImgprocModule.Imgproc.line(img, new OpenCVForUnity.CoreModule.Point(left, top), new OpenCVForUnity.CoreModule.Point(left, top + lineLength), new OpenCVForUnity.CoreModule.Scalar(255, 255, 255, 255), 10, 3); OpenCVForUnity.ImgprocModule.Imgproc.cvtColor(img, img, OpenCVForUnity.ImgprocModule.Imgproc.COLOR_RGB2RGBA); } public static void DrawAimBoxFaceDetect(OpenCVCompact.Mat img, float[] box, float shift, int lineLength) { float left = box[0] + shift; float top = box[1] + shift; float right = box[2] - shift; float bottom = box[3] - shift; OpenCVCompact.Imgproc.cvtColor(img, img, OpenCVCompact.Imgproc.COLOR_RGBA2RGB); //界面蓝 RGB = 19, 115, 230 OpenCVCompact.Imgproc.rectangle(img, new OpenCVCompact.Point(left, top), new OpenCVCompact.Point(right, bottom), new OpenCVCompact.Scalar(255, 255, 255, 50), 2, 3); //右下角 OpenCVCompact.Imgproc.line(img, new OpenCVCompact.Point(right, bottom), new OpenCVCompact.Point(right - lineLength, bottom), new OpenCVCompact.Scalar(255, 255, 255, 255), 10, 3); OpenCVCompact.Imgproc.line(img, new OpenCVCompact.Point(right, bottom), new OpenCVCompact.Point(right, bottom - lineLength), new OpenCVCompact.Scalar(255, 255, 255, 255), 10, 3); //左下角 OpenCVCompact.Imgproc.line(img, new OpenCVCompact.Point(left, bottom), new OpenCVCompact.Point(left + lineLength, bottom), new OpenCVCompact.Scalar(255, 255, 255, 255), 10, 3); OpenCVCompact.Imgproc.line(img, new OpenCVCompact.Point(left, bottom), new OpenCVCompact.Point(left, bottom - lineLength), new OpenCVCompact.Scalar(255, 255, 255, 255), 10, 3); //右上角 OpenCVCompact.Imgproc.line(img, new OpenCVCompact.Point(right, top), new OpenCVCompact.Point(right - lineLength, top), new OpenCVCompact.Scalar(255, 255, 255, 255), 10, 3); OpenCVCompact.Imgproc.line(img, new OpenCVCompact.Point(right, top), new OpenCVCompact.Point(right, top + lineLength), new OpenCVCompact.Scalar(255, 255, 255, 255), 10, 3); //左上角 OpenCVCompact.Imgproc.line(img, new OpenCVCompact.Point(left, top), new OpenCVCompact.Point(left + lineLength, top), new OpenCVCompact.Scalar(255, 255, 255, 255), 10, 3); OpenCVCompact.Imgproc.line(img, new OpenCVCompact.Point(left, top), new OpenCVCompact.Point(left, top + lineLength), new OpenCVCompact.Scalar(255, 255, 255, 255), 10, 3); OpenCVCompact.Imgproc.cvtColor(img, img, OpenCVCompact.Imgproc.COLOR_RGB2RGBA); } }