I have two versions of this app below. This one, and a rewrite I wrote to work with 8 bit images. 8 bit images work fine. 16 bit images cause Image.Threshold to crash in the native OpenCV call for threshold, the only error returned:
A first chance exception of type 'Emgu.CV.Util.CvException' occurred in Emgu.CV.dll Additional information: OpenCV:
The below causes a crash on my "hand coded" 16 bit image, it also crashes on a 16 bit TIF image, loaded via
var img = new Image<Gray,UInt16>("test.tif");
The crashes are identical, with identical error messages.
Here's my app:
Thoughts?
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");
}
}
}
Actually even if EmguCV reads 16-bit images, most processing routines require 8-bit or 32-bit images. From OpenCV docs for the threshold method:
Parameters:
src – input array (single-channel, 8-bit or 32-bit floating point).
In my case, in convert my images to 8-bit for all the Emgu processing and displaying, and I convert back to 16-bit for saving in tif format.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.