After flipping a Bitmap I want to put it an ImageBox:
//set cam
Mat m = new Mat();
m = capture.QuerySmallFrame();
//Flip picture
Image img = m.ToImage<Bgr, byte>().Bitmap;
img.RotateFlip(RotateFlipType.Rotate180FlipY);
// show it
imageBox1.Image = img
When I run this, I get:
Can not implicitly convert type System.Drawing.Image to Emgu.CV.IImage.
The ImageBox.Image expects Emgu.CV.World.IImage, so I'd suggest doing the rotation using IImage's methods and avoiding to convert into System.Drawing.Bitmap altogether. The code below should do that:
//Flip picture
var img = m.ToImage<Bgr, byte>();
img = img.Rotate(180, new Bgr(Color.Red)).Flip(Emgu.CV.CvEnum.FlipType.Vertical);
// show it
imageBox1.Image = img;
Related
I'm currently working with an OCR program. I'm using tesseract and i need to deskew images to improve the quality of the detected characters. The problem is that the deskew property given by tesseract doesn't produce enough attractive results. So i tried to deskew the image with AForge and Atalasoft, but every time, no matter what, the image is not in the format they require. What am i doing wrong? Or there is a better solution?
This is AForge implementation
System.Drawing.Bitmap imageToBitmap = AForge.Imaging.Image.FromFile(imagePath);
Console.WriteLine("before " + imageToBitmap.PixelFormat);
System.Drawing.Bitmap NewPicture = imageToBitmap.Clone(new System.Drawing.Rectangle(0, 0, imageToBitmap.Width, imageToBitmap.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
var dop = AForge.Imaging.Image.Clone(imageToBitmap, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Console.WriteLine("after " + dop.PixelFormat);
AForge.Imaging.DocumentSkewChecker skewChecker = new AForge.Imaging.DocumentSkewChecker();
// get documents skew angle
double angle = skewChecker.GetSkewAngle(dop);
// create rotation filter
AForge.Imaging.Filters.RotateBilinear rotationFilter = new AForge.Imaging.Filters.RotateBilinear(-angle);
rotationFilter.FillColor = System.Drawing.Color.White;
// rotate image applying the filter
System.Drawing.Bitmap rotatedImage = rotationFilter.Apply(imageToBitmap);
rotatedImage.Save("deskewedImage");
This is Atalasoft implementation
AtalaImage img = new AtalaImage(imagePath);
AutoDeskewCommand cmd = new AutoDeskewCommand();
AtalaImage resultImage = cmd.Apply(img).Image;
resultImage.Save("result.tif", new TiffEncoder(), null);
I finally managed to understand why it wasn't working: the image should be converted to Format8bppIndexed or the method skewChecker.GetSkewAngle(image) will throw an exception
Bitmap tempImage = AForge.Imaging.Image.FromFile(imagePath);
Bitmap image;
if (tempImage.PixelFormat.ToString().Equals("Format8bppIndexed"))
{
image = tempImage;
}
else
{
image = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(tempImage);
}
tempImage.Dispose();
AForge.Imaging.DocumentSkewChecker skewChecker = new AForge.Imaging.DocumentSkewChecker();
// get documents skew angle
double angle = skewChecker.GetSkewAngle(image);
// create rotation filter
AForge.Imaging.Filters.RotateBilinear rotationFilter = new AForge.Imaging.Filters.RotateBilinear(-angle);
rotationFilter.FillColor = Color.Black;
// rotate image applying the filter
Bitmap rotatedImage = rotationFilter.Apply(image);
var deskewedImagePath = folderSavePath + filename + "_deskewed.tiff";
rotatedImage.Save(deskewedImagePath, System.Drawing.Imaging.ImageFormat.Tiff);
image.Dispose();
rotatedImage.Dispose();
I'm trying use Aforge library for filling holes in the resulting binary image, the problem is I'm getting a BLACK image as a result instead of the filled image.
Your assistance is highly appreciated.
Bitmap bitImage = (Bitmap)pic.Image;
Bitmap bm = Convert(bitImage);
FillHoles filter = new FillHoles();
filter.CoupledSizeFiltering = true;
// apply the filter
pictureBox1.Image = filter.Apply(bm);
public Bitmap Convert(Bitmap bit)
{
Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
// apply the filter
Bitmap bm = filter.Apply(bit);
return bm;
}
I have a writeablebitmap object that I would like to save to JPG in silverlight , how can I do that?
I am also converting writeablebitmap object into image as
WriteableBitmap bitmap = new WriteableBitmap(Width,Height);
//Some operation of drawing on bitmap
and then
Image imageFHR = new Image();
imageFHR.Source = bitmap;
imageFHR.Height = Height;
imageFHR.Width = Width;
myCanvas.Children.Add(imageFHR);
[HttpPost]
public ActionResult AddImage(Image model)
{
if (model.ImageData != null && model.ImageData.ContentLength > 0)
{
var fileName = Path.GetFileName(model.ImageData.FileName);
var pathBig = Path.Combine(Server.MapPath("~/UploadedImages"), fileName);
var pathSmall = Path.Combine(Server.MapPath("~/UploadedImages"), "small_" + fileName);
// --> How to change image size to big(800 x 600)
// and small (100x80) and save them?
model.ImageData.SaveAs(pathBig);
model.ImageData.SaveAs(pathSmall);
}
}
How do I change the image size to big(800 x 600) to small (100x80) and save them?
You could try this library:
http://nuget.org/packages/ImageResizer
It does support asp.net-mvc:
http://imageresizing.net/
Or you could get a pure C# lib and use it on your app. See these posts:
Resize an Image C#
https://stackoverflow.com/a/2861813/368070
And this snippet I found : http://snippets.dzone.com/posts/show/4336
The simplest way of doing it from the framework methods itself will be to use the DrawImage() method of the Graphics Class.
The example code could be like:
//For first scale
Bitmap bmp = new Bitmap(800, 600);
Graphics gf = Graphics.FromImage(bmp);
Image userpic = Image.FromStream(/*pass here the image byte stream*/)
gf.DrawImage(userpic, new Rectangle(0,0,800,600))
gf.Save(/* the save path */);
//For second scale
Bitmap bmp = new Bitmap(100, 80);
Graphics gf = Graphics.FromImage(bmp);
Image userpic = Image.FromStream(/*pass here the image byte stream*/)
gf.DrawImage(userpic, new Rectangle(0,0,100,80))
gf.Save(/* the save path */);
I am attempting to print a JPEG file that I reference using a Uri object and am having some difficulties. I found that while the image was printing, it was cropped slightly and was flipped and mirrored. I'm guessing that the crop was caused by a size not being set properly but have no idea why it's being flipped and rotated. Assuming that this was a natural oddity, I attempted to resolve the issue by applying a transform to the drawingContext object but this results a blank page being printed. Here is my code:
public void Print(List<Uri> ListToBePrinted)
{
XpsDocumentWriter writer =
PrintQueue.CreateXpsDocumentWriter(this.SelectedPrinter.PrintQueue);
PrintCapabilities printerCapabilities =
this.SelectedPrinter.PrintQueue.GetPrintCapabilities();
Size PageSize =
new Size(printerCapabilities.PageImageableArea.ExtentWidth,
printerCapabilities.PageImageableArea.ExtentHeight);
foreach (Uri aUri in ListToBePrinted)
{
BitmapImage anImage = new BitmapImage(aUri);
//create new visual which would be initialized by image
DrawingVisual drawingVisual = new DrawingVisual();
//create a drawing context so that image can be rendered to print
DrawingContext drawingContext = drawingVisual.RenderOpen();
// Flips along X and Y axis (flips and mirrors)
drawingContext.PushTransform(new ScaleTransform(-1, -1));
drawingContext.DrawImage(anImage, new Rect(PageSize));
drawingContext.Close();
writer.Write(drawingVisual);
}
}
Any help would be greatly appreciated - thank you!
Here's what I ended up with:
public void Print(List<Uri> ListToBePrinted)
{
XpsDocumentWriter writer =
PrintQueue.CreateXpsDocumentWriter(this.SelectedPrinter.PrintQueue);
PrintCapabilities printerCapabilities =
this.SelectedPrinter.PrintQueue.GetPrintCapabilities();
Size PrintableImageSize =
new Size(printerCapabilities.PageImageableArea.ExtentWidth,
printerCapabilities.PageImageableArea.ExtentHeight);
foreach (Uri aUri in ListToBePrinted)
{
DrawingVisual drawVisual = new DrawingVisual();
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage(aUri);
imageBrush.Stretch = Stretch.Fill;
imageBrush.TileMode = TileMode.None;
imageBrush.AlignmentX = AlignmentX.Center;
imageBrush.AlignmentY = AlignmentY.Center;
using (DrawingContext drawingContext = drawVisual.RenderOpen())
{
// Flips along X and Y axis (flips and mirrors)
drawingContext.PushTransform(new ScaleTransform(-1, 1, PrintableImageSize.Width / 2, PrintableImageSize.Height / 2));
drawingContext.PushTransform(new RotateTransform(180, PrintableImageSize.Width / 2, PrintableImageSize.Height / 2)); // Rotates 180 degree
drawingContext.DrawRectangle(imageBrush, null, new Rect(25, -25, PrintableImageSize.Width, PrintableImageSize.Height));
}
writer.Write(drawVisual);
}
}
The image is a little fuzzy but is certainly acceptable. I'm still not sure why my image needed to be flipped or mirrored.
Could you do something like:
BitmapImage anImage = new BitmapImage(aUri);
Image image = new Image();
image.BeginInit();
image.Source = anImage;
image.EndInit();
image.Measure(PageSize);
image.InvalidateVisual();
Then just print the Image object since it derives from Visual...
You need to call InvalidateVisual so that OnRender will be called, if you didn't it would result in a blank image...