Reputation: 132
I am wanted to detect orientation of image like Id card or Pan card using emgucv, so that I can correct the image and passed the same to tesseract OCR. I have searched google not able to find example in C#.
Can anyone guide me or suggest some example here? Any help will be highly appreciated.
imgInput = new Image<Bgr, byte>(impath);
Image<Gray, Byte> imggray = imgInput.Convert<Gray, Byte>();
VectorOfVectorOfPoint contour = new VectorOfVectorOfPoint();
Emgu.CV.Mat hiers = new Emgu.CV.Mat();
CvInvoke.FindContours(imggray, contour, hiers, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
Bitmap imgsss;
if (contour.Size >= 1)
{
for (int i = 0; i <= 1; i++)
{
Rectangle rect = CvInvoke.BoundingRectangle(contour[i]);
Emgu.CV.Structure.RotatedRect box = CvInvoke.MinAreaRect(contour[i]);
imggray.ROI = rect;
imggray = imggray.Rotate(box.Angle, new Gray(255));
imggray.Save(impath);
imgsss = imggray.Copy().Bitmap;
break;
}
}
Upvotes: 0
Views: 1475
Reputation: 1170
Unfortunately, I've always struggled finding documentation for Emgucv. Often times I would be looking at some obscure blog post from 2013 and have to modify the code to work correctly with the latest release of Emgucv. To compensate for this, I often will look at the c++ code of opencv, and just modify parts of it to work with c#. I find this to work relatively well, because the syntax from opencv c++ to emgucv c# is relatively the same, vs opencv python to emgucv c#. To answer your question about detecting an id card and correcting the orientation, check out this link here.
https://www.learnopencv.com/image-alignment-feature-based-using-opencv-c-python/
The basic idea is you create a feature detector to detect relevant pieces of the image, then use a matcher and warp perspective to fix the orientation. Like stated earlier, the code is in c++, but I should be pretty easy to convert, with most functions being contained within the namespace CvInvoke
.
Upvotes: 1