巴拉巴拉一下
前幾天購買了一個USB雙目相機如下,然後研究了一下怎麼使用,簡要記錄一下自己的使用過程;
相機的鏈接如下:https://item.taobao.com/item.htm?spm=a1z09.2.0.0.76522e8dAWEjef&id=556150206198&_u=q2nok360f3af
1. 參數
0、購買的相機焦距f=6mm
(定焦鏡頭,手動對焦);
1、雙目相機基線距離:B=35mm-169mm
;
2、主要參數如下:像素尺寸3.75um;UVC協議攝像頭(免驅);圖像壓縮輸出格式:MJPG或YUY2可選,MJPG支持最大幀率60,YUY2支持最大幀率10,圖像分辨率可自選
2. 使用
這裏使用opencvsharp來做驅動測試,設置屬性如下:
1、Width設置爲480,Height設置爲1280(選1280×480分辨率);
2、採集圖像壓縮格式設置爲MJPG;
3、圖像採集幀率設置爲60;
按照如上思路,測試程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
namespace cvsharptest
{
class Program
{
static void Main(string[] args)
{
Mat frame = new Mat();
VideoCapture cap = new VideoCapture(1);
cap.Set(CaptureProperty.Fps, 60); //相機採集幀率設置60fps
cap.Set(CaptureProperty.FrameHeight, 480); //圖像高度設置爲480
cap.Set(CaptureProperty.FrameWidth, 1280); //圖像寬度設置爲1280
string fourcc_start = cap.Get(CaptureProperty.FourCC).ToString();
Console.WriteLine("fourcc_start: " + fourcc_start); //獲取默認設置的圖像採集格式
cap.Set(CaptureProperty.FourCC, 1196444237); //設置圖像採集格式爲MJPG
string fourcc_last = cap.Get(CaptureProperty.FourCC).ToString();
double height = cap.Get(CaptureProperty.FrameHeight);
double width = cap.Get(CaptureProperty.FrameWidth);
double fps = cap.Get(CaptureProperty.Fps);
int delay = (int)(1000 / fps);
Console.WriteLine("fourcc_last: " + fourcc_last);
Console.WriteLine("height: " + height.ToString());
Console.WriteLine("width: " + width.ToString());
Console.WriteLine("fps: " + fps.ToString());
while (true)
{
cap.Read(frame);
Cv2.ImShow("video", frame);
Cv2.WaitKey(delay);
}
}
}
}
3. 效果
- MJPG採集的圖像:幀率60,採集的圖像足夠快,沒有延遲:
- YUY2格式採集的圖像:10fps,延時特別明顯,用的很難受:
4. 需要注意的
1、如下圖(opencvsharp使用videocapture類所有參數默認沒有值,使用時需要自己賦值來設定相機參數);
fourcc是編解碼器的四字符代碼的意思,相機打開後默認設置爲YUY2圖像格式,如fourcc_start;剛開始一直沒注意到圖像輸出格式的問題
fourcc_start = 844715353; //十進制數
fourcc_start = 0x32595559; //十六進制
//對應的ASCII碼錶值爲:
fourcc_start = "2YUY" ; //即默認格式爲YUY2
//根據如上分析,設置圖像爲MJPG格式
cap.Set(CaptureProperty.FourCC, 1196444237); //設置圖像採集格式爲MJPG
//fourcc應該設置爲:
fourcc_last = "GPJM";
//轉換成16進制ASCII碼
fourcc_last = 0x4750 4A4D
//轉換爲十進制數,即可寫入參數:
fourcc_last = 1196444237; //十進制數
2、使用過程中,發現fps
參數也需要設置,默認爲0,可以設置採樣頻率爲60,才能正常採集圖像,默認圖像大小爲640×240,需要更改爲自己想要的圖像尺寸;
5. 其他
1、另一種方法: 商家提供的參考資料裏使用的是Windows SDK中的Direct Show組件實現圖像的採集與顯示,Direct Show組件是Direct X的組件之一,是用來開發高性能圖形,聲音,輸入,輸出,網絡遊戲的編程接口,DirectShow是基於COM組件的,DirectShow組件使用C++實現,C#的話也有對應的封裝,C#封裝的DirectShow鏈接如下:http://directshownet.sourceforge.net/,MSDN中關於DirectShow組件描述鏈接:https://docs.microsoft.com/zh-cn/windows/win32/directshow/directshow?redirectedfrom=MSDN
2、考慮了方便快捷性,還是使用opencv/emgucv/opencvsharp中自帶的VideoCapture類來操作設置攝像頭參數速度比較快;
3、前面也使用了opencv的一段程序來測試攝像頭工作模式,發現默認是MJPG輸出,而不是YUY2,但是在opencvsharp/emgucv中卻是默認YUY2輸出,暫時不知道爲什麼不同;
4、攝像頭正常驅動後,便可以進一步編寫圖像處理程序了,O(∩_∩)O哈哈~;
參考
1、https://blog.csdn.net/weicao1990/article/details/53379881
2、https://blog.csdn.net/aolveyu0182/article/details/101891794?depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-2&utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-2
3、https://blog.csdn.net/kyjl888/article/details/69367912