nQuant image quality of gray gradients - c#

So I am trying nQuant for png compression but having terrible results:
Using the canonical QuantizeImage call
var quantizer = new WuQuantizer();
Bitmap imageToSave = new Bitmap(image);
using (var quantized = quantizer.QuantizeImage(imageToSave))
{
quantized.Save(Path.Combine(imagesPath, imageName + "." + format), format);
}
Processing this
I obtained this
Any Idea how to prevent the quality from degrading so much?

Xialoin Wu's fast optimal color quantizer, which is one of the most effective color quantization methods, provides excellent results. However, low frequency colors in the original image tend to be excluded during the histogram counting process. In particular, the loss of original color increases when it uses a small number of boxes to quantize the image with a small number of colors (i.e. photo containing small red lips). Thus, to complement these disadvantages, a better color quantization algorithm that is effective even when it uses only a small number of colors by using the fast pairwise nearest neighbor based algorithm.
Given the limited number of colors, a severe type of artifact arises in the quantized image in areas of smooth color gradients, in the form of false edges, are clearly visible. To reduce such artifacts, a subsequent dithering step is typically employed after quantization. Dithering distributes quantization errors into neighboring pixels, helping to hide the false edges.
GDI+ supports different compression algorithms via the Encoder.Compression. But that's not "Quality". Each algorithm will compress the image to a different size; where the compression with the least number of bytes may be considered "best", in terms of quality of compression. But, that's not what Encoder.Quality means. Encoder.Quality deals with the degree of loss with lossy compression; something that doesn't apply to PNG. PNG is not a lossy format; Therefore, Quality doesn't apply. Possibly using 3rd party applications to write PNG files.
Please find the following c# open source to achieve better quality without support for the compression of PNG file using GDI+ as mentioned above.
https://github.com/mcychan/nQuant.cs

Related

Fast way to convert large vectorial images to binary matrix

I have complex vectorial images I need to convert to binary matrix (a kind of rasterizing) with high precision, using necessarily c#. Currently I'm doing it by painting vectorial images in a bitmap and reading the internal bitmap array. Then I convert this array in a binary matrix where 1 indicates that the corresponding pixel is of a specific color, and 0 when is not. As I access the internal bitmap array directly, that's fast and allows using Parallel.For
The problem is that I need to obtain very high definition matrixes of the images (50000x25000 or more). Therefore I have to paint each vectorial image dividing it in several parts, as a bitmap of this size is not supported.
As I have a lot of images to convert, it is very slow.
I'm looking for a fast way of doing that without loosing precision (ideally even increasing it). And I need to integrate this feature in a c# application.
I finally used Gdal rasterize tool, passing polygons as shapefiles, and it works quite well and fast.

Image brightness for big images c#

I use this link to add my program the capability to adjust the brightness of the image. This code is ok but it takes time to adjust the brightness(Image file size 1.8mb). When I try the lower quality image it instantly adjusts the image(Image file size 100KB). Is there any efficient way to adjust the brightness of the image.
The code seems to use GetPixel and SetPixel on regular Bitmaps. This is a bad idea because it is so slow.
To manipulate a single pixel of a Bitmap it must be locked (which Get/SetPixel do behind the scenes) and doing it on a pixel by pixel basis means that for a 1000x1000 sized image a million locking/unlocking operations must be performed. This creates an enormous overhead.
Method one
One way to avoid this is to lock the whole bitmap with the LockBits function. Now we can loop over the pixels and modify them.
Two notes about this method:
What we now access are the raw bytes of each pixel, that is each channel separately: either BGR or BGRA, depending on the pixel format. This means that the channels are physically reversed from the usual RGB/ARGB format of the Color methods.
To loop over the physical bitmap pixel rows we also need to add some stride to each row, which pads the rows to a multiple of 4 bytes. Also see here
For some examples you may want to browse over some of these posts. Note especially this one which uses a delegate to allow for flexible operations!
(Note that several of the posts use 2 or even 3 locked bitmaps because they aim at combining images..)
Method two
Another way to get around the overhead of locking pixels one by one are ready-made bitmap classes that help by locking themselves as a whole. Here and here are examples I didn't try myself.
Method three
Finally there is a very elegant method for image manipulation, which is both rather simple and really fast; also professionally crafted for best results: You can set up a ColorMatrix.
It will let you change brightness, gamma, hues and then some. Here is a very nice introduction.
The only drawback is, that is limited to some fixed operations, so you can't create custom filters for other fancy stuff, like photoshop-type layer modes or others, especially those that need to process neighbouring pixels e.g. for blurring..
But if all you want is changing brightness, this is what I would recommend!

Convert Image 16bits grayscale to Bitmap 16bits grayscale (Constructor error)

