Emgu CV - Skeleton of an Image in EmguCV (Skeletonization) - c#

i need to implement Skeletonization in Emgu CV but i not have success.
I have a code mentioned on the website below but it does not work :
Skeletonization using EmguCV^]
This code below DONT work:
Image<Gray, Byte> eroded = new Image<Gray, byte>(img2.Size);
Image<Gray, Byte> temp = new Image<Gray, byte>(img2.Size);
Image<Gray, Byte> skel = new Image<Gray, byte>(img2.Size);
skel.SetValue(0);
CvInvoke.cvThreshold(img2, img2, 127, 256, 0);
StructuringElementEx element = new StructuringElementEx(3, 3, 1, 1, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_CROSS);
bool done = false;
while (!done)
{
CvInvoke.cvErode(img2, eroded, element,1);
CvInvoke.cvDilate(eroded, temp, element,1);
temp = img2.Sub(temp);
skel = skel | temp;
img2 = eroded;
if (CvInvoke.cvCountNonZero(img2) == 0) done = true;
}
This code WORK but is very slow in video (sequential frames)
Image<Gray, byte> Skeleton(Image<Gray, byte> orgImg)
{
Image<Gray, byte> skel = new Image<Gray, byte>(orgImg.Size);
for (int y = 0; y < skel.Height; y++)
for (int x = 0; x < skel.Width; x++)
skel.Data[y, x, 0] = 0;
imageBoxOutputROI.Image = skel;
Image<Gray, byte> img = skel.Copy();
for (int y = 0; y < skel.Height; y++)
for (int x = 0; x < skel.Width; x++)
img.Data[y, x, 0] = orgImg.Data[y, x, 0];
StructuringElementEx element;
element = new StructuringElementEx(3, 3, 1, 1, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_CROSS);
Image<Gray, byte> temp;
bool done = false;
do
{
temp = img.MorphologyEx(element, Emgu.CV.CvEnum.CV_MORPH_OP.CV_MOP_OPEN, 1);
temp = temp.Not();
temp = temp.And(img);
skel = skel.Or(temp);
img = img.Erode(1);
double[] min, max;
Point[] pmin, pmax;
img.MinMax(out min, out max, out pmin, out pmax);
done = (max[0] == 0);
} while (!done);
return skel;
}
Input image:
I need a help to implement the skeletonization code.
A research in below sites but i not hace success:
http://felix.abecassis.me/2011/09/opencv-morphological-skeleton/[^]
https://stackoverflow.com/questions/26850944/skeleton-of-an-image-in-emgucv[^]
https://stackoverflow.com/questions/26850944/skeleton-of-an-image-in-emgucv[^]
I am grateful for any help .
Richard J. Algarve

Try below code.
It works for me (My input image has white background). I think the problem with your code is Sub and Or operators.
public static Bitmap Skelatanize(Bitmap image)
{
Image<Gray, byte> imgOld = new Image<Gray, byte>(image);
Image<Gray, byte> img2 = (new Image<Gray, byte>(imgOld.Width, imgOld.Height, new Gray(255))).Sub(imgOld);
Image<Gray, byte> eroded = new Image<Gray, byte>(img2.Size);
Image<Gray, byte> temp = new Image<Gray, byte>(img2.Size);
Image<Gray, byte> skel = new Image<Gray, byte>(img2.Size);
skel.SetValue(0);
CvInvoke.Threshold(img2, img2, 127, 256, 0);
var element = CvInvoke.GetStructuringElement(ElementShape.Cross, new Size(3, 3), new Point(-1, -1));
bool done = false;
while (!done)
{
CvInvoke.Erode(img2, eroded, element, new Point(-1, -1), 1, BorderType.Reflect, default(MCvScalar));
CvInvoke.Dilate(eroded, temp, element, new Point(-1, -1), 1, BorderType.Reflect, default(MCvScalar));
CvInvoke.Subtract(img2, temp, temp);
CvInvoke.BitwiseOr(skel, temp, skel);
eroded.CopyTo(img2);
if (CvInvoke.CountNonZero(img2) == 0) done = true;
}
return skel.Bitmap;
}

Related

Emgu Split Object

