private Mat Compressing(Mat SourceMat, double intensity) { ImageEncodingParam param = new ImageEncodingParam(ImwriteFlags.JpegQuality, checked ((byte)Convert.ToInt32((1.0 - intensity) * 100.0))); byte[] buffer = SourceMat.ImEncode(".jpg", param); return(Mat.ImDecode(buffer)); }
static void Main(string[] args) { Mat img = new Mat(new Size(640, 480), MatType.CV_8UC3); bool save; ImageEncodingParam[] prms = new ImageEncodingParam[] { new ImageEncodingParam(ImwriteFlags.JpegQuality, 100), new ImageEncodingParam(ImwriteFlags.JpegProgressive, 1) }; save = Cv2.ImWrite("CV.jpeg", img, prms); Console.WriteLine(save); }
public static byte[] PackImg(IRHeader header, ndarray img, int quality = 95, string img_fmt = ".jpg") { int[] encodeParams = null; OpenCvSharp.ImageEncodingParam imageEncodingParam = new ImageEncodingParam(ImwriteFlags.JpegQuality, quality); if (img_fmt.ToLower() == ".jpg" || img_fmt.ToLower() == ".jpeg") { encodeParams = new int[] { } } ; Cv2.ImEncode(img_fmt, new Mat(img.GetMemPtr()), out var buf, imageEncodingParam); return(Pack(header, buf)); } }
/// <summary> /// Сохранение первого кадра при возникновении простоя /// </summary> /// <param name="time">Время начала простоя</param> /// <param name="curFrame">Кадр с простоем</param> public static void SaveImg(DateTime time, Mat curFrame, bool downtimeEnd = false) { var folderName = "Сохраненные кадры работы"; if (!Directory.Exists(folderName)) { Directory.CreateDirectory(folderName); } var date = time.ToShortDateString(); folderName += "\\" + time.ToShortDateString(); if (!Directory.Exists(folderName)) { Directory.CreateDirectory(folderName); } var n = new ImageEncodingParam(ImwriteFlags.JpegOptimize, 100); var s = time.ToLongTimeString().Replace(':', '.'); Cv2.ImWrite($"{folderName}\\{s} ({(!downtimeEnd ? "Начало" : "Конец")} простоя).jpeg", curFrame, n); }
public static int Main(string[] args) { SerialPort serial; try { IEnumerable <PortInfo> ports = PortInfo.GetPorts(); string port = ports.FirstOrDefault((pi) => pi.Description == serialDevice)?.DeviceID; if (port != null) { serial = new SerialPort(port, baudRate); } else { throw new IOException("Could not connect to serial port."); } } catch (Exception ex) { Console.WriteLine(ex); return(1); } Console.WriteLine(); TcpListener listener = new TcpListener(IPAddress.Any, 5050); re: try { listener.Start(); Console.WriteLine("Listening " + listener.LocalEndpoint); using (var client = listener.AcceptTcpClient()) { listener.Stop(); Console.WriteLine("Connection established " + client.Client.RemoteEndPoint); using (NetworkStream stream = client.GetStream()) { int width, height, fps, quality; using (var reader = new BinaryReader(stream, System.Text.Encoding.ASCII, true)) { using (var writer = new BinaryWriter(stream, System.Text.Encoding.ASCII, true)) { width = reader.ReadInt32(); height = reader.ReadInt32(); fps = reader.ReadInt32(); quality = reader.ReadInt32(); Console.WriteLine("Parameters received:"); Console.WriteLine($" Width: {width}"); Console.WriteLine($" Height: {height}"); Console.WriteLine($" Fps: {fps}"); Console.WriteLine($" Quality: {quality}"); var encoderParam = new ImageEncodingParam(ImwriteFlags.JpegQuality, quality); using (var cap = new VideoCapture(0) { FrameWidth = width, FrameHeight = height, Fps = fps }) { Console.WriteLine("Started video capturing..."); Mat imgMatrix = new Mat(); Mat mask = new Mat(); Mat tresh = new Mat(); try { Scalar lastSeen = default(Scalar); bool autoPilot = false, camAuto = false, moveAuto = false, none; while (client.Connected && stream.CanWrite) { Command commandTaken = (Command)reader.ReadByte(); autoPilot = commandTaken.HasFlag(Command.AutoPilot); camAuto = commandTaken.HasFlag(Command.CamAuto); moveAuto = commandTaken.HasFlag(Command.MoveAuto); none = commandTaken == Command.None; if (cap.Read(imgMatrix)) { Cv2.CvtColor(imgMatrix, mask, ColorConversionCodes.BGR2HSV); Cv2.InRange(mask, greenLower, greenUpper, tresh); Cv2.Erode(tresh, tresh, null, iterations: 2); Cv2.Dilate(tresh, tresh, null, iterations: 2); Cv2.FindContours( tresh, out Point[][] contours, out HierarchyIndex[] hierarchyIndexes, RetrievalModes.External, ContourApproximationModes.ApproxSimple ); if (contours.Length > 0) { contours.OrderBy(element => Cv2.ContourArea(element)); Point[] max = contours[contours.Length - 1]; Cv2.MinEnclosingCircle(max, out Point2f xy, out float radius); //Moments M = Cv2.Moments(max); //Point center = new Point((M.M10 / M.M00), (M.M01 / M.M00)); Point center = new Point(xy.X, xy.Y); if (radius > 10.0f) { Cv2.Circle(imgMatrix, center, (int)radius, new Scalar(0, 255, 255), thickness: 2); Cv2.Circle(imgMatrix, center, 5, new Scalar(0, 0, 255), thickness: -1); //find the ball which region on the screen horizontally [0-5] int xRegion = center.X.Map(0, width, 0, 3); //find the ball which region on the screen vertically [0-5] int yRegion = center.Y.Map(0, height, 0, 3); //find the ball is too far from camera or too close int zRegion = ((int)radius).Map(10, 400, 0, 3); lastSeen = FindBallCoordinates((int)radius, center, new Point(width / 2, height / 2)); #region Automatic Decisions Command command = 0; if (autoPilot) { if (xRegion < 1 && yRegion < 1) { command |= Command.CamLeft | Command.CamUp; } else if (xRegion < 1 && yRegion > 1) { command |= Command.CamLeft | Command.CamDown; } else if (xRegion > 1 && yRegion < 1) { command |= Command.CamRight | Command.CamUp; } else if (xRegion > 1 && yRegion > 1) { command |= Command.CamRight | Command.CamDown; } else if (xRegion < 1) { command |= Command.CamLeft; } else if (xRegion > 1) { command |= Command.CamRight; } else if (yRegion < 1) { command |= Command.CamUp; } else if (yRegion > 1) { command |= Command.CamDown; } if (zRegion > 1) { command |= Command.MoveBackward; } else if (zRegion < 1) { command |= Command.MoveForward; } byte[] message = { (byte)command }; serial.Write(message, 0, 1); } else if (camAuto) { if (xRegion < 1 && yRegion < 1) { command |= Command.CamLeft | Command.CamUp; } else if (xRegion < 1 && yRegion > 1) { command |= Command.CamLeft | Command.CamDown; } else if (xRegion > 1 && yRegion < 1) { command |= Command.CamRight | Command.CamUp; } else if (xRegion > 1 && yRegion > 1) { command |= Command.CamRight | Command.CamDown; } else if (xRegion < 1) { command |= Command.CamLeft; } else if (xRegion > 1) { command |= Command.CamRight; } else if (yRegion < 1) { command |= Command.CamUp; } else if (yRegion > 1) { command |= Command.CamDown; } byte[] message = { (byte)(command | (commandTaken & Command.MoveAuto)) }; serial.Write(message, 0, 1); } else if (moveAuto) { if (zRegion > 1 && xRegion < 1) { command |= Command.MoveForward | Command.MoveLeft; } else if (zRegion > 1 && xRegion > 1) { command |= Command.MoveForward | Command.MoveRight; } else if (zRegion < 1 && xRegion < 1) { command |= Command.MoveBackward | Command.MoveRight; } else if (zRegion < 1 && xRegion > 1) { command |= Command.MoveBackward | Command.MoveLeft; } else if (zRegion > 1) { command |= Command.MoveBackward; } else if (zRegion < 1) { command |= Command.MoveForward; } else if (xRegion < 1) { command |= Command.MoveLeft; } else if (xRegion > 1) { command |= Command.MoveRight; } byte[] message = { (byte)(command | (commandTaken & Command.CamAuto)) }; serial.Write(message, 0, 1); } else if (none) { byte[] message = { (byte)(commandTaken) }; serial.Write(message, 0, 1); } #endregion } else { Console.WriteLine($"{lastSeen.ToString()}"); } } Cv2.ImEncode(".jpg", imgMatrix, out byte[] result, encoderParam); writer.Write(result.Length); stream.Write(result, 0, result.Length); }