I'm programming in WPF(C#) using emgucv-windesktop 3.1.0.2282. I'm new in image processing and I want to use FFT and DFT in my image processing application. here is my code:
Image<Gray, System.Single> _image = new Image<Gray, System.Single>(
Util.ImageSourceToBitmap(img1.Source));
UMat DFTimage = new UMat();
UMat Original = new UMat();
CvInvoke.Dft(_image.ToUMat(), DFTimage, Emgu.CV.CvEnum.DxtType.Forward , -1);
CvInvoke.Dft(DFTimage, Original, Emgu.CV.CvEnum.DxtType.Inverse, -1);
img2.Source = Util.BitmapToImageSource(Original.Bitmap);
img3.Source = Util.BitmapToImageSource(DFTimage.Bitmap);
I used a human face, flower, etc but what I see in img2 and img3 (they are Image controls) is black and empty images. What is wrong in my code?
UPDATE 1:
Now I'm using this code:
Bitmap bm = Util.ImageSourceToBitmap(img1.Source);
Image<Gray, Single> image = new Image<Gray, Single>(bm);
UMat DFTimage = new UMat(image.Size, Emgu.CV.CvEnum.DepthType.Cv32F, 2);
UMat Original = new UMat(image.Size, Emgu.CV.CvEnum.DepthType.Cv32F, 2);
CvInvoke.Dft(image, DFTimage, Emgu.CV.CvEnum.DxtType.Forward, image.Rows);
CvInvoke.Dft(Original, DFTimage, Emgu.CV.CvEnum.DxtType.Inverse, image.Rows);
Util.BitmapToImageSource(Original.Bitmap);
Now I get System.Exception exception in showing my inverse image:
An unhandled exception of type 'System.Exception' occurred in
Emgu.CV.World.dll
Additional information: Unknown color type
Try changing this:
CvInvoke.Dft(Original, DFTimage, Emgu.CV.CvEnum.DxtType.Inverse, image.Rows);
to this:
CvInvoke.Dft(DFTimage, Original, Emgu.CV.CvEnum.DxtType.Inverse, image.Rows);
Related
I got a kinect mount in the ceiling pointing down to a table (that is roughly 2 to 3 meters of the kinect) and my objective is using the kinect depth stream, locate the objects and get their position to late send to unity. So for that i am using c sharp and opencv (emgu wrapper), i first use canny to get the edges and then use BoundingRect to create a box around the object.
The result is the as follows:
Original:
As you can see the stationary objects are no problem but the box around the hands (the first was a flyer on it) is to big and sometimes is even worse. Is there any other way (using opencv ) of getting the positions of the objects?
The objective is then to send the position and dimension (of the object) to unity (trough tcp/ip), so preferably the shapes have to squares or rectangles to easy manipulation on unity.
The code I have so far is:
Image<Bgr, Byte> grayImage = new Image<Bgr, Byte>("C:\\Users\\Pedro\\Desktop\\imgRva\\KinectSnapshot-03-48-01.png");
Image<Gray, Byte> gray = grayImage.Convert<Gray, Byte>().PyrDown().PyrUp();
CvInvoke.cvShowImage("texto", gray);
CvInvoke.cvWaitKey(0);
Image<Gray, Byte> bin = gray.ThresholdBinary(new Gray(40), new Gray(255));
Image<Gray, Byte> cannyEdges = bin.Canny(300, 300);
CvInvoke.cvShowImage("texto", cannyEdges);
CvInvoke.cvWaitKey(0);
Image<Gray, Byte> canny = new Image<Gray, byte>(cannyEdges.Size);
using (MemStorage storage = new MemStorage())
for (Contour<Point> contours = cannyEdges.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, storage); contours != null; contours = contours.HNext)
{
CvInvoke.cvDrawContours(canny, contours, new MCvScalar(255), new MCvScalar(255), -1, 1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, new Point(0, 0));
}
using (MemStorage store = new MemStorage())
for (Contour<Point> contours1 = cannyEdges.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, store); contours1 != null; contours1 = contours1.HNext)
{
Rectangle r = CvInvoke.cvBoundingRect(contours1, 1);
canny.Draw(r, new Gray(255), 1);
Debug.WriteLine(r.Location + " x " + r.Width + " y " + r.Height);
}
CvInvoke.cvShowImage("texto", canny);
CvInvoke.cvWaitKey(0);
Ty
Currently, I am facing problem with my EmguCV c# code. I am trying to recognize my images from the database, but its not working. Once my face is detected, its crashing and then this error shows up
Additional information: OpenCV: Different sizes of objects.
I tried searching for this error but I am clueless.
This is my code:
//Action for each element detected
foreach (MCvAvgComp f in facesDetected[0])
{
t = t + 1;
result = currentFrame.Copy(f.rect).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
//draw the face detected in the 0th (gray) channel with blue color
currentFrame.Draw(f.rect, new Bgr(Color.Green), 2);
//Database select the image row and pass to the eigenobjectrecognizer
//ConnectToDatabase();
if (Connection.State.Equals(ConnectionState.Open))
{
Connection.Close();
TSTable.Clear();
ConnectToDatabase();
}
//Connection.Open();
OleDbCommand OledbSelect = new OleDbCommand("Select FaceName, FaceImage From TrainingSet1",Connection);
OleDbDataReader reader = OledbSelect.ExecuteReader();
while (reader.Read())
{
labels.Add(reader.GetValue(1).ToString());
trainingImages.Add(gray);
}
if (TSTable.Rows.Count != 0)
{
//TermCriteria for face recognition with numbers of trained images like maxIteration
MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);
//Eigen face recognizer
EigenObjectRecognizer recognizer = new EigenObjectRecognizer(
trainingImages.ToArray(), //database faceimage list
labels.ToArray(), //facename list
3000,
ref termCrit);
name = recognizer.Recognize(result);
//Draw the label for each face detected and recognized
currentFrame.Draw(name, ref font, new Point(f.rect.X - 2, f.rect.Y - 2), new Bgr(Color.LightGreen));
}
I currently still new to EmguCV and C#. So some of the exceptions I don't understand. Can anyone help me with this.
And once the code breaks, it goes to the EigenObjectRecognizer.cs. This is the code where it breaks:
public static float[] EigenDecomposite(Image<Gray, Byte> src, Image<Gray, Single>[] eigenImages, Image<Gray, Single> avg)
{
return CvInvoke.cvEigenDecomposite(
src.Ptr,
Array.ConvertAll<Image<Gray, Single>, IntPtr>(eigenImages, delegate(Image<Gray, Single> img) { return img.Ptr; }),
avg.Ptr);
}
I guess, problem is in trainingImages. The width and height of each eigenImages must be the same as the width and height of the input image.
Your result image has size 100x100. So, all your trainingImage should have same size. Resize them, before adding to your list.
I review your code. Trouble still in sizes.
Line 305 replace to :
result = currentFrame.Copy(f.rect).Convert<Gray, byte>().Resize(gray.Width, gray.Height, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
I am trying to view in a pictureBox a tif raw file uncompressed. that was generated with
"matrox imaging library"
I am using emgu.cv
I am doing:
Image<Gray, ushort> My_Image = new Image<Gray, ushort>(imgN);
Image<Gray, byte> My_Image1 = new Image<Gray, byte>(imgN);
and slso:
Image<Gray, byte> My_Image3 = My_Image.Convert<Gray, byte>();
I keep seeing black image,
any solutions?
I have also tried more combination resulting in black ...
This worked for me:
IntPtr img = CvInvoke.cvLoadImage(imgN,Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_UNCHANGED);
MIplImage iplImage = (MIplImage)Marshal.PtrToStructure(img, typeof(MIplImage));
Image<Gray, UInt16> res;
res = new Image<Gray, UInt16>(1024, 872, iplImage.widthStep, iplImage.imageData);
imageBox1.Image = res;
using imageBox1
i have a blood image and i applied watershed on it .. its works and determines the cells but i don't know how to put each cell in a separate image .. i'm working with emgu.cv can i get some help
here i segment the picture using my watershed method and then put the marker on the original image
Image<Gray, Int32> boundaryImage = watershedSegmenter.Process(image);
Image<Gray, Byte> test = watershedSegmenter.GetWatersheds(); Image<Bgr, byte>dest=new Image<Bgr, byte>(image.Width, image.Height);
dest = image.And(image, test);
pictureBox1.Width = boundaryImage.ToBitmap().Width;
pictureBox1.Height = boundaryImage.ToBitmap().Height;
pictureBox1.BackgroundImage = boundaryImage.ToBitmap();
It seems like EMGUcv cvWatershed() is having some bugs which is not yet resolved. The Marker Image is always returning white image. Can you share your 1st stage output images?
I'm not able to view any images by using this code.
Image<Bgr, Byte> image = Img_Source_Bgr.Copy();
Image<Gray, Int32> marker = new Image<Gray, Int32>(image.Width, image.Height);
Rectangle rect = image.ROI;
marker.Draw(
new CircleF(
new PointF(rect.Left + rect.Width / 2.0f, rect.Top + rect.Height / 2.0f),
/*(float)(Math.Min(image.Width, image.Height) / 20.0f)*/ 5.0f),
new Gray(255),
0);
Image<Bgr, Byte> result = image.ConcateHorizontal(marker.Convert<Bgr, byte>());
Image<Gray, Byte> mask = new Image<Gray, byte>(image.Size);
CvInvoke.cvWatershed(image, marker);
CvInvoke.cvCmpS(marker, 0.10, mask, CMP_TYPE.CV_CMP_GT);
imageBox1.Image = mask;
I'm doing a small experiment using fourier transform on emgu cv. My aim is to have the fourier transform of an image, then take the inverse fourier transform again, and check if the image shows up or not. mathematically, it should.
this is my code which i believe is correct
Image<Gray, float> image = new Image<Gray, float>("c://box1.png");
IntPtr complexImage = CvInvoke.cvCreateImage(image.Size, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_32F, 2);
CvInvoke.cvSetZero(complexImage); // Initialize all elements to Zero
CvInvoke.cvSetImageCOI(complexImage, 1);
CvInvoke.cvCopy(image, complexImage, IntPtr.Zero);
CvInvoke.cvSetImageCOI(complexImage, 0);
Matrix<float> dft = new Matrix<float>(image.Rows, image.Cols, 2);
CvInvoke.cvDFT(complexImage, dft, Emgu.CV.CvEnum.CV_DXT.CV_DXT_FORWARD, 0);
Matrix<float> idft = new Matrix<float>(dft.Rows, dft.Cols, 2);
CvInvoke.cvDFT(dft, idft, Emgu.CV.CvEnum.CV_DXT.CV_DXT_INVERSE, 0);
IntPtr complexImage2 = CvInvoke.cvCreateImage(idft.Size, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 2);
CvInvoke.cvShowImage("picture", idft);
System.Threading.Thread.Sleep(99999); // to wait and see the picture
I have two problems:
1- error : an error shows up saying " OpenCV: Source image must have 1, 3 or 4 channels " i believe its related about the IDFT, but I couldn't solve it
2- it still shows an output image, but unfortunately, its not the original image that was input. all what show is a plain grey image.
Thanks.
Try this,
Image<Gray, System.Single> _image = new Image<Gray, System.Single>("c://box1.png");
Image<Gray, System.Single> DFTimage = new Image<Gray, System.Single>(_image.Size);
Image<Gray, System.Single> Original = new Image<Gray, System.Single>(_image.Size);
CvInvoke.cvDFT(_image.Ptr, DFTimage.Ptr,Emgu.CV.CvEnum.CV_DXT.CV_DXT_FORWARD, -1);
CvInvoke.cvDFT(DFTimage.Ptr, Original.Ptr,Emgu.CV.CvEnum.CV_DXT.CV_DXT_INVERSE, -1);
CvInvoke.cvShowImage("picture", Original);