首页 \ 问答 \ 从byte []创建图像时C#/ EmguCv参数超出范围异常(C# / EmguCv Argument Out of Range exception when creating images from byte[])

从byte []创建图像时C#/ EmguCv参数超出范围异常(C# / EmguCv Argument Out of Range exception when creating images from byte[])

我尝试使用edgeimages上的MatchTemplate来查找图像中的教学对象。 作为图像源我使用了一个kinect2。 我首先教一个边缘图像,然后在以后捕获的图​​像中搜索教导的模板。 用canny计算边缘图像效果很好。 我将边缘保存为byte []

byte[] bytes = cannyFrame.GetData();

后来我尝试用给定的数据创建图像。 不幸的是,有时创建图像运行在ArgumentOutOfRange-Exception中,并声称该数组很大。 有时发生异常,有时不会发生。 创建当前图像以及创建教导图像时会发生异常(请参阅我的代码中的注释)。 任何人都可以告诉我,为什么会发生这种异常?

        Bitmap bmp; 

        //bmp is filled with the image from the kinect

        int width;
        int height;
        byte[] currentEdges = EdgeDetector.CalculateEdges(bmp, referenceImage.BorderOne, referenceImage.BorderTwo, out width, out height);
        try
        {
            Console.WriteLine("ref width: {0}, height: {1}, byte[]length: {2}", referenceImage.Width_px, referenceImage.Height_px, referenceImage.Edges.Length);                
            //Here happens the exception sometimes: 
            Image<Gray, byte> templateImage = new Image<Gray, byte>(referenceImage.Width_px, referenceImage.Height_px);
            templateImage.Bytes = referenceImage.Edges;
            Console.WriteLine("current image width: {0}, height: {1}, byte[]length: {2}", width, height, currentEdges.Length);

            //Here happens the exception sometimes: 
            Image<Gray, byte> searchImage = new Image<Gray, byte>(width, height);
            searchImage.Bytes = currentEdges;
            Console.WriteLine("");
            Image<Gray, float> imgMatch = searchImage.MatchTemplate(templateImage, TemplateMatchingType.CcoeffNormed);
            }
        catch (Exception)
        {                
            Console.WriteLine(ex.ToString());
        }


    internal class EdgeDetector
    {
        internal static byte[] CalculateEdges(Bitmap bmp, int borderOne, int borderTwo, out int width, out int height)
        {
            try
            {
                Image<Bgr, byte> img = new Image<Bgr, byte>(bmp);

                Mat smallGrayFrame = new Mat();
                Mat smoothedGrayFrame = new Mat();
                Mat cannyFrame = new Mat();

                CvInvoke.PyrDown(img, smallGrayFrame);

                CvInvoke.PyrUp(smallGrayFrame, smoothedGrayFrame);

                CvInvoke.Canny(smoothedGrayFrame, cannyFrame, borderOne, borderTwo);

                byte[] bytes = cannyFrame.GetData();

                width = cannyFrame.Width;
                height = cannyFrame.Height;

                return bytes;
            }
            catch (Exception ex)
            {
                width = 0;
                height = 0;
                return null;
            }
        }

示例输出:ref宽度:46,高度:44,字节[]长度:2024 System.ArgumentOutOfRangeException:请求的范围延伸超过数组的末尾。 在System.Runtime.InteropServices.Marshal.CopyToNative(对象源,Int32的tartIndex,IntPtr目标,Int32长度)在Emgu.CV.CvArray`1.set_Bytes(字节[]值)在AlgorithmsHelper.SearchEdgeImage(AssistentEdgeImageOpenCv referenceImage,Image currentImage )

任何帮助表示赞赏。


I try to use the MatchTemplate on edgeimages to find teached objects in an image. As image source i use a kinect2. I first teach an edge image and search the teached template in later captured image. Calculating the edge images with canny works very good. I save the edges as byte[] with

byte[] bytes = cannyFrame.GetData();

Later I try to create Image with the given data. Unfortunately sometimes creating the image runs in a ArgumentOutOfRange-Exception and claims that the array is to big. Sometimes the exception occurs and sometimes it doesn't occure. The exception can occur creating the current image and also when I'm creating the teached image (see comments in my code). Can anybody tell me, why this exception occurs?

        Bitmap bmp; 

        //bmp is filled with the image from the kinect

        int width;
        int height;
        byte[] currentEdges = EdgeDetector.CalculateEdges(bmp, referenceImage.BorderOne, referenceImage.BorderTwo, out width, out height);
        try
        {
            Console.WriteLine("ref width: {0}, height: {1}, byte[]length: {2}", referenceImage.Width_px, referenceImage.Height_px, referenceImage.Edges.Length);                
            //Here happens the exception sometimes: 
            Image<Gray, byte> templateImage = new Image<Gray, byte>(referenceImage.Width_px, referenceImage.Height_px);
            templateImage.Bytes = referenceImage.Edges;
            Console.WriteLine("current image width: {0}, height: {1}, byte[]length: {2}", width, height, currentEdges.Length);

            //Here happens the exception sometimes: 
            Image<Gray, byte> searchImage = new Image<Gray, byte>(width, height);
            searchImage.Bytes = currentEdges;
            Console.WriteLine("");
            Image<Gray, float> imgMatch = searchImage.MatchTemplate(templateImage, TemplateMatchingType.CcoeffNormed);
            }
        catch (Exception)
        {                
            Console.WriteLine(ex.ToString());
        }


    internal class EdgeDetector
    {
        internal static byte[] CalculateEdges(Bitmap bmp, int borderOne, int borderTwo, out int width, out int height)
        {
            try
            {
                Image<Bgr, byte> img = new Image<Bgr, byte>(bmp);

                Mat smallGrayFrame = new Mat();
                Mat smoothedGrayFrame = new Mat();
                Mat cannyFrame = new Mat();

                CvInvoke.PyrDown(img, smallGrayFrame);

                CvInvoke.PyrUp(smallGrayFrame, smoothedGrayFrame);

                CvInvoke.Canny(smoothedGrayFrame, cannyFrame, borderOne, borderTwo);

                byte[] bytes = cannyFrame.GetData();

                width = cannyFrame.Width;
                height = cannyFrame.Height;

                return bytes;
            }
            catch (Exception ex)
            {
                width = 0;
                height = 0;
                return null;
            }
        }

an example output: ref width: 46, height: 44, byte[]length: 2024 System.ArgumentOutOfRangeException: Requested range extends past the end of the array. at System.Runtime.InteropServices.Marshal.CopyToNative(Object source, Int32 s tartIndex, IntPtr destination, Int32 length) at Emgu.CV.CvArray`1.set_Bytes(Byte[] value) at AlgorithmsHelper.SearchEdgeImage(AssistentEdgeImageOpenCv referenceImage, Image currentImage)

Any help is appreciated.


原文:https://stackoverflow.com/questions/43732588
更新时间:2023-02-18 11:02

最满意答案

如果列表的顺序正确:

find ./ -type f -name *filename* -exec stat --format="%X-%n" {} \; | sort | tail +3

除此以外:

find ./ -type f -name *filename* -exec stat --format="%X-%n" {} \; | sort -r | tail +3

The result was really simple.

If You would like to list all files but the newest 3 you can do:

find ./ -type f -name "*605*" -exec stat --format="%X-%n" {} \; | sort | head -n -3

The head -n -3 is the main thing!!

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。