본문 바로가기
학교 강의

OpenCV

by hoshi03 2023. 9. 13.

WebCamTextureToMatExample.cs update부분

// 영상을 mat데이터로 rgbmat에 저장
                Utils.webCamTextureToMat(webCamTexture, rgbaMat, colors);


                //새 매트릭스를 선언하고 거기에 받아온 rgbaMat를 저장
                Mat dst = new Mat();
                //이미지를 흑백으로 변경, 이미지가 흑백이 되면서 24bit에서 8bit로 변경
                Imgproc.cvtColor(rgbaMat, dst, Imgproc.COLOR_BayerBG2GRAY);
                Imgproc.threshold(rgbaMat, dst, 200, 255,Imgproc.THRESH_BINARY);


                //Imgproc.putText (rgbaMat, "W:" + rgbaMat.width () + " H:" + rgbaMat.height () + " SO:" + Screen.orientation, new Point (5, rgbaMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);

                Utils.matToTexture2D(dst, texture, colors);

 

빨간거 인식하기, core.inrange 부분의 scalar 값이 hue 값이다 

void Update()
{
    if (hasInitDone && webCamTexture.isPlaying && webCamTexture.didUpdateThisFrame)
    {
        Utils.webCamTextureToMat(webCamTexture, rgbaMat, colors);
        Mat hsv = new Mat();
        Mat dst = new Mat();
        Imgproc.cvtColor(rgbaMat, hsv, Imgproc.COLOR_BGR2HSV);
        Core.inRange(hsv, new Scalar(100, 140, 0), new Scalar(140, 255, 255), hsv);
        Core.bitwise_and(rgbaMat, rgbaMat, dst, hsv);
        Utils.matToTexture2D(dst, texture, colors);
    }
}

 

원 인식하기

void Update()
{
    if (hasInitDone && webCamTexture.isPlaying && webCamTexture.didUpdateThisFrame)
    {
        Utils.webCamTextureToMat(webCamTexture, rgbaMat, colors);
    
        Imgproc.cvtColor(rgbaMat, rgbaMat, Imgproc.COLOR_BGR2GRAY);

        Mat circles = new Mat();

        Imgproc.HoughCircles(rgbaMat, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 50, 150, 50, 10, 100);
        
        Point pt = new Point();

        for(int i = 0; i < circles.cols(); i++)
        {
            double[] data = circles.get(0, i);
            pt.x = data[0];
            pt.y = data[1]; 
            double rho = data[2];
            Imgproc.circle(rgbaMat, pt, (int)rho, new Scalar(255, 0, 0, 255), 5);
        }

        Utils.matToTexture2D(rgbaMat, texture, colors);
    }
}

빨파초 원을 인식하는걸 만들어보자!