private Mat str2mat(String s) { // Decode data byte[] byteArr = Convert.FromBase64String(s); List <byte> byteList = new List <Byte>(byteArr); Mat data = Converters.vector_char_to_Mat(byteList); Mat img = Imgcodecs.imdecode(data, Imgcodecs.IMREAD_UNCHANGED); return(img); }
/// <summary> /// Handles the event of a new image getting captured. /// </summary> /// <param name="imageData">The raw data of the image.</param> private void OnCaptureRawImageComplete(byte[] imageData) { lock (_cameraLockObject) { _isCapturing = false; } // // Initialize to 8x8 texture so there is no discrepency // // between uninitalized captures and error texture // Texture2D texture = new Texture2D (8, 8); // bool status = texture.LoadImage (imageData); // // if (status && (texture.width != 8 && texture.height != 8)) { // // Mat imgMat = new Mat (texture.height, texture.width, CvType.CV_8UC3); // // Debug.Log ("imgMat.ToString() " + imgMat.ToString ()); // // Utils.texture2DToMat (texture, imgMat); // // Imgproc.cvtColor (imgMat, imgMat, Imgproc.COLOR_RGB2BGR); // // Run (imgMat); // // Imgproc.cvtColor (imgMat, imgMat, Imgproc.COLOR_BGR2RGB); // // Texture2D outputTexture = new Texture2D (texture.width, texture.height, TextureFormat.RGBA32, false); // Utils.matToTexture2D (imgMat, outputTexture); // // imgMat.Dispose(); // // OnImageReceivedEvent.Invoke (outputTexture); // } Mat buff = new Mat(1, imageData.Length, CvType.CV_8UC1); Utils.copyToMat <byte> (imageData, buff); Mat imgMat = Imgcodecs.imdecode(buff, Imgcodecs.IMREAD_COLOR); // Debug.Log ("imgMat.ToString() " + imgMat.ToString ()); buff.Dispose(); Run(imgMat); Imgproc.cvtColor(imgMat, imgMat, Imgproc.COLOR_BGR2RGB); Texture2D outputTexture = new Texture2D(imgMat.width(), imgMat.height(), TextureFormat.RGBA32, false); Utils.matToTexture2D(imgMat, outputTexture); imgMat.Dispose(); OnImageReceivedEvent.Invoke(outputTexture); }
/// <summary> /// Handles the event of a new image getting captured. /// </summary> /// <param name="imageData">The raw data of the image.</param> private void OnCaptureRawImageComplete(byte[] imageData) { lock (_cameraLockObject) { _isCapturing = false; } //// Initialize to 8x8 texture so there is no discrepency //// between uninitalized captures and error texture //Texture2D texture = new Texture2D(8, 8); //bool status = texture.LoadImage(imageData); //if (status && (texture.width != 8 && texture.height != 8)) //{ // _previewObject.SetActive(true); // Renderer renderer = _previewObject.GetComponent<Renderer>(); // if (renderer != null) // { // renderer.material.mainTexture = texture; // } //} Mat buff = new Mat(1, imageData.Length, CvType.CV_8UC1); MatUtils.copyToMat <byte>(imageData, buff); Mat imgMat = Imgcodecs.imdecode(buff, Imgcodecs.IMREAD_COLOR); // Debug.Log ("imgMat.ToString() " + imgMat.ToString ()); buff.Dispose(); Run(imgMat); Imgproc.cvtColor(imgMat, imgMat, Imgproc.COLOR_BGR2RGB); Texture2D outputTexture = new Texture2D(imgMat.width(), imgMat.height(), TextureFormat.RGBA32, false); Utils.matToTexture2D(imgMat, outputTexture); imgMat.Dispose(); //if (status && (texture.width != 8 && texture.height != 8)) //{ _previewObject.SetActive(true); Renderer renderer = _previewObject.GetComponent <Renderer>(); if (renderer != null) { renderer.material.mainTexture = outputTexture; } //} }
Mat str2mat(String s) { // Decode data //Debug.Log(s); byte[] byteArr = System.Convert.FromBase64String(s); List <byte> byteList = new List <Byte>(byteArr); // Decode data //string decoded_string = base64_decode(s); //List<byte> byteList = new List<Byte>(Encoding.ASCII.GetBytes(decoded_string)); Mat data = Converters.vector_char_to_Mat(byteList); //Debug.Log("Converted to vector mat"); //System.Collections.Generic.List<char> data(decoded_string.begin(), decoded_string.end()); //List<uchar> data(decoded_string.begin(), decoded_string.end()); //Debug.Log("Converted to image"); Mat img = Imgcodecs.imdecode(data, Imgcodecs.IMREAD_UNCHANGED); return(img); }
private void Update() { if (!ct.begin_flag) { if (!ct.rec) { ct.Receive(); } } if (ct.receiveMessage != null) { Debug.Log("Server:" + ct.receiveMessage); ct.receiveMessage = null; } /*if (ct.receive_joint != null) * { * Debug.Log("Server:" + ct.receive_joint); * ct.receive_joint = null; * }*/ if (ct.recieve_flag != 0) { //Debug.Log("Server:" + ct.frame_size.ToString()); if (ct.frame_img.Length != ct.dataLength_frame) { Debug.Log("Receive : " + ct.frame_img.Length.ToString() + ", len : " + ct.dataLength.ToString()); } Mat mat_img = new Mat(1, ct.frame_img.Length, CvType.CV_8U); mat_img.put(0, 0, ct.frame_img); Mat frame_img_mat = Imgcodecs.imdecode(mat_img, 1); Imgproc.cvtColor(frame_img_mat, frame_img_mat, Imgproc.COLOR_BGR2RGB); //Debug.Log(frame_img_mat.size()); byte[] image = new byte[252 * 448 * 3]; frame_img_mat.get(0, 0, image); var texture = rawImage.texture as Texture2D; texture.LoadRawTextureData(image); //TODO: Should be able to do this: texture.LoadRawTextureData(pointerToImage, 1280 * 720 * 4); texture.Apply(); ct.recieve_flag = 0; ct.rec = false; } }
/// <summary> /// Handles the event of a new image getting captured. /// </summary> /// <param name="imageData">The raw data of the image.</param> private void OnCaptureRawImageComplete(byte[] imageData) { lock (_cameraLockObject) { _isCapturing = false; } // // Initialize to 8x8 texture so there is no discrepency // // between uninitalized captures and error texture // Texture2D texture = new Texture2D(8, 8); // bool status = texture.LoadImage(imageData); // // if (status && (texture.width != 8 && texture.height != 8)) // { // OnImageReceivedEvent.Invoke(texture); // } // // Initialize to 8x8 texture so there is no discrepency // // between uninitalized captures and error texture // Texture2D texture = new Texture2D (8, 8); // bool status = texture.LoadImage (imageData); // // if (status && (texture.width != 8 && texture.height != 8)) { // // Mat imgMat = new Mat (texture.height, texture.width, CvType.CV_8UC3); // // Debug.Log ("imgMat.ToString() " + imgMat.ToString ()); // // Utils.texture2DToMat (texture, imgMat); // // Imgproc.cvtColor (imgMat, imgMat, Imgproc.COLOR_RGB2BGR); // // Run (imgMat); // // Imgproc.cvtColor (imgMat, imgMat, Imgproc.COLOR_BGR2RGB); // // Texture2D outputTexture = new Texture2D (texture.width, texture.height, TextureFormat.RGBA32, false); // Utils.matToTexture2D (imgMat, outputTexture); // // imgMat.Dispose(); // // OnImageReceivedEvent.Invoke (outputTexture); // } Mat buff = new Mat(1, imageData.Length, CvType.CV_8UC1); Utils.copyToMat <byte> (imageData, buff); Mat imgMat = Imgcodecs.imdecode(buff, Imgcodecs.IMREAD_COLOR); //Reads image from memory; IMREAD_COLOR converts to BGR image // Debug.Log ("imgMat.ToString() " + imgMat.ToString ()); buff.Dispose(); //Saving image in package at documents/C2 dir Mat buff2 = new Mat(1, imageData.Length, CvType.CV_8UC1); Utils.copyToMat <byte> (imageData, buff2); Mat imgMat2 = Imgcodecs.imdecode(buff2, Imgcodecs.IMREAD_COLOR); //Reads image from memory; IMREAD_COLOR converts to BGR image buff2.Dispose(); Imgproc.cvtColor(imgMat2, imgMat2, Imgproc.COLOR_BGR2RGB); Texture2D inputTexture = new Texture2D(imgMat2.width(), imgMat2.height(), TextureFormat.RGBA32, false); Utils.matToTexture2D(imgMat2, inputTexture); byte[] bytes = inputTexture.EncodeToJPG(); System.IO.File.WriteAllBytes(Application.persistentDataPath + "test.jpg", bytes); // Mat buff3 = new Mat(1, imageData.Length, CvType.CV_8UC1); Utils.copyToMat <byte> (imageData, buff3); Mat imgMat3 = Imgcodecs.imdecode(buff3, Imgcodecs.IMREAD_COLOR); //Reads image from memory; IMREAD_COLOR converts to BGR image buff3.Dispose(); Imgproc.cvtColor(imgMat3, imgMat3, Imgproc.COLOR_BGR2RGB); OpenCVForUnity.CoreModule.Rect crop_size = new OpenCVForUnity.CoreModule.Rect(imgMat3.width() / 2, imgMat3.height() / 2, 740, 416); Mat cropped_image = new Mat(imgMat3, crop_size); Texture2D inputTexture3 = new Texture2D(cropped_image.width(), cropped_image.height(), TextureFormat.RGBA32, false); Utils.matToTexture2D(cropped_image, inputTexture3); byte[] bytes3 = inputTexture3.EncodeToJPG(); System.IO.File.WriteAllBytes(Application.persistentDataPath + "test_crp.jpg", bytes3); // Mat buff4 = new Mat(1, imageData.Length, CvType.CV_8UC1); Utils.copyToMat <byte> (imageData, buff4); Mat imgMat4 = Imgcodecs.imdecode(buff4, Imgcodecs.IMREAD_COLOR); //Reads image from memory; IMREAD_COLOR converts to BGR image buff4.Dispose(); Imgproc.cvtColor(imgMat4, imgMat4, Imgproc.COLOR_BGR2RGB); OpenCVForUnity.CoreModule.Rect crop_size4 = new OpenCVForUnity.CoreModule.Rect(imgMat4.width() / 2, imgMat4.height() / 2, 740, 416); Mat cropped_image4 = new Mat(imgMat4, crop_size4); Size upsample = new Size(1920, 1080); Mat magnified = new Mat(1920, 1080, CvType.CV_8UC1); double scalex = 2.6; double scaley = 2.6; Imgproc.resize(cropped_image4, magnified, upsample, 0, 0); Texture2D inputTexture4 = new Texture2D(magnified.width(), magnified.height(), TextureFormat.RGBA32, false); Utils.matToTexture2D(magnified, inputTexture4); byte[] bytes4 = inputTexture4.EncodeToJPG(); System.IO.File.WriteAllBytes(Application.persistentDataPath + "test_crp_mag.jpg", bytes4); // End of saving image //Run (imgMat); //Run (imgMat); Run(magnified); Imgproc.cvtColor(imgMat, imgMat, Imgproc.COLOR_BGR2RGB); Texture2D outputTexture = new Texture2D(imgMat.width(), imgMat.height(), TextureFormat.RGBA32, false); Utils.matToTexture2D(imgMat, outputTexture); //Saving output image byte[] bytes_output = outputTexture.EncodeToJPG(); System.IO.File.WriteAllBytes(Application.persistentDataPath + "test_output.jpg", bytes_output); // imgMat.Dispose(); OnImageReceivedEvent.Invoke(outputTexture); }