帧差法监测运动,运动区域设为黑色及非运动区域设为黑色(二版)

OpenCV打开相机检测运动,Unity实时传入虚拟相机视频

OpenCV Dll:

#include<opencv2/opencv.hpp>
using namespace cv;

#define EXPORT_API __declspec(dllexport)

int m_width = 640;
int m_height = 480;

VideoCapture my_camera;
Mat temp, my_frameBGR;
Mat background;
int count = 0;
int test = 0;

Mat ProjectorPic, ProjectorPicA, ProjectorPicB;
extern "C" __declspec(dllexport)
double ComputPerFrame_From_Unity(void* image_data)
{
	Mat image(480, 640, CV_8UC3);
	memcpy(image.data, image_data, m_width * m_height * 3 * sizeof(uchar));
	cvtColor(image, image, CV_BGR2RGB);
	flip(image, image, 1);
	flip(image, image, -1);
	//imshow("虚拟相机", image);
	
	ProjectorPic = image.clone();
	ProjectorPicA = image.clone();
	ProjectorPicB = image.clone();
	if (image.empty())
	{
		//	cout << "Unable to load Image" << endl;
		return 1;
	}
}

Mat MoveDetect(Mat background, Mat tex)
{
	//将background和img转为灰度图
	Mat result = tex.clone();
	Mat gray1, gray2;
	cvtColor(background, gray1, CV_BGR2GRAY);
	cvtColor(tex, gray2, CV_BGR2GRAY);

	//进行canny边缘检测 
	Canny(background, background, 0, 30, 3);

	//将background和img做差;对差值图diff进行阈值化处理
	Mat diff;
	absdiff(gray1, gray2, diff);
	//imshow("absdiss", diff);
	threshold(diff, diff, 50, 255, CV_THRESH_BINARY);
	//imshow("threshold", diff);

	//二值化后使用中值滤波+膨胀
	Mat element = getStructuringElement(MORPH_RECT, Size(11, 11));
	medianBlur(diff, diff, 5);//中值滤波
	//imshow("medianBlur", diff);
	dilate(diff, diff, element);
	//blur(diff, diff, Size(10, 10)); //均值滤波
	//imshow("dilate", diff);

	//查找并绘制轮廓
	vector<vector<Point>> contours;
	vector<Vec4i> hierarcy;
	findContours(diff, contours, hierarcy, CV_RETR_EXTERNAL, CHAIN_APPROX_NONE); //查找轮廓
	vector<Rect> boundRect(contours.size()); //定义外接矩形集合
	//drawContours(img2, contours, -1, Scalar(0, 0, 255), 1, 8);  //绘制轮廓

	//查找正外接矩形
	int x0 = 0, y0 = 0, w0 = 0, h0 = 0;
	double Area = 0, AreaAll = 0;
	Mat ProjectorPicBROI;
	if (contours.size() == 0)
	{
		if (ProjectorPicA.empty())
		{
			//	cout << "Unable to load Image" << endl;
			return result;
		}
		else
		{
			rectangle(ProjectorPicB, Point(x0, y0), Point(x0 + ProjectorPicB.cols, y0 + ProjectorPicB.rows), Scalar(0, 255, 0), 2, 8);       //绘制第i个外接矩形
			ProjectorPicBROI = ProjectorPicB(Rect(Point(x0, y0), Point(x0 + ProjectorPicB.cols, y0 + ProjectorPicB.rows)));
			ProjectorPicBROI = { Scalar(0, 0, 0) };
			imshow("ProjectorImgA", ProjectorPicA);
			imshow("ProjectorImgB", ProjectorPicB);
		}
	}
	else
	{
		for (int i = 0; i < contours.size(); i++)
		{
			boundRect[i] = boundingRect((Mat)contours[i]); //查找每个轮廓的外接矩形
			x0 = boundRect[i].x;
			y0 = boundRect[i].y;
			w0 = boundRect[i].width;
			h0 = boundRect[i].height;

			//计算面积
			double Area = contourArea(contours[i]);//计算第i个轮廓的面积
			AreaAll = Area + AreaAll;

			Mat CameraPicMask, CameraImg, ProjectorPicMask, ProjectorImg;
			Mat ProjectorPicClone;
			Mat CameraImageROI, ProjectorImageROI;
			//筛选
			if (w0 > 50 && h0 > 50)
			{
				//ProjectorPic = imread("F:\\Unity\\Unity工程\\534TTest\\Assets\\Textures\\0003.jpeg");
				ProjectorPic.copyTo(ProjectorPicClone);

				rectangle(result, Point(x0, y0), Point(x0 + w0, y0 + h0), Scalar(0, 255, 0), 2, 8);       //绘制第i个外接矩形
				CameraImageROI = { Scalar(0, 0, 0) };

				rectangle(ProjectorPic, Point(x0, y0), Point(x0 + w0, y0 + h0), Scalar(0, 255, 0), 2, 8);
				ProjectorImageROI = ProjectorPic(Rect(Point(x0, y0), Point(x0 + w0, y0 + h0)));
				ProjectorImageROI = { Scalar(0, 0, 0) };
				imshow("ProjectorImgB", ProjectorPic);


				/*CameraPicMask = Mat::zeros(my_frameBGR.size(), CV_8UC1);
				Rect rect = (Rect(Point(x0, y0), Point(x0 + w0, y0 + h0)));
				CameraPicMask(rect).setTo(255);
				my_frameBGR.copyTo(CameraImg, CameraPicMask);*/

				Rect rect = (Rect(Point(x0, y0), Point(x0 + w0, y0 + h0)));
				ProjectorPicMask = Mat::zeros(ProjectorPic.size(), CV_8UC1);
				ProjectorPicMask(rect).setTo(255);
				ProjectorPicClone.copyTo(ProjectorImg, ProjectorPicMask);

				//imshow("CameraImgB", CameraImg);
				//imshow("CameraPicMask", CameraPicMask);
				imshow("ProjectorImgA", ProjectorImg);
				//imshow("ProjectorPicMask", ProjectorPicMask);
			}

			//文字输出
			Point org(10, 35);
			if (i >= 1 && AreaAll >= 19600)
			{
				putText(result, "Is Blocked ", org, CV_FONT_HERSHEY_SIMPLEX, 0.8f, Scalar(0, 255, 0), 2);
				test = 1;
			}

			else
				test = 0;
	    }
	}
	
	return result;
}

