Baumer工業相機堡盟工業相機如何通過YoloV8深度學習模型實現衛星圖像識別(C#代碼,UI界面版)
- 工業相機使用YoloV8模型實現水下魚類識別
- 工業相機通過YoloV8模型實現衛星圖像識別的技術背景
- 在相機SDK中獲取圖像轉換圖像的代碼分析
- 工業相機圖像轉換Bitmap圖像格式和Mat圖像重要核心代碼
- 本地文件圖像轉換Bitmap圖像格式和Mat圖像重要核心代碼
- Mat圖像導入YoloV8模型重要核心代碼
- 代碼實現演示(實現衛星圖像識別)
- 源碼下載鏈接
- 工業相機通過YoloV8模型實現衛星圖像識別的行業應用
- 關鍵技術細節
?
工業相機使用YoloV8模型實現水下魚類識別
本項目集成了 YOLOv8 檢測模型 與 C#圖形界面工具,實現了包括圖片、文件夾、視頻與攝像頭等多種輸入方式的實現衛星圖像識別功能。
Baumer工業相機堡盟相機是一種高性能、高質量的工業相機,可用于各種應用場景,如物體檢測、計數和識別、運動分析和圖像處理。
Baumer的萬兆網相機擁有出色的圖像處理性能,可以實時傳輸高分辨率圖像。此外,該相機還具有快速數據傳輸、低功耗、易于集成以及高度可擴展性等特點。
?
Baumer工業相機由于其性能和質量的優越和穩定,常用于高速同步采集領域,通常使用各種圖像算法來提高其捕獲的圖像的質量。
本文以Baumer工業相機作為案例進行演示,實現將工業相機的圖像或者本地圖像導入Yolo模型從而實現衛星圖像識別等功能。
工業相機通過YoloV8模型實現衛星圖像識別的技術背景
本文通過C#中實現一個簡單的UI界面,用于將YoloV8模型實現衛星圖像識別
用戶可以通過該界面執行以下操作:
-
轉換相機圖像為Mat圖像:通過YoloV8模型實現實現衛星圖像識別檢測
-
轉換本地圖像為mat圖像:通過YoloV8模型實現衛星圖像識別檢測
通過這個UI界面,用戶能夠在實時應用機器視覺數據處理時快速有效地進行操作,無需深入了解圖像數據的底層處理過程。這個簡單的介紹旨在為開發人員提供一個明確的方向,以便開始構建此類應用程序,并且該程序主要用于演示目的。
在相機SDK中獲取圖像轉換圖像的代碼分析
本文介紹使用Baumer工業相機,實現將圖像轉換為Bitmap圖像,再轉換Mat圖像,導入到Yolo模型進行推理,輸出實現衛星圖像識別的結果。
工業相機圖像轉換Bitmap圖像格式和Mat圖像重要核心代碼
//將相機內部圖像內存數據轉為bitmap數據
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap((int)mBufferFilled.Width, (int)mBufferFilled.Height,(int)mBufferFilled.Width,System.Drawing.Imaging.PixelFormat.Format8bppIndexed, (IntPtr)((ulong)mBufferFilled.MemPtr + mBufferFilled.ImageOffset));#region//Mono圖像數據轉換。彩色圖像數據轉換于此不同
System.Drawing.Imaging.ColorPalette palette = bitmap.Palette;
int nColors = 256;
for (int ix = 0; ix < nColors; ix++)
{uint Alpha = 0xFF;uint Intensity = (uint)(ix * 0xFF / (nColors - 1));palette.Entries[ix] = System.Drawing.Color.FromArgb((int)Alpha, (int)Intensity,(int)Intensity, (int)Intensity);
}
bitmap.Palette = palette;
#endregionstring strtime = DateTime.Now.ToString("yyyyMMddhhmmssfff");
string saveimagepath = pImgFileDir + "\\" + strtime + ".brw";//使用Bitmap格式保存
bitmap.Save(saveimagepath, System.Drawing.Imaging.ImageFormat.Bmp); //用bitmap轉換為mat
OpenCvSharp.Mat Matgray1 = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);
本地文件圖像轉換Bitmap圖像格式和Mat圖像重要核心代碼
C#環境下代碼如下所示:
if (imagePaths.Count() == 0)
{LoadImagePaths("test_img");
}string currentImagePath = imagePaths[currentImageIndex];// 顯示到pictureBoxA
pictureBoxA.Image.Dispose(); // 釋放上一張圖片資源,避免內存泄漏
pictureBoxA.Image = new Bitmap(currentImagePath);
image_path = currentImagePath;currentImageIndex = (currentImageIndex + 1) % imagePaths.Count;OnNotifyShowRecieveMsg("檢測中,請稍等……");
//textBox1.Text = "檢測中,請稍等……";
//pictureBox2.Image = null;
Application.DoEvents();image = new Mat(image_path);float ratio = Math.Min(1.0f * inpHeight / image.Rows, 1.0f * inpWidth / image.Cols);
int neww = (int)(image.Cols * ratio);
int newh = (int)(image.Rows * ratio);Mat dstimg = new Mat();
Cv2.Resize(image, dstimg, new OpenCvSharp.Size(neww, newh));Cv2.CopyMakeBorder(dstimg, dstimg, 0, inpHeight - newh, 0, inpWidth - neww, BorderTypes.Constant);
Mat圖像導入YoloV8模型重要核心代碼
C#環境下代碼如下所示:
// 定義 ONNX 模型的路徑
string onnxModelPath = "model/Detect_Satellite.onnx";
// 定義輸入圖像的形狀
OpenCvSharp.Size inputShape = new OpenCvSharp.Size(800, 800);
// 從 ONNX 模型文件加載網絡
if(net==null)net = CvDnn.ReadNetFromOnnx(onnxModelPath);string[] modelClassify = { "Expressway-Service-area", "Expressway-toll-station", "airplane", "airport", "baseballfield", "basketballcourt", "bridge","chimney","dam","golffield","groundtrackfield", "harbor", "overpass", "ship", "stadium", "storagetank" };if (imagePaths.Count() == 0)
{LoadImagePaths("test_img");
}string currentImagePath = imagePaths[currentImageIndex];// 顯示到pictureBoxA
pictureBoxA.Image.Dispose(); // 釋放上一張圖片資源,避免內存泄漏
pictureBoxA.Image = new Bitmap(currentImagePath);
image_path = currentImagePath;if (pictureBoxA.Image == null)
{return;
}
currentImageIndex = (currentImageIndex + 1) % imagePaths.Count;OnNotifyShowRecieveMsg("檢測中,請稍等……");Application.DoEvents();image = new Mat(image_path);dt1 = DateTime.Now;
// 調用識別圖像的函數,并傳入圖像路徑、閾值、網絡、輸入形狀和分類類別列表
//result_image = Recognize(image, 0.35, net, inputShape, modelClassify);
result_image = RecognizeMat(image, 0.35, net, inputShape, modelClassify);
// 獲取計算結束時間
dt2 = DateTime.Now;
// 顯示輸出的圖像
pictureBoxA.Image = new Bitmap(result_image.ToMemoryStream());// 顯示推理耗時時間
OnNotifyShowRecieveMsg("推理耗時:" + (dt2 - dt1).TotalMilliseconds + "ms");
static Mat RecognizeMat(Mat imgInput, double threshold, Net net, OpenCvSharp.Size inputShape, string[] modelClassify)
{using (Mat img = imgInput){int inpHeight = inputShape.Height; // 輸入圖像的高度int inpWidth = inputShape.Width; // 輸入圖像的寬度// 對圖像進行預處理,調整尺寸Mat image = img;float ratio = Math.Min(1.0f * inpHeight / image.Rows, 1.0f * inpWidth / image.Cols);int neww = (int)(image.Cols * ratio);int newh = (int)(image.Rows * ratio);//// 將圖像調整為模型需要的大小//Mat dstimg = new Mat();//Cv2.Resize(image, dstimg, new OpenCvSharp.Size(neww, newh));//Cv2.CopyMakeBorder(dstimg, dstimg, 0, inpHeight - newh, 0, inpWidth - neww, BorderTypes.Constant);//Mat BN_image = CvDnn.BlobFromImage(dstimg); // 將調整后的圖像轉換為Blob格式//// 配置圖片輸入數據 // 將 blob 設置為網絡的輸入//net.SetInput(BN_image);//// 從圖像生成用于網絡輸入的 blob//Mat blob = CvDnn.BlobFromImage(img, 1 / 255.0, inputShape, new Scalar(0, 0, 0), false);////Mat blob = CvDnn.BlobFromImage(img, 1.0 / 255.0, inputShape, new Scalar(0, 0, 0), true, false);// 將 blob 設置為網絡的輸入//net.SetInput(blob);//// 從圖像生成用于網絡輸入的 blobMat img0 = img;Mat blob0 = CvDnn.BlobFromImage(img0, 1 / 255.0, new OpenCvSharp.Size(inputShape.Width, inputShape.Height), swapRB: true, crop: false);net.SetInput(blob0);// 執行前向傳播獲取輸出Mat output = net.Forward();// 此處可能需要根據 C# 中 OpenCV 的特性來處理轉置操作output = ReshapeAndTranspose(output);// 獲取圖像的行數(高度)int height = img.Height;// 獲取圖像的列數(寬度)int width = img.Width;// 計算寬度的縮放因子double xFactor = (double)width / inputShape.Width;// 計算高度的縮放因子double yFactor = (double)height / inputShape.Height;// 初始化分類類別、得分和檢測框的列表List<string> classifys = new List<string>();List<float> scores = new List<float>();List<Rect> boxes = new List<Rect>();List<Double> maxVales = new List<Double>();List<OpenCvSharp.Point> maxloces = new List<OpenCvSharp.Point>();// 遍歷輸出的行for (int i = 0; i < output.Rows; i++){// 獲取當前行的檢測框數據using (Mat box = output.Row(i)){// 在框數據的特定范圍中找到最小值、最大值及其位置OpenCvSharp.Point minloc, maxloc;double minVal, maxVal;// Mat classes_scores = box.ColRange(4, 5);//GetArray(i, 5, classes_scores);// double curmates0 = box.At<float>(0);double curmates1 = box.At<float>(4);int collength = box.Cols;int rowlength = box.Rows;Mat curmates = box.ColRange(4, box.Cols);//Cv2.MinMaxLoc(box.ColRange(4, box.Cols), out minVal, out maxVal, out minloc, out maxloc);Cv2.MinMaxLoc(box.ColRange(4, box.Cols), out minVal, out maxVal, out minloc, out maxloc);int classId = maxloc.Y;if (classId == 0){// 獲取對應類別的得分 float score = (float)maxVal;// 如果得分大于閾值if (score > threshold){// 將得分添加到得分列表scores.Add(score);// 將類別添加到類別列表classifys.Add(modelClassify[classId]);// 獲取框的原始坐標float x = box.At<float>(0, 0);float y = box.At<float>(0, 1);float w = box.At<float>(0, 2);float h = box.At<float>(0, 3);// 計算調整后的坐標int xInt = (int)((x - 0.5 * w) * xFactor);int yInt = (int)((y - 0.5 * h) * yFactor);int wInt = (int)(w * xFactor);int hInt = (int)(h * yFactor);// 將調整后的框坐標添加到框列表boxes.Add(new Rect(xInt, yInt, wInt, hInt));}}}}// 執行非極大值抑制操作int[] indices;CvDnn.NMSBoxes(boxes, scores, 0.25f, 0.45f, out indices);// 遍歷非極大值抑制操作后的索引foreach (int i in indices){// 獲取對應的類別、得分和框string classify = classifys[i];float score = scores[i];Rect box = boxes[i];// 獲取框的坐標和尺寸// 在圖像上繪制矩形框Cv2.Rectangle(img, box, new Scalar(0, 255, 0), 3);// 生成類別和得分的標簽文本string label = $"{classify}: {score:F2}";// 在圖像上添加標簽文本Cv2.PutText(img, label, new OpenCvSharp.Point(box.X, box.Y - 10), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 255, 0), 2);}// 將圖像復制輸出返回Mat result_image0 = img.Clone();return result_image0;// 將處理后的圖像保存為文件// Cv2.ImWrite("result.jpg", img);}
}
代碼實現演示(實現衛星圖像識別)
源碼下載鏈接
C# WinForms工業相機+本地圖像 通過YoloV8實現衛星圖像識別
工業相機通過YoloV8模型實現衛星圖像識別的行業應用
# | 行業場景 | 業務痛點 | 衛星圖像特征 | 工業相機/傳感器角色 | YOLOv8 技術方案與效果 |
---|---|---|---|---|---|
1 | 電網無人機巡檢(衛星+航拍融合) | 大范圍輸電塔、絕緣子缺陷難以人工排查 | 0.1–0.3 m 分辨率 RGB+多光譜 | 無人機掛載工業級全局快門相機,實時回傳衛星拼圖 | 8.3.131 版 YOLOv8x 支持 10 K×10 K 大圖切片推理,mAP@0.5 0.89,單塔缺陷漏檢 <1 % |
2 | 港口船舶流量監控 | 傳統 AIS 存在盲區,需補充視覺確認 | 衛星可見光+SAR 圖像 | 地面站接收衛星數據,工業相機做二次驗證 | YOLOv8-tile 滑窗 2048×2048,overlap 0.3,檢測船只 50 ms/切片,準確率 94 % |
3 | 城市違建/屋頂光伏普查 | 人工巡查成本高,數據滯后 | 0.5 m 分辨率衛星圖 | 工業相機補拍低空細節,與衛星圖像配準 | YOLOv8s 四分類(違建/光伏/普通屋頂/空地),GeoJSON 輸出,精度 91 % |
4 | 災害應急(洪水、滑坡) | 需小時級災情評估 | Sentinel-2 10 m 多光譜 | 衛星下傳→工業 GPU 服務器 | YOLOv8 水體/滑坡檢測 30 min 內完成 500 km2 區域評估,召回率 93 % |
5 | 農田長勢與作物分類 | 精準農業需地塊級識別 | 無人機+衛星融合,RGB+NIR | 工業多光譜相機作為地面真值采集 | YOLOv8-seg 實例分割,玉米/水稻/大豆 IoU 0.85,指導變量施肥 |
6 | 森林火點早期發現 | 火點面積小,傳統 NDVI 誤報高 | 3.7 μm 中波紅外衛星圖 | 工業紅外相機做地面驗證 | YOLOv8 小目標檢測(火點 < 10 px),誤報率降低 70 % |
7 | 公路交通密度監控 | 大范圍擁堵實時可視化 | 衛星視頻 1 fps | 工業高速相機補拍地面驗證 | YOLOv8 車輛計數,單幀 2000+ 車輛,誤差 ?? % |
關鍵技術細節
大圖切片:YOLOv8.3.131 原生支持 --tile 2048 --overlap 0.25,無需手動裁剪即可處理 10 K×10 K 衛星圖。
多光譜/紅外兼容:官方已支持單通道/灰度圖自動復制 3 通道,無需改模型。
邊緣部署:Jetson Orin Nano + TensorRT-FP16,可實時處理 2 路 2048×2048 切片,功耗 15 W。
坐標輸出:后處理集成 rasterio + shapely,直接生成 GeoJSON,對接 GIS 系統。
合規:衛星原始影像按國家測繪法規加密存儲,檢測結果僅保留矢量坐標,72 h 內自動刪除原始柵格。