direct and inverse fourier transform in EMGU CV - c#

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);

Related

What is the minimal code to find the Fourier transform of an image in EmguCV.3.1.0.1?

I want to find the FT of an image, and show its magnitude, and, phase spectrum in EmguCV.3.1.0.1.
I was trying something like the following:
public partial class Form1 : Form
{
const string path = #"lenagr.png";
Image<Bgr, float> image;
public Form1()
{
InitializeComponent();
image = new Image<Bgr, float>(path);
pictureBox1.Image = image.Bitmap;
}
private void button1_Click(object sender, EventArgs e)
{
Mat dft = new Mat(image.Size, DepthType.Default, 2);
CvInvoke.Dft(image, dft, DxtType.Forward, image.Rows);
Mat[] images = dft.Split();
//Show The Data
//Image<Bgr, float> im2 = dft.ToImage<Bgr, float>();
pictureBox2.Image = images[0].Bitmap;
}
}
This source code freezes the application.
So, What is the absolutely minimal code to find the Fourier transform of an image in EmguCV.3.1.0.1?
Note: The following source code doesn't even compile in EmguCV.3.1.0.1. And, it looks a job lot.
Image<Gray, float> image = new Image<Gray, float>(open.FileName);
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);
//The Real part of the Fourier Transform
Matrix<float> outReal = new Matrix<float>(image.Size);
//The imaginary part of the Fourier Transform
Matrix<float> outIm = new Matrix<float>(image.Size);
CvInvoke.cvSplit(dft, outReal, outIm, IntPtr.Zero, IntPtr.Zero);
//Show The Data
CvInvoke.cvShowImage("Real", outReal);
CvInvoke.cvShowImage("Imaginary ", outIm);
It is not clear what statement is doing what. Also, it mixes up IntPtr, Image<T1, T2>, Matrix<T>, and doesn't make use of Mat.

Fourier Transform in EmguCV 3.4.1

The following code doesn't compile in EmguCV 3.4.1.
Image<Gray, float> image = new Image<Gray, float>(path);
IntPtr complexImage = CvInvoke.cvCreateImage(image.Size,
Emgu.CV.CvEnum.IplDepth.IplDepth32F, 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.DxtType.Forward, 0);
//The Real part of the Fourier Transform
Matrix<float> outReal = new Matrix<float>(image.Size);
//The imaginary part of the Fourier Transform
Matrix<float> outIm = new Matrix<float>(image.Size);
CvInvoke.cvSplit(dft, outReal, outIm, IntPtr.Zero, IntPtr.Zero);
//Show The Data
CvInvoke.cvShowImage("Real", outReal);
CvInvoke.cvShowImage("Imaginary ", outIm);
Some functions
cvSetZero()
cvDFT()
cvShowImage()
are not available in this version of EmguCV.
How can I get around this issue?
Since version 3.0 EmguCV goes more for Mat instead of Image. Also somemethods were moved from CvInvoke to Mat or Image or were renamed (example: CvInvoke.cvShowImage --> CvInvoke.Imshow).
I also had this problem with dft and tried to port the code from the openCV example to c#. This is what I work with (using EmguCV 3.4.1):
var image = new Mat(path, ImreadModes.Grayscale);
int a = CvInvoke.GetOptimalDFTSize(image.Rows);
int b = CvInvoke.GetOptimalDFTSize(image.Cols);
var extended = new Mat();
CvInvoke.CopyMakeBorder(image, extended, 0, a - image.Rows, 0, b - image.Cols, BorderType.Constant, new MCvScalar(0));
extended.ConvertTo(extended, DepthType.Cv32F);
var vec = new VectorOfMat(extended, new Mat(extended.Size, DepthType.Cv32F, 1));
var complex = new Mat();
CvInvoke.Merge(vec, complex);
CvInvoke.Dft(complex, complex, DxtType.Forward, 0);
CvInvoke.Split(complex, vec);
vec[0].ConvertTo(vec[0], DepthType.Cv8U);
vec[1].ConvertTo(vec[1], DepthType.Cv8U);
CvInvoke.Imshow("Real", vec[0]);
CvInvoke.Imshow("Img", vec[1]);
Some methods have been renamed or moved. Use CvInvoke.Dft with Image.toUMat and UMat for the image manipulation. ImageViewer can be used for displaying images.
Image<Gray, float> image = new Image<Gray, float>(path);
UMat dftImage = new UMat(image.Size, Emgu.CV.CvEnum.DepthType.Cv32F, 2);
CvInvoke.Dft(image, dftImage, Emgu.CV.CvEnum.DxtType.Forward, image.Rows);

Get position of objects using kinect and C#

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

Detecting Text Regions from image using EmguCV

