从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
最满意答案
如果列表的顺序正确:
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!!
相关问答
更多-
LINUX 如何查看JPG文件[2022-06-13]
find -
一行bash命令,用于查找目录中N个最旧和最新的文件(One line bash command to find N oldest and newest files in a directory)[2021-09-22]
如何添加这样的换行符: files=$(ls -lhrt dirname) && echo -e "${files}\n" | head -5 && echo -e "${files}\n" | tail -5 说明: -e标志使echo能够解释转义,例如本例中的\n 。 \n本身是“新行”的转义序列。 所以它只是在echoed变量之后添加一个新行。 ${ }称为Brace Expansion 。 由于我将字符串放在引号中, ${}会将变量扩展为字符串。 What about adding linebre ... -
你可以使用stat和find : find . -name "someFile" -exec stat -c '%n:%Y' {} + | sort -t : -k2 | cut -d : -f1 stat命令是自文件修改时间的EPOCH值以来打印每个文件名的时间 sort使用第二个键(修改时间)对stat的输出sort排序 cut只是选择第一列(文件名) 编辑:根据下面的评论你可以使用: while IFS=: read -r f t; do echo "$f <$(date -d @$t)>" ...
-
ls命令有一个参数-t按时间排序。 然后你可以用head -1抓住第一个(最新的)。 ls -t b2* | head -1 但要小心: 为什么不能解析ls的输出(1) 如何在目录中找到最新(最新,最早,最旧)的文件? 我的个人意见:解析ls只有当文件名包含有趣的字符,如换行符或空格时才是危险的。 如果可以保证文件名不包含有趣的字符,那么解析ls是非常安全的。 不过,如果您正在开发一个脚本,这个脚本在很多不同的情况下都是由许多人在许多系统上运行的,那么我非常建议不要解析ls 。 The ls comman ...
-
如何对html文件进行排序,使其在页面上显示为较旧到最新?(How to sort html files to display they to older to newest at page?)[2023-08-05]
只需用print_r($files)替换print_r($files) foreach ($files as $file) { include $file; } 而你应该没事。 Just replace print_r($files) with foreach ($files as $file) { include $file; } And you should be fine. -
也许是最简单的解决方案,但它不处理文件名中的空格 scp `ls -t | head -3` user@server:. 使用xargs具有处理文件名中的空格的优点,但将执行scp三次 ls -t | head -3 | xargs -i scp {} user@server:. 基于循环的解决方案看起来像这样。 我们在这里读取时使用,因为read的默认分隔符是换行符,而不是像for循环那样的空格字符 ls -t | head -3 | while read file ; do scp $file us ...
-
用最新日期提取文件(Extracting Files with newest date)[2023-12-21]
尝试 rem -----------------test.bat-------------- @echo off & SetLocal EnableDelayedExpansion set "file=" set "srcFolder=\\server5\Datapool" set "dstFolder=C:\Users\folder1" rem language independent time for /f "tokens=2 delims==" %%a in ('wmic os get local ... -
如果列表的顺序正确: 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 ...
-
您应该在选项中添加T参数来执行此操作: ls -1tT | head -1 添加l参数时查看输出: ls -tTl -rw-r--r-- 1 user wheel 0 Apr 23 17:54:27 2016 1 -rw-r--r-- 1 user wheel 0 Apr 23 17:54:19 2016 3 -rw-r--r-- 1 user wheel 0 Apr 23 17:54:12 2016 2 You should add T argument ...
-
在目录中查找最新文件,然后选择具有相同DateTime的所有文件(Find newest file in directory, then select all files with same DateTime)[2022-02-01]
$myfiles总是只包含一个文件 - 这是因为每个对象的秒数不同: 然后只比较你感兴趣的时间戳部分 - 在这个例子中我们比较分钟: $Timestamp = $file.ToString('yyyyMMddHHmm') $myfiles = $files | ? {$_.LastWriteTime.ToString('yyyyMMddHHmm') -eq $file} $myfiles always includes only one file - this is because the second ...