作為研究計算機視覺的一員,大家肯定對Intel大名鼎鼎的openCV系列計算機視覺庫耳熟能詳,對於很多人來說openCV甚至已經成為其項目研究不可缺少的一部分。但是,由於項目兼容性的要求、openCV的GUI功能不夠豐富等原因很多人希望能夠在C#環境中使用openCV。
在目前針對c#的計算機視覺庫主要有兩種,EmguCV和openCVSharp。
Emgucv的優勢在於不僅僅提供了計算機視覺函數接口並且提供了一系列界面控件接口,但目前只支持openCV1的書寫風格。
openCVSharp提供了openCV和openCV2兩種書寫風格,並且和openCV的imagewatch一樣,提供了一種簡單有效的調試工具Debugger Visualizer,具體使用可見一下網址
https://github.com/shimat/opencvsharp/wiki/Debugger-Visualizer。
在這里我的推薦是使用openCVSharp,在這里主要考慮的是協議方面的問題。opencv的協議是BSD協議,這是對開發者來說是相當友好的協議;網上常見的免費版EmguCV則是GUN協議,任何發表都需要至少公布你的源代碼;openCVSharp則是相對溫和多的LGUN協議,這個協議和QT是差不多相同的,甚至當你不使用自帶的DLL時,和openCV一樣是BSD協議(如果你對協議感興趣的話可以自行百度)。
具體配置方法可以自行百度,簡單來說就是添加相應的com組件(注意有一個com組件右鍵添加,只能手動放在相應的exe目錄下),然后添加相應的命名空間。
下面分別是c和c++風格的代碼,獲取相應位置像素值,速度由慢向快排列。
//c風格代碼
//方案一
IplImage img = new IplImage("baz.png", LoadMode.Color);
for (int y = 0; y < img.Height; y++) {
for (int x = 0; x < img.Width; x++) {
CvColor c = img[y, x];
img[y, x] = new CvColor() {
B = (byte)Math.Round(c.B * 0.7 + 10),
G = (byte)Math.Round(c.G * 1.0),
R = (byte)Math.Round(c.R * 0.0),
};
}
}
//方案二
IplImage img = new IplImage("baz.png", LoadMode.Color);
unsafe {
byte* ptr = (byte*)img.ImageData;
for (int y = 0; y < img.Height; y++) {
for (int x = 0; x < img.Width; x++) {
int offset = (img.WidthStep * y) + (x * 3);
byte b = ptr[offset + 0]; // B
byte g = ptr[offset + 1]; // G
byte r = ptr[offset + 2]; // R
ptr[offset + 0] = r;
ptr[offset + 1] = g;
ptr[offset + 2] = b;
}
}
}
方案三
IplImage img = new IplImage("baz.png", LoadMode.Color);
IntPtr ptr = img.ImageData;
for (int x = 0; x < image.Width; x++) {
for (int y = 0; y < image.Height; y++) {
int offset = (image.WidthStep * y) + (x * 3);
byte b = Marshal.ReadByte(ptr, offset + 0); // B
byte g = Marshal.ReadByte(ptr, offset + 1); // G
byte r = Marshal.ReadByte(ptr, offset + 2); // R
Marshal.WriteByte(ptr, offset, r);
Marshal.WriteByte(ptr, offset, g);
Marshal.WriteByte(ptr, offset, b);
}
}
//c++風格代碼
//方案一
Mat mat = new Mat("lenna.png", LoadMode.Color);
for (int y = 0; y < mat.Height; y++)
{
for (int x = 0; x < mat.Width; x++)
{
Vec3b color = mat.Get<Vec3b>(y, x);
byte temp = color.Item0;
color.Item0 = color.Item2; // B <- R
color.Item2 = temp; // R <- B
mat.Set<Vec3b>(y, x, color);
}
}
GenericIndexer (reasonably fast)
//方案二
Mat mat = new Mat("lenna.png", LoadMode.Color);
var indexer = mat.GetGenericIndexer<Vec3b>();
for (int y = 0; y < mat.Height; y++)
{
for (int x = 0; x < mat.Width; x++)
{
Vec3b color = indexer[y, x];
byte temp = color.Item0;
color.Item0 = color.Item2; // B <- R
color.Item2 = temp; // R <- B
indexer[y, x] = color;
}
}
方案三
Mat mat = new Mat("lenna.png", LoadMode.Color);
MatOfByte3 mat3 = new MatOfByte3(mat); // cv::Mat_<cv::Vec3b>
var indexer = mat3.GetIndexer();
for (int y = 0; y < mat.Height; y++)
{
for (int x = 0; x < mat.Width; x++)
{
Vec3b color = indexer[y, x];
byte temp = color.Item0;
color.Item0 = color.Item2; // B <- R
color.Item2 = temp; // R <- B
indexer[y, x] = color;
}
}
最后,openCVSharp下載地址為https://github.com/shimat/opencvsharp/releases。如果,無法下載私信我,我可以提供2.4.10、3.1和3.2三種版本。
---------------------
作者:小立1991
來源:CSDN
原文:https://blog.csdn.net/qq_21400315/article/details/52451941
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!