Why Contours ar not shown as blue after canny edge detection - image-processing

I want to find contours after canny edge detection. But they shown up in white color even though BGR is set as (255,0,0).
What is wrong in the code?
private void Capture_ImageGrabbed1(object sender, EventArgs e)
{
try
{
Mat m = new Mat();
capture.Retrieve(m);
pic1.Image = m.ToImage<Bgr, byte>().Bitmap;
if(mode == 1)
{
var image = m.ToImage<Bgr, byte>();
var grayScaleImage = image.Convert<Gray, byte>();
var blurredImage = grayScaleImage.SmoothGaussian(5, 5, 0, 0);
var cannyImage = new UMat();
CvInvoke.Canny(blurredImage, cannyImage, 10, 50);
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
CvInvoke.FindContours(cannyImage, contours, null, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
CvInvoke.DrawContours(cannyImage, contours, -1, new MCvScalar(255, 0, 0),2);
pic2.Image = cannyImage.Bitmap;
}
}
catch (Exception)
{
throw;
}
}
pic2.Image is the picturebox. Code is working but no blue contours as in attached enter image description herepicture.

private void Capture_ImageGrabbed1(object sender, EventArgs e)
{
try
{
Mat m = new Mat();
capture.Retrieve(m);
pic1.Image = m.Bitmap;
if(mode == 1)
{
var image = m.ToImage<Bgr, byte>();
var grayScaleImage = image.Convert<Gray, byte>();
var blurredImage = grayScaleImage.SmoothGaussian(5, 5, 0, 0);
var cannyImage = new UMat();
CvInvoke.Canny(blurredImage, cannyImage, 10, 50);
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
CvInvoke.FindContours(cannyImage, contours, null, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
var cannyOut = cannyImage.ToImage<Bgr, byte>();
CvInvoke.DrawContours(cannyOut, contours, -1, new MCvScalar(255, 0, 0),2);
pic2.Image = cannyOut.Bitmap;
}
}
catch (Exception)
{
throw;
}
}

Related

I can't find why memory isn't release using in SharpDX

I'm making on some Winform application, I noticed my program's memory issue.
This is my winform custom control code.
using System;
using System.Drawing;
using System.Windows.Forms;
using DX = SharpDX;
using D2D = SharpDX.Direct2D1;
using SharpDX.Mathematics.Interop;
using DW = SharpDX.DirectWrite;
namespace WinFormTest
{
public partial class BitmapSurface : Control
{
D2D.Factory d2dFactory;
DW.Factory dwFactory;
D2D.WindowRenderTarget wrt;
D2D.BitmapRenderTarget brt;
Rectangle clippingRect = new Rectangle(0, 0, 100, 100);
public BitmapSurface()
{
InitializeComponent();
InitializeRenderer();
}
DX.Size2 clientSize2;
DX.Size2F clientSize2f;
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (clientSize2 == null)
clientSize2 = new DX.Size2();
if (clientSize2f == null)
clientSize2f = new DX.Size2F();
clientSize2.Width = ClientSize.Width;
clientSize2.Height = ClientSize.Height;
clientSize2f.Width = ClientSize.Width;
clientSize2f.Height = ClientSize.Height;
if (wrt != null)
{
wrt.Resize(clientSize2);
}
if (brt != null)
{
brt.Dispose();
brt = new D2D.BitmapRenderTarget(wrt, D2D.CompatibleRenderTargetOptions.None, clientSize2f, null, null);
brt.AntialiasMode = D2D.AntialiasMode.Aliased;
}
this.Invalidate();
}
private D2D.SolidColorBrush GetBrush(float r, float g, float b, float a = 255)
{
var brush = new D2D.SolidColorBrush(
wrt, new RawColor4(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f));
return brush;
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
clippingRect = this.ClientRectangle;
var rect = clippingRect;
DrawBitmap();
}
public void InitializeRenderer()
{
int width = this.ClientSize.Width;
int height = this.ClientSize.Height;
D2D.HwndRenderTargetProperties hrtp = new D2D.HwndRenderTargetProperties();
hrtp.Hwnd = this.Handle;
hrtp.PixelSize = new DX.Size2(width, height);
// D2DFactory
if (d2dFactory == null)
d2dFactory = new D2D.Factory();
// DirectWrite
if (dwFactory == null)
dwFactory = new DW.Factory();
wrt = new D2D.WindowRenderTarget(d2dFactory, new D2D.RenderTargetProperties(), hrtp);
wrt.AntialiasMode = D2D.AntialiasMode.Aliased;
brt = new D2D.BitmapRenderTarget(wrt, D2D.CompatibleRenderTargetOptions.None, new DX.Size2F(width, height), null, null);
brt.AntialiasMode = D2D.AntialiasMode.Aliased;
}
public void DrawBitmap()
{
brt.BeginDraw();
brt.Clear(new RawColor4(0, 0, 0, 1));
// Draw Something
for (int i = 0; i < 1000; i++)
{
brt.DrawLine(
new RawVector2(0, i),
new RawVector2(90, 90 + i),
GetBrush(255, 255, 255));
}
for (int i = 0; i < 1000; i++)
{
brt.FillRectangle(
new RawRectangleF(90, 0 + i, 150, 10 + i),
GetBrush(255 - i, 255 - i, 255 - i));
}
brt.EndDraw();
wrt.BeginDraw();
wrt.DrawBitmap(brt.Bitmap, 1, D2D.BitmapInterpolationMode.NearestNeighbor);
wrt.EndDraw();
}
}
}
My program's mainform can have many childs, Child draw somethings with SharpDX.
Memory usage is increase When I open child forms, but after closing child form and GC.Collect memory usage is NOT decrease.
Is this bad usage for SharpDX?

How to draw rectangle when form is resized

enter code hereI'm using OpenCv and displaying frames which i get from video file on the imagebox. Now I'm drawing multiple rectangles on the frame and trying to move and delete them. This is working fine. But, when i'm resizing the form...i can't draw,move or delete the rectangles at the correct position as the frame coordinates are not getting changed but only imagebox coordinates are being changed and the frame is fitted into it.Please help me out.
black rectangle is where i have drawn the rectangle after form is resized...red rectangle is where the rectangle is being drawn
private void imageBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
drawRect = true;
RectStartpt = e.Location;
if (count > 0)
{
foreach (Rectangle allRect in Rect_List)
{
rect_count++;
if (allRect.Contains(e.Location))
{
Cursor cursor = Cursors.Cross;
drawRect = false;
RectStartpt = new Point(e.X, e.Y);
rect = allRect;
break;
}
}
}
}this.Invalidate();}
private void imageBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left && e.Button != MouseButtons.Right)
{
return;
}
else if (e.Button == MouseButtons.Left)
{
Point EndPt = new Point(e.X, e.Y);
Size shift = new Size(Math.Abs(RectStartpt.X - EndPt.X), Math.Abs(RectStartpt.Y - EndPt.Y));
if (drawRect == true)
{
rect.Location = new Point(Math.Min(RectStartpt.X, EndPt.X), Math.Min(RectStartpt.Y, EndPt.Y));
rect.Size = shift;
count = 1;
}
else if (drawRect == false)
{
rect.X += e.X - RectStartpt.X;
rect.Y += e.Y - RectStartpt.Y;
RectStartpt = new Point(e.X, e.Y);
}
}
}
private void imageBox1_MouseUp(object sender, MouseEventArgs e)
{
if (frame != null)
{
if (drawRect == true)
{
frame.Draw(rect, new Bgr(Color.Red), 2);
Rect_List.Add(rect);
}
else if (drawRect == false)
{
frame.Draw(rect, new Bgr(Color.Red), 2);
Rect_List.RemoveAt(rect_count - 1);
Rect_List.Add(rect);
if (Timer_enabled == false)
{
frame = captureFrame.QueryFrame().ToImage<Bgr, Byte>();
foreach (Rectangle allRect in Rect_List)
{
frame.Draw(allRect, new Bgr(Color.Red), 2);
}
imageBox1.Image = frame;
imageBox1.Refresh();
rect = new Rectangle();
frame_no++;
}
}
imageBox1.Image = frame;
imageBox1.Refresh();
//rect = new Rectangle();
rect_count = 0;
}
}
private void Form1_Resize(object sender, EventArgs e)
{
formGraphics = null;
formGraphics = imageBox1.CreateGraphics();
frame = frame.Resize(imageBox1.Width, imageBox1.Height, Inter.Linear);
imageBox1.Image = frame;
}

