摘要: 以opencv驱动摄像头, 并处理图像, 将结果传递给C#, 不涉及图像数据Mat的传递. 工程主体为C#, 封装opencv为dll, 进行调用. 开发环境用VS2019
系统简化: 为了能够更具体地描述openCV C++与C#混合编程, 对系统进行简化. 目标是用简单的C#窗体程序, 读取摄像头, 并显示原始图像, 算法处理后, 显示结果图像. 不涉及图像数据传递, 也就无法在窗体中重绘图像, 显示窗口仍用opencv的HighGUI模块.
由于不传递图像Mat, openCV程序应在作用域内处理图像, 就需要用到全局变量. 主要定义有:
void initCamera(int cameraIndex, double frameWidth, double frameHeight, double fps)
{
camera.open(cameraIndex);
if(!camera.isOpened())
{
cerr << "Couldn't open camera." << endl;
}
camera.set(cv::CAP_PROP_FRAME_WIDTH, frameWidth); // 宽度
camera.set(cv::CAP_PROP_FRAME_HEIGHT, frameHeight); // 高度
camera.set(cv::CAP_PROP_FPS, fps); // 帧率
}
注意备份, 谨慎更改
)void setCameraParams(double brightness, double contrast, double saturation, double hue, double gain, double exposure)
{
//打印摄像头参数
cout << "\n参数修改前:" << endl;
printf("brightness = %.2f\n", camera.get(cv::CAP_PROP_BRIGHTNESS));
printf("contrast = %.2f\n", camera.get(cv::CAP_PROP_CONTRAST));
printf("saturation = %.2f\n", camera.get(cv::CAP_PROP_SATURATION));
printf("hue = %.2f\n", camera.get(cv::CAP_PROP_HUE));
printf("gain = %.2f\n", camera.get(cv::CAP_PROP_GAIN));
printf("exposure = %.2f\n", camera.get(cv::CAP_PROP_EXPOSURE));
// 修改摄像头参数
camera.set(cv::CAP_PROP_BRIGHTNESS, brightness); // 亮度
camera.set(cv::CAP_PROP_CONTRAST, contrast); // 对比度
camera.set(cv::CAP_PROP_SATURATION, saturation); // 饱和度
camera.set(cv::CAP_PROP_HUE, hue); // 色调
camera.set(cv::CAP_PROP_GAIN, gain); // 增益
camera.set(cv::CAP_PROP_EXPOSURE, exposure); // 曝光度
//打印摄像头参数
cout << "\n参数修改后:" << endl;
printf("brightness = %.2f\n", camera.get(cv::CAP_PROP_BRIGHTNESS));
printf("contrast = %.2f\n", camera.get(cv::CAP_PROP_CONTRAST));
printf("saturation = %.2f\n", camera.get(cv::CAP_PROP_SATURATION));
printf("hue = %.2f\n", camera.get(cv::CAP_PROP_HUE));
printf("gain = %.2f\n", camera.get(cv::CAP_PROP_GAIN));
printf("exposure = %.2f\n", camera.get(cv::CAP_PROP_EXPOSURE));
}
void readFrame()
{
camera >> frame;
cv::imshow("camera", frame);
cv::waitKey(1);
}
void processingImage()
{
cv::cvtColor(frame, frame, cv::COLOR_BGR2GRAY);
cv::Canny(frame, frame, 100, 200);
cv::imshow("processor", frame);
cv::waitKey(1);
}
void closeCamera(string winName)
{
if(winName == "camera")
cv::destroyWindow("camera");
else if(winName == "processor")
cv::destroyWindow("processor");
else
;// do nothing
}
由于是导出为dll, 需要特别的语法支持
extern "C" __declspec(dllexport) void initCamera(int cameraIndex, double frameWidth, double frameHeight, double fps);
extern "C" __declspec(dllexport) void readFrame();
extern "C" __declspec(dllexport) void processingImage();
extern "C" __declspec(dllexport) void closeCamera(string winName);
配置类型
为 dll, 如图. 然后右键生成
或Ctrl+Shift+B
, 即可在输出目录看到dll文件注意不要选错类型, 具体步骤可参考微软官方教程: Create a Windows Forms app in Visual Studio with C#
如同导出声明一样, 需要特定的导入格式
// 初始化摄像头
[DllImport("ImageTracker.dll", EntryPoint = "initCamera", CallingConvention = CallingConvention.Cdecl)]
public static extern void initCamera(int cameraIndex, double frameWidth, double frameHeight, double fps);
有博客提到, 用reference的方法添加dll, 未尝试. Visual Studio 在 C# 项目添加动态链接库 dll
完整可运行项目已经打包:Project_CPP_CSharp.zip 需要在VSPropertySheet中适配自己的opencv环境
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Camera
{
public partial class FormCamera : Form
{
// 初始化摄像头
[DllImport("ImageProcessing.dll", EntryPoint = "initCamera", CallingConvention = CallingConvention.Cdecl)]
public static extern void initCamera(int cameraIndex, double frameWidth, double frameHeight, double fps);
// 读取图像并显示
[DllImport("ImageProcessing.dll", EntryPoint = "readFrame", CallingConvention = CallingConvention.Cdecl)]
public static extern void readFrame();
// 处理图像并显示
[DllImport("ImageProcessing.dll", EntryPoint = "processingImage", CallingConvention = CallingConvention.Cdecl)]
public static extern void processingImage();
// 关闭窗口
[DllImport("ImageProcessing.dll", EntryPoint = "closeCamera", CallingConvention = CallingConvention.Cdecl)]
public static extern void closeCamera(string winName);
public FormCamera()
{
InitializeComponent();
ImageProcessing();
}
private void ImageProcessing()
{
initCamera(0, -1, -1, -1);
for(int i = 0; i <= 10000; i++)
{
readFrame();
processingImage();
}
closeCamera("camera");
closeCamera("processor");
}
}
}
生成dll
小节的参考文献: 封装opencv的函数成dll,独立调用imshow
后追加waitKey(1)
, 参见博客: OpenCV使用cv::imshow在子线程中更新图片不刷新