public DifferenceDetector(int x, int y, int width, int height) { temp1 = new Image32F(width, height); temp2 = new Image32F(width, height); Width = width; Height = height; RoiX = x; RoiY = y; }
/// <summary> /// Default constructor initializing the background /// </summary> /// <param name="im">Initial background image</param> protected BackgroundModel(Image8 im) { //we allow null to be passed in the constructor to give derived classes the ability //to initialize the background if (im == null) { return; } background = new Image32F(im); width = im.Width; height = im.Height; }
void GenerateMask() { //To generate the mask, we use a euclidian distance transform to the center pixel //followed by a thresholding operation //To save resources we only do this within the outer square of the circle since //all pixels outside of that square will be black anyway double xStart, yStart; //compute outer square xStart = _center.x - _radius; yStart = _center.y - _radius; if (xStart < 0) { xStart = 0; } if (yStart < 0) { yStart = 0; } IppiROI outerSquare = new IppiROI((int)xStart, (int)yStart, 2 * _radius, 2 * _radius); if (outerSquare.X + outerSquare.Width >= _mask.Width) { outerSquare.Width = _mask.Width - outerSquare.X - 1; } if (outerSquare.Y + outerSquare.Height >= _mask.Height) { outerSquare.Height = _mask.Height - outerSquare.Y - 1; } //The distance transform will calculate for all non-0 pixels the distance to the closest //0 pixel - hence we set all pixels in our mask temporarily to 1 and the circles center to 0 IppHelper.IppCheckCall(ip.ippiSet_8u_C1R(1, _mask.Image, _mask.Stride, _mask.Size)); IppHelper.IppCheckCall(ip.ippiSet_8u_C1R(0, _mask[Center], _mask.Stride, new IppiSize(1, 1))); //initialize buffers and calculate distance transform within outer square //we initialize the distance transform image with a value > radius so that all the //untouched pixel outside of the outer square will later be set to 0 int bufferSize; IppHelper.IppCheckCall(cv.ippiTrueDistanceTransformGetBufferSize_8u32f_C1R(outerSquare.Size, &bufferSize)); byte * dTransBuffer = (byte *)Marshal.AllocHGlobal(bufferSize); Image32F distImage = new Image32F(_mask); IppHelper.IppCheckCall(ip.ippiSet_32f_C1R(Radius + 1, distImage.Image, distImage.Stride, distImage.Size)); IppHelper.IppCheckCall(cv.ippiTrueDistanceTransform_8u32f_C1R(_mask[outerSquare.TopLeft], _mask.Stride, distImage[outerSquare.TopLeft], distImage.Stride, outerSquare.Size, dTransBuffer)); //set all pixels whose value after the distance transform is >radius to 0 IppHelper.IppCheckCall(ip.ippiThreshold_GTVal_32f_C1IR(distImage.Image, distImage.Stride, distImage.Size, Radius, 0)); //now set all pixels whose value is >0 to 1 IppHelper.IppCheckCall(ip.ippiThreshold_GTVal_32f_C1IR(distImage.Image, distImage.Stride, distImage.Size, 0, 1)); //convert dist image, copying it into the mask IppHelper.IppCheckCall(ip.ippiConvert_32f8u_C1R(distImage.Image, distImage.Stride, _mask.Image, _mask.Stride, _mask.Size, IppRoundMode.ippRndNear)); //Clean up Marshal.FreeHGlobal((IntPtr)dTransBuffer); distImage.Dispose(); _maskValid = true; /* * //We want to generate the mask with the least amount of pixel-iterations possible * //based on the radius we can define to rectangular regions for which we can * //bulk-set the pixels * //The inner square of the circle is definitely white (centered around center, side-length = sqrt(r*r/2) * //The outer square is definitely black (centerered around center, side-length=radius) * double sideLength,xStart,yStart; * * //compute inner square * sideLength = Math.Sqrt(_radius * _radius / 2); * xStart = _center.x - sideLength / 2; * yStart = _center.y - sideLength / 2; * if (xStart < 0) * xStart = 0; * if (yStart < 0) * yStart = 0; * IppiROI innerSquare = new IppiROI((int)xStart, (int)yStart, (int)sideLength, (int)sideLength); * if (innerSquare.X + innerSquare.Width >= _mask.Width) * { * innerSquare.Width = _mask.Width - innerSquare.X - 1; * } * if (innerSquare.Y + innerSquare.Height >= _mask.Height) * { * innerSquare.Height = _mask.Height - innerSquare.Y - 1; * } * * //compute outer square * xStart = _center.x - _radius / 2.0; * yStart = _center.y - _radius / 2.0; * if (xStart < 0) * xStart = 0; * if (yStart < 0) * yStart = 0; * IppiROI outerSquare = new IppiROI((int)xStart, (int)yStart, _radius, _radius); * if (outerSquare.X + outerSquare.Width >= _mask.Width) * { * outerSquare.Width = _mask.Width - outerSquare.X - 1; * } * if (outerSquare.Y + outerSquare.Height >= _mask.Height) * { * outerSquare.Height = _mask.Height - outerSquare.Y - 1; * } * * //set whole image to black and inner square to 1 * IppHelper.IppCheckCall(ip.ippiSet_8u_C1R(0, _mask.Image, _mask.Stride, _mask.Size)); * IppHelper.IppCheckCall(ip.ippiSet_8u_C1R(1, _mask[innerSquare.TopLeft], _mask.Stride, innerSquare.Size)); * * //loop over all the pixels that are in between the boundaries of the outer * //and inner square, measure their distance to the center and * //p = d>r?0:1; */ }