I try to convert a Image (16bits grayscale) to Bitmap (16bits grayscale).
This example doesn't works (Exception : ArgumentException)
private Mat ToMat16bGrayscale(Image im)
{
Bitmap bmp2 = new Bitmap(im);
}
But this example works :
private Mat ToMat16bGrayscale(Image im)
{
//Cast obligatoire la création ou la copie d'une bitmap ne fonctionne pas ici...
Bitmap bmp = (Bitmap)(im);
Bitmap new_bmp = (Bitmap)bmp.Clone();
}
I want to know why the constructor Bitmap doesn't works with a Image 16bits grayscale and why the cast Image to Bitmap works.
Thanks :)
PixelFormat.Format16bppGrayscale is a white elephant image format. Commodity hardware, the kind you'd find back in your machine, isn't even remotely close to being able to accurately render such a bitmap. Which is RGB-based with 8 bits for each color channel, producing 16 million distinct colors. Only 256 of them are shades of gray. Even worse are commodity LCD monitors, they generally can't do better than make 6 bits of color info distinctive, in effect rendering no more than 64 shades of gray. Mapping 65,536 shades of gray down to 256 or 64 of course causes lots of details to be lost.
It does get used in very specialized applications. One is radiological imaging, displaying X-ray images for example. But they also have extraordinary expensive monitors to display such an image. The non-trivial electrical component is the D/A converter, a 16-bit converter that can run at ~250 MHz costs lots and lots of money. They also use specialized image file formats, DICOM is common, formats that are not otherwise supported by the codecs included with GDI+.
There is significant liability here, always an issue in the USA, when you display such a medical image on commodity hardware then whomever looks at it isn't going to be able to see enough details to properly diagnose, say, an evolving tumor. In itself enough to discourage Microsoft to support pixel format conversions for the pixel format, they do not want to be involved in such a lawsuit.
GDI+ supports storing such an image. You can get to the pixel data with Bitmap.LockBits(). Suitable enough for image processing applications. But any attempt to create a Graphics object for such an image or using Graphics.DrawImage() on such an image will cause an exception. Which is what the Bitmap(Image) constructor does. Look for a vendor like LeadTools for library support.

removing the minor variations in pixel intensities in an image

using C# and emgu.
I am working with jpegs which will ultimately end up in a browser.
I am already lowering the quality of the image (to 60%) to reduce the byte array size.
Whenever I get the chance I spend sometime looking around to find ideas to reduce the size of this byte array even more.
I do know the brighter the image the more bytes it seems to hold and the more contrast the image has the more bytes it seems to hold.
Upon googling I came across this:
http://en.kioskea.net/download/download-666-greycstoration
It seemed to imply that by reducing minor pixel variations in an image that I can reduce the byte array defining these jpeg images.
So, to my approach (and understanding)...
Do I iterate through all the pixels and 'average' a group of pixels say on a 4x4 area? Or am I missing the meaning entirely here. I ask because I have already done this but it makes no difference to the image size (in bytes).
I could post my code (and will) but it was just a mock up and not fit for production code.
I am more interested in understanding the meaning/implementation of all this. I can code it this myself as soon as this is clarified (and will post back with the code here)...
Could not add comment due to rep points.
First, if you are going to do and average or some type of filtering on a per pixel basis while using nearest neighbors then you will most likely want to use a filter that is an odd number say 5x5. This will cause the target pixel to be centered in the filter.
You will end up with smaller or equal sized files but only testing will prove how much smaller.
EDIT:
So I think you could also use a simple reduction factor that would give you less colors per image. The algo that I saw was data[i]= (data[i]/N) * N + N/2, N is the reduction factor and data[i] is to be each component of each pixel in your image. The pixels will have the same range 0-255 for each of the rgb channels but certain numbers in this range will not be available to the pixels depending on the reduction factor!
This should give you a reduced jpeg image because you will have less colors.

Finding matches between high quality and low quality, pixelated images - is it possible ? How?

