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/Ship_detection.onnx";
// 定義輸入圖像的形狀
OpenCvSharp.Size inputShape = new OpenCvSharp.Size(640, 640);
// 從 ONNX 模型文件加載網絡
if(net==null)net = CvDnn.ReadNetFromOnnx(onnxModelPath);string[] modelClassify = { "BULK CARRIER", "CONTAINER SHIP", "GENERAL CARGO", "OIL PRODUCTS TANKER", "PASSENGERS SHIP", "TANKER", "TRAWLER", "TUG", "VEHICLES CARRIER", "YACHT"};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 實現輪船檢測識別的 6 大行業落地場景
(全部來自 2024-2025 年公開源碼、專利及論文)
行業/場景 | 業務痛點 | 工業相機/硬件形態 | YOLOv8 技術方案 & 關鍵指標 | 公開源碼/數據集 |
---|---|---|---|---|
① 港口船舶流量監控 | 人工統計誤差大、AIS 盲區 | 塔架 4K/8K 云臺工業相機 30× 變焦 | YOLOv8-tile 2048×2048 切片推理,overlap 0.3,檢測 50 ms/切片,準確率 94 % | GitHub: ships_detection671 |
② SAR 衛星艦船檢測 | 圖像分辨率低,艦船占比小 | 機載/星載 SAR → 工業 GPU 服務器 | 改進 YOLOv8:增加 4× 下采樣層 + EMA 注意力 + WIoU 損失,SSDD 數據集 mAP@0.5 提升至 91.2 % | |
③ 無人機海岸巡檢 | 小人-大船比例差異大,小目標易漏 | 無人機掛載 5 MP 全局快門相機 + RTK | SF-YOLO(YOLOv8 改進)+ 空間信息感知模塊,Tiny-Person 數據集 mAP 94.9 %,模型僅 3 M | |
④ 河道/船閘過閘管理 | 夜航、逆光、大霧干擾 | 星光級 2 MP 工業相機 + 激光補光 | YOLOv8n 量化 3.1 MB,RK3588 邊緣盒 40 ms/幀,誤報 <2 % | CSDN 博客源碼 |
⑤ 海上風電運維 | 運維船靠近風機需實時預警 | 塔架 8 K 可見光 + 紅外雙光相機 | YOLOv8-seg 實例分割,船體 IoU 0.85,2 s 內聯動聲光報警 | 中國光學期刊網論文 |
⑥ 海關緝私/邊防 | 高速快艇、夜間偷渡 | 固定式 12 MP 長焦工業相機陣列 | YOLOv8 + DeepSort 跟蹤,速度 0.5 m/s 可測,軌跡自動回傳指揮中心 | GitHub: ships_detection671 |
關鍵技術細節
小目標增強
? 在 YOLOv8 中新增 4× 下采樣特征圖(160×160)+ EMA 注意力,顯著改善 <10 px 小艦船檢測。
輕量化部署
? Jetson Orin Nano INT8 量化后模型 5 MB,640×640 輸入 25 ms/幀。
多源數據融合
? SAR 圖像與可見光工業相機結果級聯,提升全天候識別魯棒性。
合規
? 港口/邊防場景需在監控區域張貼告示,原始視頻 72 h 自動覆蓋,符合《數據安全法》。
一鍵體驗
? 完整源碼 + 訓練數據集:GitHub VisionMillionDataStudio/ships_detection671