extern "C" bool  EXPORT_API openCamera()
{
	bool my_open = false;
	while (!my_camera.isOpened())
	{
		//std::cout << "Cannot open the camera!" << std::endl;
		my_camera.open(0);
	}
	my_camera.set(CV_CAP_PROP_FRAME_WIDTH, m_width);
	my_camera.set(CV_CAP_PROP_FRAME_HEIGHT, m_height);
	if (my_camera.isOpened())
	{
		my_open = true;
	}
	return my_open;
}

extern "C" void   EXPORT_API recieveFrame(uchar* texturePtr)
{

	Mat my_frameBGR;
	Mat my_frameRBG;
	Mat result;

	my_camera >> my_frameBGR;
	if (my_frameBGR.data)
	{
		cvtColor(my_frameBGR, my_frameRBG, CV_BGR2RGB);
		memcpy(texturePtr, my_frameRBG.data, my_frameRBG.cols*my_frameRBG.rows*my_frameRBG.channels()*sizeof(uchar));

		count++;
		if (count == 1)

			/*background = my_frameBGR.clone();
			result = MoveDetect(background, my_frameBGR);*/

			result = MoveDetect(my_frameBGR, my_frameBGR);
		else
			result = MoveDetect(temp, my_frameBGR);
		temp = my_frameBGR.clone();

		//imshow("CameraImg", result);
	}
}

extern "C" bool EXPORT_API num()
{
	if (test != 0)
	{
		return true;
	}
	else
		return false;
}

extern "C" void EXPORT_API Quit()
{
	destroyAllWindows();
	my_camera.release();
}

Unity C#获取虚拟相机视角视频:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.InteropServices;

using UnityEngine.UI;
public class ComputePerFrameFromUnity : MonoBehaviour
{
    public Camera maincamera;
    RenderTexture rt;
    Texture2D t2d;
    public GameObject p1;

    public byte[] imageData;

    [DllImport("3DLL")]
    private static extern double ComputPerFrame_From_Unity(byte[] image_data);

    [DllImport("3DLL")]
    private static extern void Quit();

    // Use this for initialization
    void Start()
    {
        t2d = new Texture2D(640, 480, TextureFormat.RGB24, false);
        rt = new RenderTexture(640, 480, 30);
        maincamera.targetTexture = rt;

    }

    // Update is called once per frame
    void Update()
    {
        p1.GetComponent<Renderer>().material.mainTexture = rt;
        RenderTexture.active = rt;
        t2d.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
        t2d.Apply();
        RenderTexture.active = null;
        imageData = t2d.GetRawTextureData();

    }
    private void LateUpdate()
    {
        ComputPerFrame_From_Unity(imageData);
    }
    private void OnApplicationQuit()
    {
        Quit();
    }
}

Unity C#接收VS OpenCV真实相机画面 :

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;//用c++中dll文件需要引入  

public class GetCameraImgFromVS : MonoBehaviour
{

    [DllImport("3DLL")]
    public static extern bool openCamera();

    [DllImport("3DLL")]
    public static extern void recieveFrame(byte[] imageData);

    [DllImport("3DLL")]
    public static extern bool num();

    [DllImport("3DLL")]
    private static extern double ComputPerFrame_From_Unity(byte[] image_data);

    private bool IsOpen = false;
    public byte[] imageData;
    private int Width = 640;
    private int Length = 480;
    Texture2D tex;

    // Use this for initialization
    void Start()
    {
        //open one camera
        IsOpen = openCamera();
        if (IsOpen)
        {
            imageData = new byte[Length * Width * 3];
            tex = new Texture2D(Width, Length, TextureFormat.RGB24, false);
        }
    }

    // Update is called once per frame
    void Update()
    {
        //update one camera
        if (IsOpen)
        {
            recieveFrame(imageData);
            tex.LoadRawTextureData(imageData);
            tex.Apply();
            GetComponent<Renderer>().material.mainTexture = tex;
        }

        if (num())
            print(num());
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值