How to achieve good sharpness with twain/emgu/open cv? - c#

I am using an Epson Perfection V700 scanner and selecting the following options when scanning using their tool:
ICM Color correction (source: EPSON-Standard and target: sRGB)
Unsharp Mask (medium)
That produces this image:
Now my problem is this - I actually need to interact with this scanner using TWAIN .Net and when I do so, the image I get back is this:
Aside: I unselected the aforementioned two options and scanned again with the Epson and got a very similar image to what I get through TWAIN.
So I figure that perhaps these are post processing steps that I can do myself on the image (maybe they are done in the hardware somehow though, I don't know).
I am using EmguCV so first of all I created an extension method that applies the ICM (I struggled to find any documentation for this, so it is a bit of a guess and maybe I am wrong straight away but I got the information from here: The bitmap transform class and it seems to make a difference to the image):
public static Image<Bgr, TDepth> ApplyIcm<TDepth>(
this Image<Bgr, TDepth> source,
string sourceIcm,
string targetIcm)
where TDepth : new()
{
var target = source.CopyBlank();
using (source)
{
using (var b = source.Bitmap)
{
using (var memory = new MemoryStream())
{
b.Save(memory, ImageFormat.Bmp);
memory.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
var ccb = new ColorConvertedBitmap();
ccb.BeginInit();
ccb.Source = bitmapImage;
ccb.SourceColorContext =
new ColorContext(new Uri(sourceIcm));
ccb.DestinationColorContext =
new ColorContext(new Uri(targetIcm));
ccb.EndInit();
var encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(ccb));
using (var ms = new MemoryStream())
{
encoder.Save(ms);
target.Bitmap = new Bitmap(ms);
}
}
}
}
return target;
}
Then I looked at that unsharpen thing and came across this question: How to sharpen an image in OpenCV? which says:
You use a gaussian smoothing filter and subtract the smoothed version from the original image
(I also checked this question to find out what the equivalent emgucv call is Why might EmguCV Gaussian blur not return identical results as OpenCV Gaussian blur?) and came up with this additional extension method:
public static Image<Bgr, TDepth> UnsharpMask<TDepth>(
this Image<Bgr, TDepth> source,
Size kernelSize,
int kernelHoritonalStandardDeviation,
int kernelVerticalStandardDeviation,
double alpha,
double beta,
double gamma)
where TDepth : new()
{
Image<Bgr, TDepth> ret = source.CopyBlank();
CvInvoke.cvSmooth(source,
ret,
SMOOTH_TYPE.CV_GAUSSIAN,
kernelSize.Width,
kernelSize.Height,
kernelHoritonalStandardDeviation,
kernelVerticalStandardDeviation);
CvInvoke.cvAddWeighted(source, alpha, ret, beta, gamma, ret);
return ret;
}
Now I call it like so:
string sourceIcm = #"C:\Windows\System32\spool\drivers\color\ewrgb18.icm";
string targetIcm = #"C:\Windows\System32\spool\drivers\color\ewsrgb.icm";
using(var im = new Image<Bgr, byte>("out.bmp"))
{
using (var icmmed = im.ApplyIcm(sourceIcm, targetIcm))
{
using (var ret = icmmed.UnsharpMask(new Size(0, 0), 5, 5, 2.4, -1.5, 0))
{
ret.Save("ret.bmp");
}
}
}
and this is the result:
Not very good! :-(
I have fiddled with the parameters endlessly but I just cannot work out how (or even if) I can achieve the same result as the Epson tool.
So, my question is:
Does anyone know if it is possible to achieve a result using opencv/emgucv (or even TWAIN - I had a look through the documentation for that and tried adjusting some of the capability parameters but I just made the image worse) that is similar in sharpness to the original image above or is there another technique I should try (could it be that I would need to know some details about the hardware itself in order to achieve correct sharpening)?

I think you should know how using WIA (Windows Image Acquisition) in your project, you may don't need to get access to hardware using opencv. WIA is used for integrating with webcams and scanners. Or, you can use TWAIN as you mentioned
have a look at these examples they could by helpful for your project:
using WIA
and
using TWAIN
Concerning the sharpening, you can use opencv functionality at software level, as another choice to solve your problem

Related

C# Emgu CV - how to use cuda in dnn

public static void DetectObjects(Bitmap bitmapImage)
{
Image<Bgr, byte> image;
image = bitmapImage.ToImage<Bgr, byte>();
Mat input = DnnInvoke.BlobFromImage(image, 1 / 255.0, swapRB: true);
_netModel.SetInput(input);
Mat output = _netModel.Forward();
}
private static Net _netModel = DnnInvoke.ReadNetFromDarknet("B:/yolov3.cfg.txt", "B:/yolov3.weights");
The project has EmguCV version 4.4.0.4099 and the CUDA module of the same version.
As a result, I need the function to perform calculations on the GPU.
How many did not search about this, I did not find suitable information.
It seems enough to use
_netModel.SetPreferableBackend(Emgu.CV.Dnn.Backend.Cuda);
Source

Create a Mat from a PvBuffer in C# using eBUS SDK and EmguCV

I'm using EmguCV 3.4.1 and the eBUS SDK. I have video coming in over GigE and I would like to convert the PvBuffer to a MAT so I can use OpenCV to create a display a histogram.
I followed a similar style to how you create a Mat in C++. Unfortunately this solution requires marking the project as unsafe.
unsafe private Mat convertPvBufferToMat(PvBuffer aBuffer)
{
PvImage lImage = aBuffer.Image;
lImage.Alloc(lImage.Width, lImage.Height, PvPixelType.Mono16);
int[] sizes = new int[2] { (int)lImage.Height, (int)lImage.Width };
Mat aMat = new Mat(sizes, DepthType.Cv16U, (IntPtr)lImage.DataPointer);
return aMat;
}
I'm taking images from an infared camera outputing 16 bit grayscale images.

Are there any good examples of how to take a screenshot in selenium webdriver C#, then crop and save the image?

I've been searching for some good examples of how to take a screenshot using ITakesScreenshot in Selenium Webdriver in C#, then to crop the image using the elements dimensions and then saving this new image. With no such luck have I found any in C#.
I have this method at the moment but every so often I get a Out of Memory exception when used in a test. It fails on the line where it tries to crop.
public void TakeScreenShotOfElement(IWebDriver _driver, string rootpath, string imgName, string element2)
{
string element3 = element2;
var element = driver.FindElement(By.XPath(element3));
Byte[] ba = ((ITakesScreenshot)driver).GetScreenshot().AsByteArray;
var ss = new Bitmap(new MemoryStream(ba));
var crop = new Rectangle(element.Location.X, element.Location.Y, element.Size.Width, element.Size.Height);
//create a new image by cropping the original screenshot
Bitmap image2 = ss.Clone(crop, ss.PixelFormat);
if (!Directory.Exists(rootpath))
{
Directory.CreateDirectory(rootpath);
}
image2.Save(String.Format("{0}\\{1}.png", rootpath, imgName), System.Drawing.Imaging.ImageFormat.Png);
}
Any help is much appreciated, thanks in advance!!!
Some of the objects you're creating are IDisposable. You need to make sure Dispose() gets called on them. As it stands they're not releasing the memory they've claimed, which is why you get the exceptions.
The easiest way to make sure these items get disposed is to wrap each of them in a using block.

How to use the Nokia Imaging SDK's BlendFilter on WP8?

I'm staring with the Nokia Imaging SDK to play a little with it. Now, I'm facing the problem where I have an Image which already exists (in a folder in my visual studio solution) and I want to convert this image in order to use it in the BlendFilter class of the Nokia Imaging SDK. However I don't know how to use it.
I was trying to convert the existing image in a stream and then pass it as a parameter to the BlendFilter constructor. But not luck. The compiler says that the best overload method match ... has some invalid arguments.
This is the way I'm trying to load the existing image to a stream:
Image image = new Image();
image.Source = new BitmapImage(new Uri("/Images/Template3.2.png", UriKind.Relative));
BitmapImage bitImage = new BitmapImage(new Uri("/Images/Template3.2.png", UriKind.Relative));
WriteableBitmap Bitmap = new WriteableBitmap(bitImage);
And then:
var BlendFilter = new BlendFilter(bitImage, BlendFunction.Add); --> the compiler error is here
Does anyone know how to use the BlendFilter class? any example would be very helpful.
Regards!
Blend filter takes an IImageProvider as input. That means you can use any of the X-ImageSource classes as input and it will do all the work internally.
If you have a stream of the image I suggest you create an StreamImageSource and pass that to BlendFilter.
The list of different image sources is quite long, I suggest you look into the documentation and chose the one that is most appropriate to you.
Here is an example that takes a stream of an image as input, and blends a new image on top of it. For simplicity the other image is just an image filled with one color (ColorImageSource), but you can set any IImageProvider as source: chose the most convenient one.
using (var backgroundSource = new StreamImageSource(stream))
using (var filterEffect = new FilterEffect(backgroundSource))
{
using (BlendFilter blendFilter = new BlendFilter())
{
var size = new Windows.Foundation.Size(400, 400);
var color = Windows.UI.Color.FromArgb(250, 128, 255, 200);
blendFilter.ForegroundSource = new ColorImageSource(size, color);
blendFilter.BlendFunction = BlendFunction.Add;
filterEffect.Filters = new[] { blendFilter };
var result = await new JpegRenderer(filterEffect).RenderAsync();
}
}

C# MemoryStream slowing programme performance

I'm working on a project using WPF to display the Kinect ColorImageFrame and a skeleton representation. I also have to record those two videos.
I'm able to display and record (using EmguCV) those two images, but I have some performance issues. It seems that this part of my code is the reason of my loss of performance.
private void DrawSkeleton(Skeleton[] skeletons)
{
using (System.Drawing.Bitmap skelBitmap = new System.Drawing.Bitmap(640, 480))
{
foreach (Skeleton S in skeletons)
{
if (S.TrackingState == SkeletonTrackingState.Tracked)
{
DrawBonesAndJoints(S,skelBitmap);
}
else if (S.TrackingState == SkeletonTrackingState.PositionOnly)
{
}
}
_videoArraySkel.Add(ToOpenCVImage<Bgr, Byte>(skelBitmap));
BitmapSource source = ToWpfBitmap(skelBitmap);
this.skeletonStream.Source = source;
}
}
and more precisely from the ToWpfBitmap which allows me to display it in my Window:
public static BitmapSource ToWpfBitmap(System.Drawing.Bitmap bitmap)
{
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
stream.Position = 0;
BitmapImage result = new BitmapImage();
result.BeginInit();
// According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
// Force the bitmap to load right now so we can dispose the stream.
result.CacheOption = BitmapCacheOption.OnLoad;
result.StreamSource = stream;
result.EndInit();
result.Freeze();
return result;
}
}
The loss of performance is characterized by:
- The videos displayed on the Window are not fluent anymore
- The video recording seems to miss some frames which leads to a video going faster/lower than the normal.
Can you help me by telling me where this problem may come from?
Try to use RecyclableMemoryStream instead of MemoryStream. It was designed for solving some issue with memory.
Check out this article for details - Announcing Microsoft.IO.RecycableMemoryStream
Have you tried doing the memory write i/o in a separate thread, while maintaining the data in a buffer like a queue?

Categories

Resources