[英]16 bit image crashes Image.ThresholdBinary in EMGU.CV
我在下面有这个应用程序的两个版本。 这是我写的一个重写,用于处理8位图像。 8位图像工作正常。 16位图像导致Image.Threshold在本机OpenCV调用阈值时崩溃,返回的唯一错误是:
Emgu.CV.dll中发生了类型为'Emgu.CV.Util.CvException'的第一次机会异常。其他信息:OpenCV:
以下内容导致我的“手编码” 16位图像崩溃,也导致16位TIF图像崩溃,通过
var img = new Image<Gray,UInt16>("test.tif");
崩溃是相同的,带有相同的错误消息。
这是我的应用程序:
思考?
using System;
using System.Runtime.InteropServices;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MyUnitTest
{
[TestClass]
public class UnitTestOpenCV
{
// Set up an image with a solid rectangle in it.
private IntPtr _smallImg;
private const int Ncols = 500;
private const int Nrows = 100;
[TestInitialize]
public void Init()
{
const int n = Ncols * Nrows;
var pixels = new short[n];
_smallImg = Marshal.AllocHGlobal(n * 2);
Marshal.Copy(pixels, 0, _smallImg, pixels.Length);
for (int col = 75; col < 75 + 200; col++)
{
for (int row = 25; row < 25 + 50; row++)
{
Marshal.WriteInt16(_smallImg, 2 * (row * Ncols + col), 10000);
}
}
}
[TestCleanup]
public void Cleanup()
{
Marshal.FreeHGlobal(_smallImg);
}
[TestMethod]
public void FindingMinBoundingRectangle()
{
var img = new Image<Gray, ushort>(Ncols, Nrows, Ncols, _smallImg);
var rotatedImg = img.Rotate(20, new Gray(0), false);
var bwImg = img.ThresholdBinary(new Gray(5000), new Gray(255));
var contour = bwImg.FindContours();
var rect = contour.GetMinAreaRect();
rotatedImg.Draw(rect, new Gray(30000), 50);
rotatedImg.Save("img.tif");
AOIAlgorithmsBase.SaveImage("myImg.tif", img.MIplImage.imageData, Ncols, Nrows, Ncols);
ImageViewer.Show(rotatedImg, "Image Viewer Window");
}
}
}
实际上,即使EmguCV读取16位图像,大多数处理例程也需要8位或32位图像。 从OpenCV文档获取阈值方法:
参数:
src –输入数组(单通道,8位或32位浮点)。
就我而言,将所有Emgu处理和显示的图像转换为8位,然后转换回16位以tif格式保存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.