I have done the grabcut implementation interactive in c# but there is one problem which Iam unable to solve. How to smooth the edges. The edges looks rough after applying grabcut. I googled and found out a solution in c++ but I am unable to convert it into c#. Here What I have done so far.
Image<Bgr, Byte> image = new Image<Bgr, Byte>("SourceImage");
Image<Gray, Byte> mask1 = newMask;//Grabcut mask
Image<Bgr, float> maskF = mask1.Convert<Bgr, float>();
Image<Bgr, float> maskF2 = maskF.Mul(1 / 255);
Image<Bgr, float> imageF = image.Convert<Bgr, float>();
var img= imageF.Mul(maskF).Convert<Bgr, byte>();
Image<Bgr, byte> result3 = new Image<Bgr, byte>(imageSource.Width, imageSource.Height);
imageSource.Copy(result3, newMask);Pictureboxoutput.image=(result3+img).ToBitMap();
OutputImageInputImage
Related
this code recognizes the image using ''template matching'', I would like the mouse to make a movement when recognizing the figure using mouse_event.
example: when recognizing person1, make the automatic movement using ''pattern'' and that I can place the angles to make the movement.
each character have their own ''pattern'', so I can put the moves!
namespace Auto_Person_Detection
{
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 15, 15));
Detector.RunWorkerAsync();
}
private void Detector_DoWork(object sender, DoWorkEventArgs e)
{
Graphics g;
Bitmap bmp;
bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width / 8, Screen.PrimaryScreen.Bounds.Height / 4);
g = Graphics.FromImage(bmp);
Image<Gray, Byte>[] guns = new Image<Gray, Byte>[] {
new Image<Gray, Byte>(#"./img/Person1.png"),
new Image<Gray, Byte>(#"./img/Person2.png"),
new Image<Gray, Byte>(#"./img/Person3.png"),
new Image<Gray, Byte>(#"./img/Person4.png"),
new Image<Gray, Byte>(#"./img/Person5.png"),
new Image<Gray, Byte>(#"./img/Person6.png"),
new Image<Gray, Byte>(#"./img/Person7.png"),
new Image<Gray, Byte>(#"./img/Person8.png"),
new Image<Gray, Byte>(#"./img/Person9.png"),
new Image<Gray, Byte>(#"./img/Person10.png"),
new Image<Gray, Byte>(#"./img/Person11.png"),
new Image<Gray, Byte>(#"./img/Person12.png"),
new Image<Gray, Byte>(#"./img/Person13.png"),
new Image<Gray, Byte>(#"./img/Person14.png"),
new Image<Gray, Byte>(#"./img/Person15.png"),
new Image<Gray, Byte>(#"./img/Person16.png"),
new Image<Gray, Byte>(#"./img/Person17.png"),
new Image<Gray, Byte>(#"./img/Person18.png")
};
I'm trying to use TemplateMatching from EMGU CV in order to find(match) a template image in a source image.
Below is the code (sample taken from Stack Overflow)
public static void TemplateMatch()
{
Image<Bgr, Byte> sourceImg = new Image<Bgr, Byte>(#"D:\ImageA.png");
Image<Bgr, Byte> templateImg = new Image<Bgr, Byte>(#"D:\ImageB.png");
Image<Bgr, byte> lastImage = sourceImg.Copy();
using (Image<Gray, float> resultImg = sourceImg.MatchTemplate(templateImg, Emgu.CV.CvEnum.TemplateMatchingType.CcorrNormed))
{
double[] minVal, maxVal;
System.Drawing.Point[] minLocations, maxLocations;
resultImg.MinMax(out minVal, out maxVal, out minLocations, out maxLocations);
if (maxVal[0] > 0.9)
{
Rectangle match = new Rectangle(maxLocations[0], templateImg.Size);
lastImage.Draw(match, new Bgr(Color.Red), 3);
}
ImageViewer.Show(lastImage);
}
}
But the disadvantage is that if I try to match a template image of different size with the source image, then there is no result.
I have been suggested to loop over the scales of the image in order to find the match. But I'm not sure how to do it in C#.
Here my aim is to find image A (template image) in image B (source image) using template matching in C# irrespective of the scaling and resolution.
I try to use disparity map calculation in C# using Emgu.CV
I read the images from this article as bitmapLeft and bitmapRight. For reference I used the example code from here
Here is my source code:
bitmapLeft = (Bitmap) mainForm.pictureBoxLeft.Image;
bitmapRight = (Bitmap)mainForm.pictureBoxRight.Image;
Image<Gray, Byte> imageLeft = new Image<Gray, Byte>(bitmapLeft);
Image<Gray, Byte> imageRight = new Image<Gray, Byte>(bitmapRight);
Image<Gray, Byte> imageDisparity = new Image<Gray, Byte>(bitmapLeft.Width, bitmapLeft.Height);
StereoBM stereoBM = new StereoBM(16, 15);
StereoMatcherExtensions.Compute(stereoBM, imageLeft, imageRight, imageDisparity);
Image bitmapDisparity = imageDisparity.ToBitmap();
However, the resulting bitmap is all black.
I think your problem is at the end. The result of calling StereoMatcherExtentions.Comput is a Mat/Image that has a depth of Cv16S, I converted that back to Cv8U and was able to display it. Here is my example. I used the same two images.
Mat leftImage = new Mat(#"C:\Users\jones_d\Desktop\Disparity\LeftImage.png", ImreadModes.Grayscale);
Mat rightImage = new Mat(#"C:\Users\jones_d\Desktop\Disparity\\RightImage.png", ImreadModes.Grayscale);
CvInvoke.Imshow("Left", leftImage);
CvInvoke.Imshow("Right", rightImage);
Mat imageDisparity = new Mat();
StereoBM stereoBM = new StereoBM(16, 15);
StereoMatcherExtensions.Compute(stereoBM, leftImage, rightImage, imageDisparity);
Mat show = new Mat();
imageDisparity.ConvertTo(show, DepthType.Cv8U);
CvInvoke.Imshow("Disparity", show);
CvInvoke.WaitKey(0);
Here are the images:
Which seems to match the result at: Depth Map from Image
Doug
I have two sets of images which have the same size and pixels. Now I have to compare selectedFrame which is the 1st image to backImageFrame which is the 2nd image. I need to get the difference in the images and extract it so I can output it in a ImageBox. Now, I am using AbsDiff function of EmguCV
selectedFrame.ROI = recArray[random];
backImageFrame.ROI = recArray[random];
// backImageFrame = selectedFrame.AbsDiff(backImageFrame);
CvInvoke.AbsDiff(selectedFrame, backImageFrame, backImageFrame)
imgTry.Image = backImageFrame;
imageBox1.Image = selectedFrame;
The imgTry ImageBox doesn't have any value in it
You can use the Image API to find the difference between one image and the other, then you can define a threshold for the difference to be considered and apply that.
The code will be something like:
Image<Bgr, Byte> Frame; //current Frame from camera
Image<Bgr, Byte> Previous_Frame; //Previiousframe aquired
Image<Bgr, Byte> Difference; //Difference between the two frames
int Threshold = 60; //stores threshold for thread access
Difference = Previous_Frame.AbsDiff(Frame); //find the absolute difference
/*Play with the value 60 to set a threshold for movement*/
Difference = Difference.ThresholdBinary(new Bgr(Threshold, Threshold, Threshold), new Bgr(255,255,255)); //if value > 60 set to 255, 0 otherwise
do followup with this example to better understand.
This works for me.
Image<Gray, Byte> img1 = picPrev.Convert<Gray, Byte>();
Image<Gray, Byte> img2 = picCurrent.Convert<Gray, Byte>();
Image<Gray, Byte> img3;
img3 = img1 - img2; //Here the difference is applied.
pictureBox3.Image = img3.ToBitmap();
EmguCV AbsDiff based comparison
Bitmap inputMap = //bitmap source image
Image<Gray, Byte> sourceImage = new Image<Gray, Byte>(inputMap);
Bitmap tempBitmap = //Bitmap template image
Image<Gray, Byte> templateImage = new Image<Gray, Byte>(tempBitmap);
Image<Gray, byte> resultImage = new Image<Gray, byte>(templateImage.Width,
templateImage.Height);
CvInvoke.AbsDiff(sourceImage, templateImage, resultImage);
double diff = CvInvoke.CountNonZero(resultImage);
diff = (diff / (templateImage.Width * templateImage.Height)) * 100; // this will give you the difference in percentage
As per my experience, this is the best method compared to MatchTemplate based comparison. Match template failed to capture very minimal changes in two images.
But AbsDiff will be able to capture very small difference as well
Hi in opencv c+ method Findcontours return the array hierarchy and to get the boundaries of the hole I can get the hierarchy .
how can i get these boundaries in emgu cv please any help?
how can i find holes in emgu cv?
You can get the Contour hierarchy in Emgucv by using this following code.
Image<Bgr, byte> Img_Result_Bgr = Img_Source_Bgr.Copy();
Image<Gray, byte> Img_Org_Gray = Img_Source_Bgr.Convert<Gray, byte>();
Image<Gray, byte> Img_CannyEdge_Gray = new Image<Gray, byte>(Img_Source_Bgr.Width,Img_Source_Bgr.Height);
Img_CannyEdge_Gray = Img_Org_Gray.Canny(10, 50);
Img_Org_Gray.Dispose();
Random Rnd = new Random();
#region Finding Contours
using (MemStorage storage = new MemStorage()) //allocate storage for contour approximation
for (Contour<Point> contours = Img_CannyEdge_Gray.FindContours(); contours != null; contours = contours.HNext)
{
Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);//if you want to Approximate the contours into a polygon play with this function().
if (contours.Area > 100) //only consider contours with area greater than 100
{
Img_Result_Bgr.Draw(contours, new Bgr(Rnd.Next(255),Rnd.Next(255),Rnd.Next(255)), 2);
}
}
#endregion
Img_CannyEdge_Gray.Dispose();
imageBox1.Image = Img_Result_Bgr;
For further Reference use this Online Tutor!
Here is the output of this code.
http://s18.postimg.org/511xwpm15/Forum_Contour.jpg