C#opencv设置摄像头分辨率
时间: 2025-02-23 19:15:30 浏览: 47
### C# 中使用 OpenCV 设置摄像头分辨率
为了在 C# 中使用 OpenCV 库来设置摄像头的分辨率,通常会借助 Emgu CV 这个封装库。Emgu CV 是 OpenCV 的 .NET 封装版本,提供了更方便的操作接口。
下面是一个简单的例子展示如何设置摄像头分辨率为 1280x720:
```csharp
using System;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
class Program {
static void Main(string[] args) {
Capture capture = new Capture();
// Set camera properties using SetProperty method.
// Check if the property can be set successfully by verifying the return value.
double resultWidth = capture.SetCaptureProperty(CapProp.FrameWidth, 1280);
double resultHeight = capture.SetCaptureProperty(CapProp.FrameHeight, 720);
Console.WriteLine($"Set Width Result: {resultWidth}, Expected: 1280");
Console.WriteLine($"Set Height Result: {resultHeight}, Expected: 720");
Mat frame = new Mat();
while (true) {
try {
capture.Read(frame); // Read a new frame from video source
if (!frame.IsEmpty()) {
CvInvoke.Imshow("Camera", frame); // Display the frame in window named "Camera"
// Break loop when ESC key pressed
if (((int)CvInvoke.WaitKey(10)) == 27) break;
}
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
CvInvoke.DestroyAllWindows(); // Destroy all windows before exiting program
capture.Dispose(); // Release resources used by VideoCapture object
}
}
```
这段代码创建了一个 `Capture` 对象用于访问默认摄像设备,并尝试将其帧宽度和高度分别设为 1280 和 720 像素。需要注意的是并非所有的硬件都支持任意尺寸的调整;具体取决于所使用的摄像头型号及其驱动的支持情况[^1]。
阅读全文
相关推荐


















