Emgu Image.Data 与 Image.GetSubRect

问题描述

我一直在 C# 中的 Emgu 4.3 中试验 subImg=img.GetSubRect(rect)。这……很奇怪。

如果 rect=Rectangle(rX,rY,rW,rH)subImg.Size==(rW,rH)。伟大的。然而,subImg 只是一个以 rect 作为其 ROI 的新图像。例如。 subImg.IsROISet==false 和设置 subImg.ROI=Rectangle.Empty 不会改变任何东西!如果您曾经使用过 subImg.Data,这尤其困难;突然 subImg[y,x].Intensity==subImg.Data[y+rY,x+rX],但我不知道如何找到 (rY,rX) 只给出 subImg。 :(

我喜欢 GetSubRect 因为它是 O(1) 和线程安全的(而不是例如 img.copy())。我喜欢 img.Data 因为它可以快速访问像素值(而不是例如 (byte)Math.Round(img[y,x].Intensity)。不幸的是,如果有人使用.GetSubRect,似乎没有人可以使用.Data。 :(

img.MlplImage.imageData 有问题。它是底层像素数据的 IntPtr,我们知道两个 subimgs 具有相同的底层数据。但是,GetSubRect 会更改该指针。也许将它移动 (rX,rY)?

问题:

  1. 是否有更好的方法来访问原始像素值,所以 img.Data 没有用可以吗?
  2. 有没有办法从 img.MlplImage.imageData 得到 (rX,rY),所以 img.Data 有用吗?

代码

//All assertions pass (much to my chagrin)

//Make an arbitrary image.
int origH = 121;
in origW = 101;
double fill(Point p)
{
     return ((double)p.Y * origW + p.X) / (origH * origW);
}
double[,] data = new double[origH,origW,1];
for (int y = 0; y < origH; y++)
{
     for(int x = 0; x < origW; x++)
          data[y,x,0] = fill(new Point(x,y));
}
Image<Gray,double> fullImg = new Image<Gray,double>(data);

//Get the sub image two ways.
Rectangle subRect = new Rectangle(11,21,31,41);
//by GetSubRect
Image<Gray,double> subImg = fullImg.GetSubRect(subRect);
//by ROI
Image<Gray,double> roiImg = fullImg;
roiImg.ROI = subRect;

//Same functional images.
Assert.IsTrue(subImg.Size == roiImg.Size);
Point localPoint = new Point(1,2);
double subColor0 = subImg[localPoint].Intensity;
double roiColor0 = roiImg[localPoint].Intensity;
Point globalPoint = localPoint.Plus(subRect.Location);
double color0 = fill(globalPoint);  
Assert.IsTrue(subColor0 == color0);
Assert.IsTrue(roiColor0 == color0);

//Different ROIs
Assert.IsTrue(roiImg.IsROISet);
Assert.IsTrue(roiImg.ROI.Location == subRect.Location);
Assert.IsTrue(!subImg.IsROISet);
Assert.IsTrue(subImg.ROI.Location == default);

//Same data.
Assert.IsTrue(subImg.Data == roiImg.Data);
//Even up to changing it under the hood.
double color1 = 1.234;
roiImg.Data[globalPoint.Y,globalPoint.X,0] = color1;
double subColor1 = subImg[localPoint].Intensity;
double roiColor1 = roiImg[localPoint].Intensity;
Assert.IsTrue(subColor1 == color1);
Assert.IsTrue(roiColor1 == color1);

//The images DO differ deep in their MIplImages?
IntPtr subMIplPtr = subImg.MIplImage.imageData;
IntPtr roiMIplPtr = roiImg.MIplImage.imageData;
Assert.IsTrue(subMIplPtr != roiMIplPtr);

感谢您的任何意见或建议。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)