126 lines
4.1 KiB
C#
126 lines
4.1 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using OpenCVForUnity.CoreModule;
|
|||
|
using OpenCVForUnity.ImgprocModule;
|
|||
|
using System.Linq;
|
|||
|
using Vuforia;
|
|||
|
|
|||
|
public class detection_script_2 : MonoBehaviour
|
|||
|
{
|
|||
|
Mat cameraImageMat;
|
|||
|
Mat stylizedMat = new Mat();
|
|||
|
Mat greyMat = new Mat();
|
|||
|
|
|||
|
|
|||
|
int width = 400;
|
|||
|
int height = 400;
|
|||
|
|
|||
|
Texture2D outputTexture;
|
|||
|
MatOfPoint2f dstPoints;
|
|||
|
|
|||
|
// Start is called before the first frame update
|
|||
|
void Start()
|
|||
|
{
|
|||
|
dstPoints = new MatOfPoint2f();
|
|||
|
dstPoints.alloc(4);
|
|||
|
dstPoints.put(3, 0, width, height);
|
|||
|
dstPoints.put(2, 0, 0, height);
|
|||
|
dstPoints.put(1, 0, width, 0);
|
|||
|
dstPoints.put(0, 0, 0, 0);
|
|||
|
outputTexture = new Texture2D(width, height, TextureFormat.RGBA32, false);
|
|||
|
}
|
|||
|
|
|||
|
// Update is called once per frame
|
|||
|
void Update()
|
|||
|
{
|
|||
|
MatDisplay.SetCameraFoV(41.5f);
|
|||
|
|
|||
|
Image cameraImage = CameraDevice.Instance.GetCameraImage(Image.PIXEL_FORMAT.RGBA8888);
|
|||
|
|
|||
|
if (cameraImage != null)
|
|||
|
{
|
|||
|
if (cameraImageMat == null)
|
|||
|
{
|
|||
|
cameraImageMat = new Mat(cameraImage.Height, cameraImage.Width, CvType.CV_8UC4);
|
|||
|
}
|
|||
|
cameraImageMat.put(0, 0, cameraImage.Pixels);
|
|||
|
|
|||
|
Imgproc.cvtColor(cameraImageMat, greyMat, Imgproc.COLOR_RGB2GRAY);
|
|||
|
|
|||
|
|
|||
|
Imgproc.threshold(greyMat, stylizedMat, 69, 255, Imgproc.THRESH_BINARY);
|
|||
|
|
|||
|
|
|||
|
List<MatOfPoint> contourList = new List<MatOfPoint>();
|
|||
|
Mat hierarchy = new Mat();
|
|||
|
Imgproc.findContours(stylizedMat, contourList, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
|
|||
|
|
|||
|
//Imgproc.drawContours(cameraImageMat, contourList, -1, new Scalar(255,0,0), 2);
|
|||
|
|
|||
|
List<MatOfPoint> squareContours = new List<MatOfPoint>();
|
|||
|
foreach (var contour in contourList)
|
|||
|
{
|
|||
|
MatOfPoint2f contour2f = new MatOfPoint2f();
|
|||
|
contour.convertTo(contour2f, CvType.CV_32FC2);
|
|||
|
|
|||
|
double epsilon = 0.01f * Imgproc.arcLength(contour2f, true);
|
|||
|
MatOfPoint2f approx = new MatOfPoint2f();
|
|||
|
Imgproc.approxPolyDP(contour2f, approx, epsilon, true);
|
|||
|
|
|||
|
if (approx.toList().Count == 4)
|
|||
|
{
|
|||
|
Imgproc.drawContours(cameraImageMat, new List<MatOfPoint> { contour }, -1, new Scalar(255,0,0), 2);
|
|||
|
squareContours.Add(contour);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
MatOfPoint square = findSquare(squareContours);
|
|||
|
if (square == null)
|
|||
|
{
|
|||
|
MatDisplay.DisplayMat(cameraImageMat, MatDisplaySettings.FULL_BACKGROUND);
|
|||
|
// TODO: Maybe display cam first
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
Imgproc.drawContours(cameraImageMat, new List<MatOfPoint> { square }, -1, new Scalar(0, 255, 0), 2);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
MatDisplay.DisplayMat(cameraImageMat, MatDisplaySettings.FULL_BACKGROUND);
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
MatOfPoint findSquare (List<MatOfPoint> squareContours)
|
|||
|
{
|
|||
|
foreach (var outer_square in squareContours)
|
|||
|
{
|
|||
|
var outer_maxX = outer_square.toList().Max(point => point.x);
|
|||
|
var outer_maxY = outer_square.toList().Max(point => point.y);
|
|||
|
|
|||
|
var outer_minX = outer_square.toList().Min(point => point.x);
|
|||
|
var outer_minY = outer_square.toList().Min(point => point.y);
|
|||
|
|
|||
|
foreach (var inner_square in squareContours)
|
|||
|
{
|
|||
|
var inner_maxX = inner_square.toList().Max(point => point.x);
|
|||
|
var inner_maxY = inner_square.toList().Max(point => point.y);
|
|||
|
|
|||
|
var inner_minX = inner_square.toList().Min(point => point.x);
|
|||
|
var inner_minY = inner_square.toList().Min(point => point.y);
|
|||
|
|
|||
|
if (outer_minX < inner_minX &&
|
|||
|
outer_minY < inner_minY &&
|
|||
|
outer_maxX > inner_maxX &&
|
|||
|
outer_maxY > inner_maxY)
|
|||
|
{
|
|||
|
return outer_square;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|