Reputation: 847
I'm trying to draw on an image
Graphics g = Graphics.FromImage(BWImage);
Pen red = new Pen(Color.Red, 2);
foreach (Blob blob in blobs)
{
List<IntPoint> leftPoints, rightPoints, edgePoints = new List<IntPoint>();
// get blob's edge points
blobCounter.GetBlobsLeftAndRightEdges(blob,
out leftPoints, out rightPoints);
edgePoints.AddRange(leftPoints);
edgePoints.AddRange(rightPoints);
// blob's convex hull
List<IntPoint> hull = hullFinder.FindHull(edgePoints);
g.DrawPolygon(red, ToPointsArray(hull));
}
I get frames from a webcam, and for each frame I do some processing then do that code, but it didn't work, I tried to debug it, I found that the line
Graphics g = Graphics.FromImage(BWImage);
It keeps executing forever without executing any line after it!
Any help please? Thanks.
Here is my code
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
ProcessImage(bitmap);
}
and the ProcessImage method:
#region Skin Detection
YCbCrFiltering YCbCrFilter = new YCbCrFiltering();
YCbCrFilter.Y = new Range(0, 1);
YCbCrFilter.Cb = new Range(-0.1862745098039216f, 0.0294117647058824f);
YCbCrFilter.Cr = new Range(0.0137254901960784f, 0.2254901960784314f);
// apply the filter
var YCbCrFilteredImage = YCbCrFilter.Apply(bitmap);
#endregion
#region GrayScale
// create grayscale filter (BT709)
Grayscale Gray_filter = new Grayscale(0.2125, 0.7154, 0.0721);
// apply the filter
Bitmap grayImage = Gray_filter.Apply(YCbCrFilteredImage);
Threshold threshold = new Threshold(50);
Bitmap BWImage = threshold.Apply(grayImage);
#endregion
#region Remove noise
// create filter
BlobsFiltering BlobsFilteringfilter = new BlobsFiltering();
// configure filter
BlobsFilteringfilter.CoupledSizeFiltering = true;
BlobsFilteringfilter.MinWidth = 150;
BlobsFilteringfilter.MinHeight = 150;
BlobsFilteringfilter.MaxHeight = 600;
BlobsFilteringfilter.MaxWidth = 600;
// apply the filter
var BWImageFiltered = BlobsFilteringfilter.Apply(BWImage);
#endregion
#region Fill Holes
FillHoles FillHolesfilter = new FillHoles();
FillHolesfilter.MaxHoleHeight = 50;
FillHolesfilter.MaxHoleWidth = 50;
FillHolesfilter.CoupledSizeFiltering = false;
var BWImageFilteredHoles = FillHolesfilter.Apply(BWImageFiltered);
#endregion
#region Contouring
// process image with blob counter
BlobCounter blobCounter = new BlobCounter();
blobCounter.ProcessImage(BWImageFilteredHoles);
Blob[] blobs = blobCounter.GetObjectsInformation();
Graphics g = Graphics.FromImage(BWImageFilteredHoles);
Pen red = new Pen(Color.Red, 2);
// create convex hull searching algorithm
GrahamConvexHull hullFinder = new GrahamConvexHull();
foreach (Blob blob in blobs)
{
List<IntPoint> AllEdgesPoints = new List<IntPoint>();
List<IntPoint> leftPoints, rightPoints, edgePoints = new List<IntPoint>();
// get blob's edge points
blobCounter.GetBlobsLeftAndRightEdges(blob,
out leftPoints, out rightPoints);
edgePoints.AddRange(leftPoints);
edgePoints.AddRange(rightPoints);
// blob's convex hull
List<IntPoint> hull = hullFinder.FindHull(edgePoints);
g.DrawPolygon(red, ToPointsArray(hull));
g.Dispose();
#endregion
pictureBox1.Image = grayImage;
pictureBox2.Image = BWImage;
}
Upvotes: 0
Views: 3057
Reputation: 700
Is there an exception handler around this code that isn't shown? Graphics.FromImage()
should be throwing an exception because you're passing an image with an indexed pixel format; Grayscale.Apply()
returns an image with Format8bppIndexed
, and BlobsFiltering.Apply()
would preserve that.
Bottom line, you need to create a new Bitmap
with the same size as BWImage
, get your Graphics
from that, paint BWImage
onto the new image, then rectangle your blobs.
Upvotes: 0