摘要: 以opencv驱动摄像头, 并处理图像, 将结果传递给C#, 不涉及图像数据Mat的传递. 工程主体为C#, 封装opencv为dll, 进行调用. 开发环境用VS2019
文章目录
系统简化: 为了能够更具体地描述openCV C++与C#混合编程, 对系统进行简化. 目标是用简单的C#窗体程序, 读取摄像头, 并显示原始图像, 算法处理后, 显示结果图像. 不涉及图像数据传递, 也就无法在窗体中重绘图像, 显示窗口仍用opencv的HighGUI模块.
openCV 程序设计
全局变量
由于不传递图像Mat, openCV程序应在作用域内处理图像, 就需要用到全局变量. 主要定义有:
- VideoCapture
- frame
摄像头初始化
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
- 更改项目属性, 设置
配置类型
为 dll, 如图. 然后右键生成
或Ctrl+Shift+B
, 即可在输出目录看到dll文件 - 由于该dll依赖于opencv库, 所以还应包含opencv的dll. 如果是独立模块, 则需包括所有调用的; 如果是集合成了world文件, 就只需包含"opencv_world411.dll"即可. 参见博客: 封装opencv的函数成dll,独立调用
C#窗体程序
建立窗体项目
注意不要选错类型, 具体步骤可参考微软官方教程: Create a Windows Forms app in Visual Studio with C#
导入dll
如同导出声明一样, 需要特定的导入格式
// 初始化摄像头
[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
调用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模块不存在: 将两个dll都放入C#的生成目录. 参见
生成dll
小节的参考文献: 封装opencv的函数成dll,独立调用 - dll格式无效: 将调试平台设置为x64, 因为openCV只支持64位. 参见博客: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
- HighGUI窗体无响应: 设置为主线程, 即在
imshow
后追加waitKey(1)
, 参见博客: OpenCV使用cv::imshow在子线程中更新图片不刷新 - 打不开视频:因为C#的string无法传给C++的string,只能将C#中的string路径传给C++中的char*