EmguCV never open ".jpg" file but open only ".tif" files..Why? - c#

I am using EmguCV in Visual Studio 2022 and I am simple trying to read and open my image but it only opens .tif image files not .jpg or any other file...
When I try to open any .jpg file it gives me an exception openCV:index out of bound
Here:
I also debugged it and find something like it is not reading file when I passed it to EmguCV image structure "Image<Bgr,byte> imgFile=new Image<Bgr,byte>(filename)"
while it is loading file from dialogue but not passing forward from this structure.
Here's the Debug SS:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.Util;
namespace test_EmguCV
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
Image<Bgr, byte> imgFile = new Image<Bgr, byte>(ofd.FileName);
pictureBox1.Image = imgFile.Bitmap;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void exToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Another Imp:
My friend is also doing the same work with me and he is using the same ide same mehtods same references same dll files but his is running fine but not of mine..

I just tried your code snippet in a new Form and I can say that I can successfully load a .jpg image and display it in a PictureBox. Make sure your image is not corrupted in the first place.
Here's my code:
private void button1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
Image<Bgr, byte> imgFile = new Image<Bgr, byte>(ofd.FileName);
pictureBox1.Image = imgFile.ToBitmap();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Note that it's imgFile.ToBitmap(), not imgFile.Bitmap()
Also I would suggest you to switch to Mat instead of Image<>. More info from OpenCV docs
With Mat the code to get the image is as easy as:
Mat imgFile = new Mat(ofd.FileName);
More info about Mat in EmguCV here

Related

How to properly take image and save image?

I made a Win-Forms application and want to Read a Image from the Webcam and save it to the disk and show it in a picture box. Everything works fine, but when i call the "Capture_Image()" function several times (on button14-click) i get a Error like "Object already in use"
using System;
using System.IO;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Runtime.InteropServices;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
private VideoCapture capture = null;
private Bitmap tempImg = null;
public Form1()
{
InitializeComponent();
capture = new VideoCapture();
}
public void Capture_Image()
{
try
{
capture.Start();
tempImg = capture.QueryFrame().Bitmap;
pictureBox2.Image = tempImg;
pictureBox2.Refresh();
tempImg.Save("C:/FHT59N3/Bildanalyse_Projekt/image.jpg",
System.Drawing.Imaging.ImageFormat.Jpeg);
capture.Stop();
}
catch (NullReferenceException)
{
string message = "No Camera found";
string title = "Please connect Camera";
MessageBoxButtons buttons = MessageBoxButtons.OK;
MessageBox.Show(message, title, buttons, MessageBoxIcon.Warning);
}
}
private void button14_Click(object sender, EventArgs e)
{
Capture_Image();
}
Am i doing something wrong with the capture? Why do i get this error

ArgumentException "parameter is incorrect"

I'm trying to make a code that converts memorystream to a png image, but I'm getting a ArgumentException "parameter is incorrect" error on using(Image img = Image.FromStream(ms)). It doesn't specify it any further so I don't know why I'm getting the error and what am I supposed to do to it.
Also, how do I use the Width parameter with img.Save(filename + ".png", ImageFormat.Png);? I know I can add parameters and it recognizes "Width", but I have no idea how it should be formatted so visual studio would accept it.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
MemoryStream ms = new MemoryStream();
public string filename;
private void button1_Click(object sender, EventArgs e)
{
OpenFile();
}
private void button2_Click(object sender, EventArgs e)
{
ConvertFile();
}
private void OpenFile()
{
OpenFileDialog d = new OpenFileDialog();
if(d.ShowDialog() == DialogResult.OK)
{
filename = d.FileName;
var fs = d.OpenFile();
fs.CopyTo(ms);
}
}
private void ConvertFile()
{
using(Image img = Image.FromStream(ms))
{
img.Save(filename + ".png", ImageFormat.Png);
}
}
}
}
I suspect the problem is with how you're reading the file here:
fs.CopyTo(ms);
You're copying the content of the file into the MemoryStream, but then leaving the MemoryStream positioned at the end of the data rather than the start. You can fix that by adding:
// "Rewind" the memory stream after copying data into it, so it's ready to read.
ms.Position = 0;
You should consider what happens if you click on the buttons multiple times though... and I'd strongly advise you to use a using directive for your FileStream, as currently you're leaving it open.

getting Exception in HaarCascade.cs file using Emgucv