I am new to EmguCV and OpenCV. I want to detect the text Regions from an Image using EmguCV.
There are already some solutions posted on Stack using OpenCV.
Extracting text OpenCV
But unable to convert that OpenCV code to EmguCV.
Here is a direct conversion of the accepted answer in the link you provided into c# with EMGU. You might have to make some alterations since its a slightly different implementation but it should get you started. I also doubt it is a very robust so depending on your specific use it might not be suitable. Best of luck.
public List<Rectangle> detectLetters(Image<Bgr, Byte> img)
{
List<Rectangle> rects = new List<Rectangle>();
Image<Gray, Single> img_sobel;
Image<Gray, Byte> img_gray, img_threshold;
img_gray = img.Convert<Gray, Byte>();
img_sobel = img_gray.Sobel(1,0,3);
img_threshold = new Image<Gray, byte>(img_sobel.Size);
CvInvoke.cvThreshold(img_sobel.Convert<Gray, Byte>(), img_threshold, 0, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_OTSU);
StructuringElementEx element = new StructuringElementEx(3, 17, 1, 6, Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_RECT);
CvInvoke.cvMorphologyEx(img_threshold, img_threshold, IntPtr.Zero, element, Emgu.CV.CvEnum.CV_MORPH_OP.CV_MOP_CLOSE, 1);
for (Contour<System.Drawing.Point> contours = img_threshold.FindContours(); contours != null; contours = contours.HNext)
{
if (contours.Area > 100)
{
Contour<System.Drawing.Point> contours_poly = contours.ApproxPoly(3);
rects.Add(new Rectangle(contours_poly.BoundingRectangle.X, contours_poly.BoundingRectangle.Y, contours_poly.BoundingRectangle.Width, contours_poly.BoundingRectangle.Height));
}
}
return rects;
}
Usage:
Image<Bgr, Byte> img = new Image<Bgr, Byte>("VfDfJ.png");
List<Rectangle> rects = detectLetters(img);
for (int i=0;i<rects.Count();i++)
img.Draw(rects.ElementAt<Rectangle>(i),new Bgr(0,255,0),3);
CvInvoke.cvShowImage("Display", img.Ptr);
CvInvoke.cvWaitKey(0);
CvInvoke.cvDestroyWindow("Display");

How to copy segment from the image?

I have Emgu image :
Image<Bgr,byte> image = new Image<Bgr,byte>("image.jpg");
Here is how the file(image.jpg) looks like:
All pixels that inside red-yellow triangle I want to copy to the new image called:
Image<Bgr,byte> copiedSegment;
Any idea how to implement it if I have coordinates all coordinates of the triangle contour.
Thank you in advance.
In the opencv c++ api you can just use the matrix copy function with a mask composed of your triangular components.
Mat image = imread("image.jpg",CV_LOAD_IMAGE_COLOR);
vector<Point> triangleRoi;
Mat mask;
//draw your trianlge on the mask
cv::fillConvexPoly(mask, triangleRoi, 255);
Mat copiedSegment;
image.copyTo(copiedSegment,mask);
You should beable to write some similar code in emgu based on this.
// no we apply filter to get rid of Equalization Noise.
ImageBilateral = new Image<Gray, Byte>(grayImg.Size);
CvInvoke.BilateralFilter(grayImg, ImageBilateral, 0, 20.0, 2.0);
//ImageBilateral.Save(String.Format("C:\\Temp\\BilateralFilter{0}.jpg", counter));
retImage = AlignFace(ImageBilateral);
Point faceCenter = new Point(ImageBilateral.Width / 2, (int)Math.Round(ImageBilateral.Height * FACE_ELLIPSE_CY));
Size size = new Size((int)Math.Round(ImageBilateral.Width * FACE_ELLIPSE_W), (int)Math.Round(ImageBilateral.Width * FACE_ELLIPSE_H));
// Filter out the corners of the face, since we mainly just care about the middle parts.
// Draw a filled ellipse in the middle of the face-sized image.
Image<Gray, Byte> mask = new Image<Gray, Byte>(ImageBilateral.Size);
// draw Ellipse on Mask
CvInvoke.Ellipse(mask, faceCenter, size, 0, 0, 360, new MCvScalar(255, 255, 255), -1, Emgu.CV.CvEnum.LineType.AntiAlias, 0);
mask.Save(String.Format("C:\\Temp\\mask{0}.bmp", counter));
Image<Gray, Byte> temp1 = ImageBilateral.Copy(mask);
ImageBilateral.Save(String.Format("C:\\Temp\\ImageBilateral.CopyTo(mask){0}.bmp", counter));
temp1.Save(String.Format("C:\\Temp\\temp1{0}.bmp", counter));

Categories

Resources