JavaCV FaceRecognizer predict not working

I'm using opencv 2.4.6 and javacv 0.6. I'm trying to make face recognizer.
This is my code:
FaceRecognizer ef = createEigenFaceRecognizer(1, 0.00000001);
int facewidth = 92, faceheight = 112;
private boolean stopRec = false;
List<String> names = new ArrayList<String>();
public void recognize(IplImage face) {
int predicted;
int [] tabPredicted = new int[2];
double[] predConfTab = new double[2];
IplImage resizedFace = IplImage.create(new CvSize(facewidth, faceheight), IPL_DEPTH_8U, 1);
cvResize(face, resizedFace);
if (names.size() != 0)
{
ef.predict(resizedFace, tabPredicted, predConfTab);
predicted = tabPredicted[0];
}
else
{
predicted = -1;
}
if(predicted == -1 )
{
//adding user like:
int i = names.size();
names.add(name);
System.out.println("Identified new person: " + names.get(i));
MatVector mv = new MatVector(1);
mv.put(0, resizedFace);
int[] u = new int[] {i};
ef.train(mv, u);
}
I tried lot of configurations. I'm sure that i have valid face image in grayscale. The problem is that after ef.predict(resizedFace, tabPredicted, predConfTab);
tabPredicted[0] is always index of last added user and predConfTab[0] always equals 0, so it means that any faces exacly matches the last one added.

Drawing 2D lines in XNA using rotations

I'm trying to draw a line from the player to a targets asteroid as a "Grapple" hook, i've found some examples for doing this in 2D in xna but my "Position" and "origin" vector2 seems to change depending on who and when they are used. In the case of the lines they appear to draw up and right of the position (roughly 100 pixels) in the opposite direction of the target and rotate about an origin somewhere to the left of the target as the player moves.
here is the player code including the grapple code and target assignment
namespace Sparatius.Sprites
{
public class PlayerSprite : BaseSprite
{
ControlInput controlInput;
Texture2D Grapple;
SpriteFont font;
bool LeftGrapple = false, RightGrapple = false;
int GrappleRange = 300;
Sprites.AsteroidSprite LeftTarget, RightTarget;
public BoundingSphere grappleHitBox
{
get { return new BoundingSphere(new Vector3(Position.X + Origin.X, Position.Y + Origin.Y, 0), GrappleRange); }
}
public float speed
{
get { return Speed; }
}
public PlayerSprite(Vector2 spriteLocal)
:base(spriteLocal)
{
this.Rotation = 0;
this.Speed = 0;
this.FrameCount = new Point(4, 2);
this.ColorTint = Color.White;
this.controlInput = new ControlInput();
}
public void LoadContent(ContentManager content)
{
Texture = content.Load<Texture2D>("Sprites/PinballSpin");
font = content.Load<SpriteFont>("Fonts/Font1");
Grapple = content.Load<Texture2D>("Sprites/Grapple");
FrameSize = new Point((int)Texture.Width / FrameCount.X, (int)Texture.Height / FrameCount.Y);
Origin = new Vector2(FrameSize.X / 2, FrameSize.Y / 2);
Animation = new Animation(Texture, FrameSize);
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
controlInput.GetControlStates();
if (controlInput.JustPressed(Keys.W))
Speed += 2;
else if (controlInput.JustPressed(Keys.S))
Speed -= 2;
if (controlInput.IsHeld(Keys.A))
Rotation -= 0.05f;
if (controlInput.IsHeld(Keys.D))
Rotation += 0.05f;
if (LeftTarget != null)
{
LeftTarget.Distance = Vector2.Distance(Position, LeftTarget.Position);
if (LeftTarget.Distance > GrappleRange)
{
LeftTarget.isTarget = false;
LeftTarget = null;
}
if (controlInput.IsHeld(Keys.Q))
{
LeftGrapple = true;
}
else
LeftGrapple = false;
}
if (RightTarget != null)
{
RightTarget.Distance = Vector2.Distance(Position, RightTarget.Position);
if (RightTarget.Distance > GrappleRange)
{
RightTarget.isTarget = false;
RightTarget = null;
}
if (controlInput.IsHeld(Keys.E))
{
RightGrapple = true;
}
else
RightGrapple = false;
}
}
public override void Draw(SpriteBatch spriteBatch)
{
base.Draw(spriteBatch);
if (LeftGrapple)
{
float leftRotation = (float)Math.Atan2(LeftTarget.Position.Y - Position.Y, LeftTarget.Position.X - Position.X);
//spriteBatch.Draw(Texture, Position, null, ColorTint, leftRotation, Position, 1f, SpriteEffects.None, 0f);
spriteBatch.Draw(Grapple,
new Rectangle((int)Position.X, (int)Position.Y, 2, (int)LeftTarget.Distance),
null, Color.Blue, leftRotation, Position, SpriteEffects.None, 0f);
}
if (RightGrapple)
{
float rightRotation = (float)Math.Atan2(RightTarget.Position.Y - Position.Y, RightTarget.Position.X - Position.X);
//spriteBatch.Draw(Texture, Position, null, ColorTint, rightRotation, Position, 1f, SpriteEffects.None, 0f);
spriteBatch.Draw(Grapple,
new Rectangle((int)Position.X, (int)Position.Y, 2, (int)RightTarget.Distance),
null, Color.Blue, rightRotation, Position, SpriteEffects.None, 0f);
}
spriteBatch.DrawString(font, "Player Rotation: " + Rotation, Position, Color.Red);
spriteBatch.DrawString(font, "Player RoationDegree: " + (int)MathHelper.ToDegrees(Rotation), origin, Color.Blue);
}
public void GrappleCheck(AsteroidSprite target)
{
float targetTragectory = (float)Math.Atan2(Position.Y - target.Position.Y, Position.X - target.Position.X);
if ((targetTragectory < (rotation - (float)MathHelper.PiOver4)) && ((targetTragectory > (rotation - (float)MathHelper.Pi + (float)MathHelper.PiOver4))))
{
target.Distance = Vector2.Distance(Position, target.Position);
if (LeftTarget != null)
{
if (LeftTarget.Distance > target.Distance)
{
LeftTarget.isTarget = false;
LeftTarget = target;
LeftTarget.isTarget = true;
}
}
else
{
LeftTarget = target;
LeftTarget.isTarget = true;
}
}
if ((targetTragectory > (rotation + (float)MathHelper.PiOver4)) && ((targetTragectory < (rotation + (float)MathHelper.Pi - (float)MathHelper.PiOver4))))
{
target.Distance = Vector2.Distance(Position, target.Position);
if (RightTarget != null)
{
if (RightTarget.Distance > target.Distance)
{
RightTarget.isTarget = false;
RightTarget = target;
RightTarget.isTarget = true;
}
}
else
{
RightTarget = target;
RightTarget.isTarget = true;
}
}
}
}
}
any idea whats going wrong? cheers
public static void DrawLine(SpriteBatch spriteBatch, Vector2 begin, Vector2 end, Color color, int width = 1)
{
Rectangle r = new Rectangle((int)begin.X, (int)begin.Y, (int)(end - begin).Length()+width, width);
Vector2 v = Vector2.Normalize(begin - end);
float angle = (float)Math.Acos(Vector2.Dot(v, -Vector2.UnitX));
if (begin.Y > end.Y) angle = MathHelper.TwoPi - angle;
spriteBatch.Draw(Pixel, r, null, color, angle, Vector2.Zero, SpriteEffects.None, 0);
}
Pixel is just a 1x1 sprite
You can also use the this keyword to make a handy extension method.

Why is the CvBlobDetector in openCV in the legacy lib?

Is there a newer blob detection/tracking library?
Is it not a good library?
Isn't legacy supposed to be old and not useful code?
Does anybody know?
Here is newer blob detector:
http://opencv.itseez.com/modules/features2d/doc/common_interfaces_of_feature_detectors.html#SimpleBlobDetector
Here is my code which i used to track the blob in Emgucv 3.1 Version
//MCvFont font = new MCvFont(Emgu.CV.CvEnum.FontFace.HersheySimplex, 0.5, 0.5);
using (CvTracks tracks = new CvTracks())
using (ImageViewer viewer = new ImageViewer())
using (Capture capture = new Capture())
using (Mat fgMask = new Mat())
{
//BGStatModel<Bgr> bgModel = new BGStatModel<Bgr>(capture.QueryFrame(), Emgu.CV.CvEnum.BG_STAT_TYPE.GAUSSIAN_BG_MODEL);
BackgroundSubtractorMOG2 bgModel = new BackgroundSubtractorMOG2(0, 0, true);
//BackgroundSubstractorMOG bgModel = new BackgroundSubstractorMOG(0, 0, 0, 0);
capture.ImageGrabbed += delegate(object sender, EventArgs e)
{
Mat frame = new Mat();
capture.Retrieve(frame);
bgModel.Apply(frame, fgMask);
using (CvBlobDetector detector = new CvBlobDetector())
using (CvBlobs blobs = new CvBlobs())
{
detector.Detect(fgMask.ToImage<Gray, Byte>(), blobs);
blobs.FilterByArea(100, int.MaxValue);
tracks.Update(blobs, 20.0, 10, 0);
Image<Bgr, Byte> result = new Image<Bgr, byte>(frame.Size);
using (Image<Gray, Byte> blobMask = detector.DrawBlobsMask(blobs))
{
frame.CopyTo(result, blobMask);
}
//CvInvoke.cvCopy(frame, result, blobMask);
foreach (KeyValuePair<uint, CvTrack> pair in tracks)
{
if (pair.Value.Inactive == 0) //only draw the active tracks.
{
CvBlob b = blobs[pair.Value.BlobLabel];
Bgr color = detector.MeanColor(b, frame.ToImage<Bgr, Byte>());
result.Draw(pair.Key.ToString(), pair.Value.BoundingBox.Location, Emgu.CV.CvEnum.FontFace.HersheySimplex, 0.5, color);
result.Draw(pair.Value.BoundingBox, color, 2);
Point[] contour = b.GetContour();
result.Draw(contour, new Bgr(0, 0, 255), 1);
}
}
viewer.Image = frame.ToImage<Bgr, Byte>().ConcateVertical(fgMask.ToImage<Bgr, Byte>().ConcateHorizontal(result));
}
};
capture.Start();
viewer.ShowDialog();
}

Resources