I am showing a simple version of my project which i am trying to develop.I continuously getting error in haarCascade.cs that TypeIntilizationException was unhandled.This is my code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.CvEnum;
using System.IO;
namespace Browseface
{
public partial class Form1 : Form
{
private HaarCascade haar;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
haar = new HaarCascade(#"C:\Users\Balram\Desktop\Frontal Face HaarCascades\Frontal Face HaarCascades\haarcascade_frontalface_alt_tree.xml");
}
catch (Exception ae)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Image inputimg = Image.FromFile(#"C:\Users\Balram\Desktop\ad.jpg");
Image<Bgr, byte> imageframe = new Image<Bgr, byte>(new Bitmap(inputimg));
if (imageframe != null)
{
Image<Gray, byte> grayform = imageframe.Convert<Gray, byte>();
var faces =
grayform.DetectHaarCascade(haar, 1.4, 4, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(25, 25))[0];
foreach (var face in faces)
{
imageframe.Draw(face.rect, new Bgr(Color.Green), 3);
}
pictureBox1.Image = imageframe.ToBitmap();
}
}
catch (Exception at)
{
}
}
}
}
In my HaarCascade.cs file in this following line i getting an error
protected override void DisposeObject()
{
CvInvoke.cvReleaseHaarClassifierCascade(ref _ptr);
}
That TypeIntilizationException was unhandled.
(I already add in references Emgu.CV ,Emgu.CV.UI and Emgu.util dll files

select multiple xml files using file dialog and saving them

HI I have several xml files , I select them using open file dialog, then delete duplicate data from those files and now I wanted every individual files to saved as.bak file I can select multiple files and delete those data from files but dont no how to save those files after deleting.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO.Path;
namespace XML_Deletes
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
//single indivual file
var doc = XDocument.Load(#"C:\\Users\IT-Administrator\Desktop\21.xml");
doc.Root.Elements("Incident")
.GroupBy(s => (string)s.Element("Comment"))
.SelectMany(g => g.Skip(1))
.Remove();
//doc.Save(#"C:\Users\IT-Administrator\Desktop\22.xml");
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "XML files(.xml)|*.xml|all Files(*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = true;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
{
if (!String.Equals(Path.GetExtension(openFileDialog1.FileName),
".xml",
StringComparison.OrdinalIgnoreCase))
{
// Invalid file type selected; display an error.
MessageBox.Show("The type of the selected file is not supported by this application. You must select an XML file.",
"Invalid File Type",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
}
} }
}
}
Are you looking for XDocument.Save?
doc.Save(#"C:\\Users\IT-Administrator\Desktop\21.xml");
That's already in your code (though its commented out?). Are you having issues saving?

How to take screenshot of webpage in C#

I am totally new to C# coding - I am trying to take screen shots of webpages whose URLs are initially picked up form a notepad uploaded on the same form.
As I read through the documentation on the web_browser control in MSDN.. I did arrive at the following code - yet when I run, I only get white screens
I tried uploading a .txt with (google/yahoo URLs as test data in 2 lines)
Can someone please say what more should I take care apart from handling which should get what I want as I read in MSDN.
P.S: pls forgive if I'm terribly wrong in coding style .. as aforesaid I'm just starting my C# coding :)
Code that I tried...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MSDN_wbc_tut1
{
public partial class Form1 : Form
{
public int temp = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void FileUploadButton1_Click(object sender, EventArgs e)
{
//once a file is uploaded i want Pgm to read contents & browse them as URLS
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.CheckFileExists = true;
openFileDialog.AddExtension = true;
openFileDialog.Multiselect = true;
//Filtering for Text files alone
openFileDialog.Filter = "text files (*.txt)|*.txt";
//if file is selected we must then do our coding part to proceed hecneforth
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//my code to say pgm wat to do once upload done...
//Getting my Chosen file's name
string Fileuploaded = openFileDialog.FileName;
//Read all line opens files - reads till EOF & closes ,prevents a while condition
string[] FileuploadedContent = System.IO.File.ReadAllLines(Fileuploaded);
foreach (string s in FileuploadedContent)
{
NavigateContent(s);
}
}
}
private void NavigateContent(string lineasurl)
{
// Add an event handler that images the document after it loads.
try
{
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler
(takescreen);
webBrowser1.Navigate(new Uri(lineasurl));
//webBrowser1.Navigate(lineasurl); also works
}
catch (System.UriFormatException)
{
return;
}
}
private void takescreen(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//specifying sample values for image or screenshot size
int x = 600, y = 700;
Bitmap bitmap = new Bitmap(x, y);
webBrowser1.DrawToBitmap(bitmap, new Rectangle(0, 0, x, y));
//to give each screen a unique name - i append some no onto it
temp = temp + 1;
string TempFname = "Screenshotref" + temp.ToString() + "." + "jpg";
bitmap.Save(TempFname);
}
}
}

Categories

Resources