#define VIDEO
using OpenCvSharp;
using System;
namespace CaptureSeaLine
{
class Program
{
static string rtspUrl = "rtsp://xxxx";
static void Main(string[] args)
{
VideoCapture Capture = new VideoCapture(rtspUrl);
Cv2.NamedWindow("test", WindowFlags.Normal);
Cv2.NamedWindow("org", WindowFlags.Normal);
#if VIDEO
while (Capture.IsOpened())
{
Mat img = new Mat();
try
{
Capture.Read(img);
}
catch
{
Console.WriteLine( "no signal");
}
Mat imgGray = new Mat();
Cv2.CvtColor(img, imgGray, ColorConversionCodes.BGR2GRAY);//灰度化
Cv2.MedianBlur(imgGray, imgGray, 9);//中值滤波
Cv2.Threshold(imgGray, imgGray, 0, 255, ThresholdTypes.Otsu);//二值化
//Cv2.Threshold(imgGray, imgGray, 100, 250, ThresholdTypes.Tozero);
Cv2.Canny(imgGray, imgGray, 10, 50, 3);//边沿检测
LineSegmentPoint[] linePoint = Cv2.HoughLinesP(imgGray, 1.0, Cv2.PI / 180, 150, 300, 100);//霍夫直线检测
//划线
for (int i = 0; i < linePoint.Length; i++)
{
Point p1 = linePoint[i].P1;
Point p2 = linePoint[i].P2;
Cv2.Line(img, p1, p2, Scalar.Green, 4);
}
//Cv2.ImShow("test", imgGray);
Cv2.ImShow("org", img);
Cv2.WaitKey(1);
}
#else
Mat img = Cv2.ImRead(@"D:\C#\Code\CaptureSeaLine\bin\Debug\SeaLine.png", ImreadModes.Color);
Mat imgGray = new Mat();
Cv2.CvtColor(img, imgGray, ColorConversionCodes.BGR2GRAY);
Cv2.MedianBlur(imgGray, imgGray, 9);
Cv2.Threshold(imgGray, imgGray, 0, 255, ThresholdTypes.Otsu);
//Cv2.Threshold(imgGray, imgGray, 100, 250, ThresholdTypes.Tozero);
Cv2.Canny(imgGray, imgGray, 10, 50, 3);
LineSegmentPoint[] linePoint = Cv2.HoughLinesP(imgGray, 1.0, Cv2.PI / 180, 200, 300, 100);
for (int i = 0; i < linePoint.Length; i++)
{
Point p1 = linePoint[i].P1;
Point p2 = linePoint[i].P2;
Cv2.Line(img, p1, p2, Scalar.Green, 4);
}
Cv2.ImShow("test", imgGray);
Cv2.ImShow("org", img);
Cv2.WaitKey(0);
#endif
}
}
}
OpencvSharp检测海天线
最新推荐文章于 2022-08-27 12:59:21 发布