|
@@ -19,6 +19,7 @@ namespace bbiwarg.Images
|
|
|
{
|
|
|
private int width, height;
|
|
|
private Image<Gray, short> image;
|
|
|
+ private int[] histogram;
|
|
|
|
|
|
public DepthImage(int width, int height, short[] data)
|
|
|
{
|
|
@@ -52,47 +53,107 @@ namespace bbiwarg.Images
|
|
|
|
|
|
public void filterMedian(int filterSize)
|
|
|
{
|
|
|
- image = image.SmoothMedian(3);
|
|
|
+ image = image.SmoothMedian(filterSize);
|
|
|
}
|
|
|
|
|
|
public void thresholdDepth(int min, int max)
|
|
|
{
|
|
|
image = image.ThresholdToZero(new Gray(min));
|
|
|
- image = image.ThresholdBinaryInv(new Gray(max), new Gray(2000));
|
|
|
- }
|
|
|
+ image = image.ThresholdTrunc(new Gray(max));
|
|
|
|
|
|
- public void thresholdPosition(int minX, int maxX, int minY, int maxY)
|
|
|
+ for (int x = 0; x < width; ++x)
|
|
|
+ {
|
|
|
+ for (int y = 0; y < height; ++y)
|
|
|
+ {
|
|
|
+ if (getDepth(x, y) == 0)
|
|
|
+ setDepth(x, y, (short) max);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public int[] getHistogram()
|
|
|
{
|
|
|
+ Int16 minDepth = getMinDepth();
|
|
|
+ Int16 maxDepth = getMaxDepth();
|
|
|
+
|
|
|
+ histogram = new int[maxDepth - minDepth];
|
|
|
for (int x = 0; x < width; ++x)
|
|
|
{
|
|
|
for (int y = 0; y < height; ++y)
|
|
|
{
|
|
|
- if (x < minX || x > maxX || y < minY || y > maxY)
|
|
|
- setDepth(x, y, 0);
|
|
|
+ int depth = getDepth(x, y);
|
|
|
+ if (depth != maxDepth)
|
|
|
+ histogram[depth - minDepth]++;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ return histogram;
|
|
|
}
|
|
|
|
|
|
- public List<Vector> getRectPoints()
|
|
|
+ public Int16 getPeakDepth()
|
|
|
{
|
|
|
- Image<Gray, Byte> tmp = image.Convert<Byte>(delegate(short s) { return (s == 2000) ? (byte) 1 : (byte) 0; });
|
|
|
+ Int16 minDepth = getMinDepth();
|
|
|
|
|
|
- Contour<Point> contours = tmp.FindContours();
|
|
|
- MCvBox2D box = contours.GetMinAreaRect();
|
|
|
- Seq<Point> points = contours.GetConvexHull(Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKWISE);
|
|
|
+ Int16 peak = -1;
|
|
|
+ int maxValue = 0;
|
|
|
+ for (int i = 0; i < histogram.Length; ++i)
|
|
|
+ {
|
|
|
+ if (histogram[i] > maxValue)
|
|
|
+ {
|
|
|
+ maxValue = histogram[i];
|
|
|
+ peak = (Int16) (i + minDepth);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return peak;
|
|
|
+ }
|
|
|
|
|
|
- List<Vector> result = new List<Vector>();
|
|
|
- foreach (Point p in points)
|
|
|
+ public Int16 getMinDepth()
|
|
|
+ {
|
|
|
+ Int16 minDepth = Int16.MaxValue;
|
|
|
+ for (int x = 0; x < width; ++x)
|
|
|
{
|
|
|
- result.Add(new DenseVector(new float[]{p.X, p.Y}));
|
|
|
+ for (int y = 0; y < height; ++y)
|
|
|
+ {
|
|
|
+ Int16 depth = getDepth(x, y);
|
|
|
+ if (depth < minDepth)
|
|
|
+ minDepth = depth;
|
|
|
+ }
|
|
|
}
|
|
|
- return result;
|
|
|
+ return minDepth;
|
|
|
}
|
|
|
|
|
|
- public void floodFill(int x, int y)
|
|
|
+ public Int16 getMaxDepth()
|
|
|
{
|
|
|
+ Int16 maxDepth = Int16.MinValue;
|
|
|
+ for (int x = 0; x < width; ++x)
|
|
|
+ {
|
|
|
+ for (int y = 0; y < height; ++y)
|
|
|
+ {
|
|
|
+ Int16 depth = getDepth(x, y);
|
|
|
+ if (depth > maxDepth)
|
|
|
+ maxDepth = depth;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return maxDepth;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void floodFill(int x, int y, int val)
|
|
|
+ {
|
|
|
+ Image<Gray, Byte> tmp = image.Convert<Byte>(delegate(short s) { return (byte) ((int) s * Byte.MaxValue / 2000); });
|
|
|
+
|
|
|
Emgu.CV.Structure.MCvConnectedComp comp = new MCvConnectedComp();
|
|
|
- Emgu.CV.CvInvoke.cvFloodFill(image.Ptr, new Point(x, y), new MCvScalar(155), new MCvScalar(5), new MCvScalar(5), out comp, Emgu.CV.CvEnum.CONNECTIVITY.FOUR_CONNECTED, 0, IntPtr.Zero); //, out comp,Emgu.CV.CvEnum.CONNECTIVITY.EIGHT_CONNECTED, Emgu.CV.CvEnum.FLOODFILL_FLAG.DEFAULT,mask.Ptr)
|
|
|
+ Emgu.CV.CvInvoke.cvFloodFill(tmp.Ptr, new Point(x, y), new MCvScalar(val), new MCvScalar(30), new MCvScalar(30), out comp, 0, IntPtr.Zero);
|
|
|
+
|
|
|
+ for (int i = 0; i < width; ++i)
|
|
|
+ {
|
|
|
+ for (int j = 0; j < height; ++j)
|
|
|
+ {
|
|
|
+ if (tmp.Data[j, i, 0] == 255)
|
|
|
+ setDepth(i, j, 2000);
|
|
|
+ else
|
|
|
+ setDepth(i, j, 0);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|