May I ask, how can I separate the detected object in a contour?
below is my source code
Image<Gray, byte> imgOutput = imgInput.Convert<Gray, byte>().ThresholdBinary(new Gray(100), new Gray(255)); Emgu.CV.Util.VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); Mat m = new Mat();
//Image<Gray, byte> imgOut = new Image<Gray, byte>(imgInput.Width, imgInput.Height, new Gray(0));
CvInvoke.FindContours(imgOutput, contours, m, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
if (contours.Size > 0)
{
double perimeter = CvInvoke.ArcLength(contours[1], true);
VectorOfPoint approx = new VectorOfPoint();
CvInvoke.ApproxPolyDP(contours[1], approx, 0.04 * perimeter, true);
CvInvoke.DrawContours(imgInput, contours, 1, new MCvScalar(0, 0, 255), 2);
pictureBox2.Image = imgInput.Bitmap;
}
Separated objects result.

EmguCV Mat change to LineSegment2D[]

I'm using emguCV houghines Method and get a Mat Object, how can I get LineSegment2D[] by this Mat.
Sample of code:
Image<Bgr, Byte> img1 = new Image<Bgr, Byte>(fileName);
UMat uimage = new UMat();
CvInvoke.CvtColor(img1, uimage, ColorConversion.Bgr2Gray);
UMat mat = new UMat();
CvInvoke.HoughLines(uimage, mat, 1, Math.PI / 180, 20);
here I can see the size is not 0, how can I get these lines, or other function to show them

How to get value of a variable from another class

I am using Emgu CV's SURF and I used int j = CvInvoke.cvCountNonZero(mask); to find matched pair points.
The problem how to return this value into main()?
...
...
...
public static Image<Bgr, Byte> Draw(Image<Gray, Byte> modelImage, Image<Gray, byte> observedImage, out long matchTime)
{
HomographyMatrix homography;
VectorOfKeyPoint modelKeyPoints;
VectorOfKeyPoint observedKeyPoints;
Matrix<int> indices;
Matrix<byte> mask;
FindMatch(modelImage, observedImage, out matchTime, out modelKeyPoints, out observedKeyPoints, out indices, out mask, out homography);
//Draw the matched keypoints
Image<Bgr, Byte> result = Features2DToolbox.DrawMatches(modelImage, modelKeyPoints, observedImage, observedKeyPoints, indices, new Bgr(255, 255, 255), new Bgr(255, 255, 255), mask, Features2DToolbox.KeypointDrawType.DEFAULT);
int j = CvInvoke.cvCountNonZero(mask);
return result;
}
You can create a simple result object:
public class DrawingResult {
public Image<Bgr, Byte> Images { get; private set; }
public int Count {get; private set; }
public DrawingResult(Image<Bgr, Byte> images, int count) {
Images = images;
Count = count;
}
}
And in your method:
public static DrawingResult Draw(Image<Gray, Byte> modelImage, Image<Gray, byte> observedImage, out long matchTime)
{
HomographyMatrix homography;
VectorOfKeyPoint modelKeyPoints;
VectorOfKeyPoint observedKeyPoints;
Matrix<int> indices;
Matrix<byte> mask;
FindMatch(modelImage, observedImage, out matchTime, out modelKeyPoints, out observedKeyPoints, out indices, out mask, out homography);
//Draw the matched keypoints
Image<Bgr, Byte> result = Features2DToolbox.DrawMatches(modelImage, modelKeyPoints, observedImage, observedKeyPoints,
indices, new Bgr(255, 255, 255), new Bgr(255, 255, 255), mask, Features2DToolbox.KeypointDrawType.DEFAULT);
int j = CvInvoke.cvCountNonZero(mask);
return new DrawingResult(result, j);
}
And in the main method:
DrawingResult result = MyClass.Draw(...);
int count = result.Count;
Image<Bgr, Byte> images = result.Images;

Alpha composite images using emgu.cv

Emgu.CV (Nuget package 2.4.2) doesn't as far as I can tell implement the gpu::alphaComp method available in OpenCV.
As such when trying to implement this specific type of composite it is incredible slow in C#, such that it takes up some 80% of the total cpu usage of my app.
This was my original solution that performs really badly.
static public Image<Bgra, Byte> Overlay( Image<Bgra, Byte> image1, Image<Bgra, Byte> image2 )
{
Image<Bgra, Byte> result = image1.Copy();
Image<Bgra, Byte> src = image2;
Image<Bgra, Byte> dst = image1;
int rows = result.Rows;
int cols = result.Cols;
for (int y = 0; y < rows; ++y)
{
for (int x = 0; x < cols; ++x)
{
// http://en.wikipedia.org/wiki/Alpha_compositing
double srcA = 1.0/255 * src.Data[y, x, 3];
double dstA = 1.0/255 * dst.Data[y, x, 3];
double outA = (srcA + (dstA - dstA * srcA));
result.Data[y, x, 0] = (Byte)(((src.Data[y, x, 0] * srcA) + (dst.Data[y, x, 0] * (1 - srcA))) / outA); // Blue
result.Data[y, x, 1] = (Byte)(((src.Data[y, x, 1] * srcA) + (dst.Data[y, x, 1] * (1 - srcA))) / outA); // Green
result.Data[y, x, 2] = (Byte)(((src.Data[y, x, 2] * srcA) + (dst.Data[y, x, 2] * (1 - srcA))) / outA); // Red
result.Data[y, x, 3] = (Byte)(outA*255);
}
}
return result;
}
Is there a way to optimise the above in C#?
I've additionally looked at using OpencvSharp, but that doesn't appear to provide access to gpu::alphaComp neither.
Is there any OpenCV C# wrapper library that can do alpha Compositing?
AddWeighted does not do what I need it to do.
Whilst similar, this question doesn't provide an answer
So so simple.
public static Image<Bgra, Byte> Overlay(Image<Bgra, Byte> target, Image<Bgra, Byte> overlay)
{
Bitmap bmp = target.Bitmap;
Graphics gra = Graphics.FromImage(bmp);
gra.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
gra.DrawImage(overlay.Bitmap, new Point(0, 0));
return target;
}

EmguCV snake function

I am trying to use snake active contour from EmguCV ,but i don't take anything.Here is my code:
Image<Gray, Byte> img = new Image<Gray, Byte>(300, 300, new Gray());
Point center = new Point(100, 100);
double width = 20;
double height = 40;
Rectangle rect = new Rectangle(center, new Size(20, 20));
img.Draw(rect, new Gray(255.0), -1);
using (MemStorage stor = new MemStorage())
{
Seq<Point> pts = new Seq<Point>((int)SEQ_TYPE.CV_SEQ_POLYGON, stor);
pts.Push(new Point(20, 20));
pts.Push(new Point(20, 280));
pts.Push(new Point(280, 280));
pts.Push(new Point(280, 20));
//Image<Gray, Byte> canny = img.Canny(100.0, 40.0);
Seq<Point> snake = img.Snake(pts, 0.1f, 0.5f, 0.4f, new Size(21, 21), new MCvTermCriteria(500, 0.1), stor);
img.Draw(pts, new Gray(120), 1);
img.Draw(snake, new Gray(80), 2);
What i am doing wrong?Any idea?
you miss to draw your initialization points.
I have setup some code for you and for the whole community as there are no emgu snakes sample out there.
private void TestSnake()
{
Image<Gray, Byte> grayImg = new Image<Gray, Byte>(400, 400, new Gray());
Image<Bgr, Byte> img = new Image<Bgr, Byte>(400, 400, new Bgr(255,255,255));
// draw an outer circle on gray image
grayImg.Draw(new Ellipse(new PointF(200,200),new SizeF(100,100),0), new Gray(255.0), -1);
// inner circle on gray image to create a donut shape :-)
grayImg.Draw(new Ellipse(new PointF(200, 200), new SizeF(50, 50), 0), new Gray(0), -1);
// this is the center point we'll use to initialize our contour points
Point center = new Point(200, 200);
// radius of polar points
double radius = 70;
using (MemStorage stor = new MemStorage())
{
Seq<Point> pts = new Seq<Point>((int)Emgu.CV.CvEnum.SEQ_TYPE.CV_SEQ_POLYGON, stor);
int numPoint = 100;
for (int i = 0; i < numPoint; i++)
{ // let's have some fun with polar coordinates
Point pt = new Point((int)(center.X + (radius * Math.Cos(2 * Math.PI * i / numPoint))), (int)(center.Y + (radius * Math.Sin(2 * Math.PI * i / numPoint))) );
pts.Push(pt);
}
// draw contour points on result image
img.Draw(pts, new Bgr(Color.Green), 2);
// compute snakes
Seq<Point> snake = grayImg.Snake(pts, 1.0f, 1.0f, 1.0f, new Size(21, 21), new MCvTermCriteria(100, 0.0002), stor);
// draw snake result
img.Draw(snake, new Bgr(Color.Yellow), 2);
// use for display in a winform sample
imageBox1.Image = grayImg;
imageBox2.Image = img;
}
}
Hope this helps, just change some params and you will be surprised of result.

Categories

Resources