I have a problem. My company has given me an awfully boring task. We have two databases of dialog boxes. One of these databases contains images of horrific quality, the other very high quality.
Unfortunately, the dialogs of horrific quality contain important mappings to other info.
I have been tasked with, manually, going through all the bad images and matching them to good images.
Would it be possible to automate this process to any degree? Here is an example of two dialog boxes (randomly pulled from Google images) :
So I am currently trying to write a program in C# to pull these photos from the database, cycle through them, find the ones with common shapes, and return theird IDs. What are my best options here ?
I really see no reason to use any external libraries for this, I've done this sort of thing many times and the following algorithm works quite well. I'll assume that if you're comparing two images that they have the same dimensions, but you can just resize one if they don't.
badness := 0.0
For x, y over the entire image:
r, g, b := color at x,y in image 1
R, G, B := color at x,y in image 2
badness += (r-R)*(r-R) + (g-G)*(g-G) + (b-B)*(b-B)
badness /= (image width) * (image height)
Now you've got a normalized badness value between two images, the lower the badness, the more likely that the images match. This is simple and effective, there are a variety of things that make it work better or faster in certain cases but you probably don't need anything like that. You don't even really need to normalize the badness, but this way you can just come up with a single threshold for it if you want to look at several possible matches manually.
Since this question has gotten some more attention I've decided to add a way to speed this up in cases where you are processing many images many times. I used this approach when I had several tens of thousands of images that I needed to compare, and I was sure that a typical pair of images would be wildly different. I also knew that all of my images would be exactly the same dimensions. In a situation in which you are comparing dialog boxes your typical images may be mostly grey-ish, and some of your images may require resizing (although maybe that just indicates a mis-match), in which case this approach may not gain you as much.
The idea is to form a quad-tree where each node represents the average RGB values of the region that node represents. So an 4x4 image would have a root node with RGB values equal to the average RGB value of the image, its children would have RGB values representing the average RGB value of their respective 2x2 regions, and their children would represent individual pixels. (In practice it is a good idea to not go deeper than a region of about 16x16, at that point you should just start comparing individual pixels.)
Before you start comparing images you will also need to decide on a badness threshold. You won't calculate badnesses above this threshold with any reliable accuracy, so this is basically the threshold at which you are willing to label an image as 'not a match'.
Now when you compare image A to image B, first compare the root nodes of their quad-tree representations. Calculate the badness just as you would for a single pixel image, and if the badness exceeds your threshold then return immediately and report the badness at this level. Because you are using normalized badnesses, and since badnesses are calculated using squared differences, the badness at any particular level will be equal to or less than the badness at lower levels, so if it exceeds the threshold at any points you know it will also exceed the threshold at the level of individual pixels.
If the threshold test passes on an nxn image, just drop to the next level down and compare it like it was a 2nx2n image. Once you get low enough just compare the individual pixels. Depending on your corpus of images this may allow you to skip lots of comparisons.
I would personally go for an image hashing algorithm.
The goal of image hashing is to transform image content into a feature sequence, in order to obtain a condensed representation.
This feature sequence (i.e. a vector of bits) must be short enough for fast matching and preserve distinguishable features for similarity measurement to be feasible.
There are several algorithms that are freely available through open source communities.
A simple example can be found in this article, where Dr. Neal Krawetz shows how the Average Hash algorithm works:
Reduce size. The fastest way to remove high frequencies and detail is to shrink the image. In this case, shrink it to 8x8 so that there are 64 total pixels. Don't bother keeping the aspect ratio, just crush it down to fit an 8x8 square. This way, the hash will match any variation of the image, regardless of scale or aspect ratio.
Reduce color. The tiny 8x8 picture is converted to a grayscale. This changes the hash from 64 pixels (64 red, 64 green, and 64 blue) to 64 total colors.
Average the colors. Compute the mean value of the 64 colors.
Compute the bits. This is the fun part. Each bit is simply set based on whether the color value is above or below the mean.
Construct the hash. Set the 64 bits into a 64-bit integer. The order does not matter, just as long as you are consistent. (I set the bits from left to right, top to bottom using big-endian.)
David Oftedal wrote a C# command-line application which can classify and compare images using the Average Hash algorithm.
(I tested his implementation with your sample images and I got a 98.4% similarity).
The main benefit of this solution is that you read each image only once, create the hashes and classify them based upon their similiarity (using, for example, the Hamming distance).
In this way you decouple the feature extraction phase from the classification phase, and you can easily switch to another hashing algorithm if you find it's not enough accurate.
Edit
You can find a simple example here (It includes a test set of 40 images and it gets a 40/40 score).
Here's a topic discussing image similarity with algorithms, already implemented in OpenCV library. You should have no problem importing low-level functions in your C# application.
The Commercial TinEye API is a really good option.
I've done image matching programs in the past and Image Processing technology these days is amazing, its advanced so much.
ps here's where those two random pics you pulled from google came from: http://www.tineye.com/search/1ec9ebbf1b5b3b81cb52a7e8dbf42cb63126b4ea/
Since this is a one-off job, I'd make do with a script (choose your favorite language; I'd probably pick Perl) and ImageMagick. You could use C# to accomplish the same as the script, although with more code. Just call the command line utilities and parse the resulting output.
The script to check a pair for similarity would be about 10 lines as follows:
First retrieve the sizes with identify and check aspect ratios nearly the same. If not, no match. If so, then scale the larger image to the size of the smaller with convert. You should experiment a bit in advance with filter options to find the one that produces the most similarity in known-equivalent images. Nine of them are available.
Then use the compare function to produce a similarity metric. Compare is smart enough to deal with translation and cropping. Experiment to find a similarity threshold that doesn't provide too many false positives.
I would do something like this :
If you already know how the blurred images have been blurred, apply the same function to the high quality images before comparison.
Then compare the images using least-squares as suggested above.
The lowest value should give you a match. Ideally, you would get 0 if both images are identical
to speed things up, you could perform most comparison on downsampled images then refine on a selected subsample of the images
If you don't know, try various probable functions (JPEG compression, downsampling, ...) and repeat
You could try Content-Based Image Retrieval (CBIR).
To put it bluntly:
For every image in the database, generate a fingerprint using a
Fourier Transform
Load the source image, make a fingerprint of the
image
Calculate the Euclidean Distance between the source and all
the images in the database
Sort the results
I think a hybrid approach to this would be best to solve your particular batch matching problem
Apply the Image Hashing algorithm suggested by #Paolo Morreti, to all images
For each image in one set, find the subset of images with a hash closer that a set distance
For this reduced search space you can now apply expensive matching methods as suggested by #Running Wild or #Raskolnikov ... the best one wins.
IMHO, best solution is to blur both images and later use some similarity measure (correlation/ mutual information etc) to get top K (K=5 may be?) choices.
If you extract the contours from the image, you can use ShapeContext to get a very good matching of images.
ShapeContext is build for this exact things (comparing images based on mutual shapes)
ShapeContext implementation links:
Original publication
A goot ppt on the subject
CodeProject page about ShapeContext
*You might need to try a few "contour extraction" techniques like thresholds or fourier transform, or take a look at this CodeProject page about contour extraction
Good Luck.
If you calculate just pixel difference of images, it will work only if images of the same size or you know exactly how to scale it in horizontal and vertical direction, also you will not have any shift or rotation invariance.
So I recomend to use pixel difference metric only if you have simplest form of problem(images are the same in all characteristics but quality is different, and by the way why quality is different? jpeg artifacts or just rescale?), otherwise i recommend to use normalized cross-correlation, it's more stable metric.
You can do it with FFTW or with OpenCV.
If bad quality is just result of lower resolution then:
rescale high quality image to low quality image resolution (or rescale both to equal low resolution)
compare each pixel color to find closest match
So for example rescaling all of images to 32x32 and comparing that set by pixels should give you quite reasonable results and its still easy to do. Although rescaling method can make difference here.
You could try a block-matching algorithm, although I'm not sure its exact effectiveness against your specific problem - http://scien.stanford.edu/pages/labsite/2001/ee368/projects2001/dropbox/project17/block.html - http://www.aforgenet.com/framework/docs/html/05d0ab7d-a1ae-7ea5-9f7b-a966c7824669.htm
Even if this does not work, you should still check out the Aforge.net library. There are several tools there (including block matching from above) that could help you in this process - http://www.aforgenet.com/
I really like Running Wild's algorithm and I think it can be even more effective if you could make the two images more similar, for example by decreasing the quality of the better one.
Running Wild's answer is very close. What you are doing here is calculating the Peak Signal to Noise Ratio for each image, or PSNR. In your case you really only need the Mean Squared Error, but the squaring component of it helps a great deal in calculating difference between images.
PSNR Reference
Your code should look like:
sum = 0.0
for(imageHeight){
for(imageWidth){
errorR = firstImage(r,x,y) - secondImage(r,x,y)
errorG = firstImage(g,x,y) - secondImage(g,x,y)
errorB = firstImage(b,x,y) - secondImage(b,x,y)
totalError = square(errorR) + square(errorG) + square(errorB)
}
sum += totalError
}
meanSquaredError = (sum / (imageHeight * imageWidth)) / 3
I asume the images from the two databases show the same dialog and that the images should be close to identical but of different quality? Then matching images will have same (or very close to same) aspect ratio.
If the low quality images were produced from the high quality images (or equivalent image), then you should use the same image processing procedure as a preprocessing step on the high quality image and match with the low quality image database. Then pixel by pixel comparison or histogram matching should work well.
Image matching can use a lot of resources if you have many images. Maybe a multipass approach is a good idea? For example:
Pass 1: use simple mesures like aspect ratio to groupe images (width and height fields in db?) (computationally cheap)
Pass 2: match or groupe by histogram for 1st-color-channel (or all channels) (relatively computationally cheap)
I will also recommend OpenCV. You can use it with c,c++ and Python (and soon Java).
Just thinking out loud:
If you use two images that should be compared as layers and combine these (subtract one from the other) you get a new image (some drawing programs can be scripted to do batch conversion, or you could use the GPU by writing a tiny DirectX or OpenGL program)
Next you would have to get the brightness of the resulting image; the darker it is, the better the match.
Have you tried contour/thresholding techniques in combination with a walking average window (for RGB values ) ?

Categories

Resources