Artifacts appear using imread function from opencv - matlab

I use imread function to read one jpeg file and save the rgb image in bmp format. Comparing the two files, I found artifacts appear and use green circle to denote artifacts. The version of OpenCV is 3.0. I compile the libraries by myself with SSE, SSE2 and SSE3 switchd on (default setting). My OS is windows 7 professional. You can use the following image to check.
original jpeg image
saved bmp file
If I read the jpeg file in Matlab, the rgb image is correct. I save rgb image in png format in Matlab, read the png file using opencv and save the loaded image in bmp file. Everything is OK. It seems that there is a problem with jpeg decoder. The jpeg library used is libjpeg.lib.
Due to the size limit, I cut the patch from the second image.

You're always going to get some artifacts in JPEG. You can reduce the appearance of such artifacts by changing the quantization tables used (usually with loss of compression).
JPEG encoders often use a "quality" setting to change the quantization tables.

Related

How to open a .img/.rrd satellite image in Matlab?

I have a couple of multispectral satellite images which are in .img/.rrd format and I want to oopen them in Matlab for further processing.
I'm not sure Matlab can recognise .img /.rdd files directly, but you could try using ImageJ or Fiji to convert your .img files to one of the image formats that is accepted. e.g. tiff, png, bmp, jpeg. The list of accepted formats can be found by typing 'imformats' in the command line.

Why does PNGing an image from a JPG make it 10x bigger?

I have an image captured via webcam of my cat (the subject might not be important). I've aquired it as a 31 kB JPG file. When I open it with an image editor, then save it (without alteration) as a PNG (max. compression) it stores as a 297 kB file.
Why is the PNG file 10x larger than the original JPG. As I understand it, opening a JPG is lossless, and saving a PNG is lossless. So, where does all the extra data come from? If the image comes entirely out of the small file, why does it then re-save to 10x the size on disc?
Please read this carefully. I'm not asking why the two formats produce different file sizes from an original image. I'm asking why opening an existing JPG then saving that exact same image as PNG is 10x bigger. I don't think that this is a duplicate question as far as I can ascertain.
Some tests I've done:-
I've looked at both JPG and PNG and they look identical.
I've zipped both files and got cat.jpg.zip as 31 kB, and cat.png.zip as 296 kB. I take this to mean that both files are fully compressed with virtually no latent redundancy.
I've tried this via the BMP format as well; cat.jpg (31 kB) -> cat.bmp (922 kB) -> cat.bmp.zip (404 kB).
Any ideas regarding the mysterious extra data..?
JPEG inherently produces better compression than PNG. However, JPEG trades off fidelity to the original image for better compression. PNG reproduces the original exactly.
If you go from JPEG to PNG, you are not going to see a changes.
If you go from PNG to JPEG, it is likely you ill see a lot of change.
JPEG uses a series of compression techniques. One of them, the DCT, transforms the image. This creates a subtle waviness in color. For example, if you start with a solid red block that is all one color, JPEG produces a lot of slight color variations.
PNG compression relies on finding repeated pixel patterns in scan lines. The subtle color variations introduced by JPEG can make PNG compression less effective.
The extra data you refer to is simply the difference in how the two format represent the same image.
If I take a JPEG image from a camera and convert to PNG, the result is usually about 10 times larger.
For a PNG Graphic image going to JPEG, I normally get files about 1/3 smaller.
JPG uses lossy compression, while PNG uses loseless compression. When you convert JPG to PNG, what actually happens is uncompressing from JPG and saving the results in PNG.
The "extra data" is actually due to different algorithms used.
As for why zipped files also have different size, that's because PNG has to save all pixels(including those JPG has lossy compressed) loselessy.

PIL: converting an image with mode "I" to "RGB" results in a fully white image

The image at the end of this question is a PNG with mode I, which stands for Indexed, as far as I can tell.
I'm trying to create a thumbnail out of it, and save it as JPG with PIL.
However, is I leave the mode alone, PIL won't let me resize it with error unable to generate thumbnail: cannot write mode I as JPEG.
If I convert it to RGB, the result will be a fully white image.
Is there a way to fix this?
https://www.dropbox.com/s/2d1edk2iu4ixk25/NGC281.png
The input image is a 16-bit grayscale PNG, and it appears PIL has a problem with this. Manually converting it to an 8-bit image before further processing makes it work again.
The problem may originate inside PIL itself. The PyPNG homepage asserts
..PIL only has internal representations (PIL mode) for 1-bit and 8-bit channel values. This makes me wonder if PIL can read PNG files with bit depth 2 or 4 (greyscale or palette), and also bit depth 16 (which PNG supports for greyscale and RGB images).
Then again, that page is from 2009. It could be worth tracking down where PIL is maintained from, and report this as a bug (? Or possibly a feature request?).

Matlab: how to save TIFF with transparency or PNG with no compression?

I have to process a lot of images and save results to image files with transparency in Matlab. But PNG compression takes too much time for me. How can I save PNG with no compression or TIFF with transparency? Are there other ways to save an image without compression and with transparency?
It's my first question here, sorry for my bad English and wrong question style if there are any mistakes in question.
Using the TIFF class in Matlab you can write TIFFs with transparancy:
%# create a synthetic RGBA image
ch = checkerboard(100);
rgba = repmat(ch,[1,1,4]);
rgba(:,:,4) = rgba(:,:,4)==0;
rgba = uint8(round(rgba*255));
%# create a tiff object
tob = Tiff('test.tif','w');
%# you need to set Photometric before Compression
tob.setTag('Photometric',Tiff.Photometric.RGB)
tob.setTag('Compression',Tiff.Compression.None)
%# tell the program that channel 4 is alpha
tob.setTag('ExtraSamples',Tiff.ExtraSamples.AssociatedAlpha)
%# set additional tags (you may want to use the structure
%# version of this for convenience)
tob.setTag('ImageLength',size(ch,1));
tob.setTag('ImageWidth',size(ch,2));
tob.setTag('BitsPerSample',8);
tob.setTag('RowsPerStrip',16);
tob.setTag('PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
tob.setTag('Software','MATLAB')
tob.setTag('SamplesPerPixel',4);
%# write and close the file
tob.write(rgba)
tob.close
%# open in Photoshop - see transparency!
Matlab's imwrite does not have parameter for the PNG compression level. If it did, you could set it to zero for no compression. While for TIFF it does have a none option for Compression, there is no alpha channel. You can write to the old Sun Raster (RAS) format with an alpha channel and no compression. Though nothing would likely be able to read it.
"There is no uncompressed variant of PNG. It is possible to store uncompressed data by using only uncompressed deflate block"
The uncompressed deflate block uses a header of 5 bytes + up to 65535 bytes of uncompressed data per block.
http://www.w3.org/TR/PNG-Rationale.html

Matlab: How to save plots of patches / fill() automatically in the code?

quick question: I'm creating "random" polygons using either the patch() or the fill() function in Matlab. This works quite good and it is plotted correctly.
However, I need to at least save a few hundres polygons as images to my hard drive for working with them later - so I'm looking for a way to directly save the image in my function rather than saving each polygon myself using the file-menu.
Is there any way to do this?
Thanks in advance!
You can indeed use the print function, but I would not use the jpeg device. JPEG is never the right format for plots (you will get a lot of artifacts near all your lines).
If you need a bitmap image, try the png or tiff device. If you don't need a bitmap, use the appropriate vector image format: fig is the native MATLAB format (which allows you to edit the plot afterwards), so this is the best one if you stick with MATLAB for all your operations. For exporting to other software, I would recommend pdf (works almost anywhere), epsc (EPS with color, great for LaTeX or inkscape), wmf/emf (Windows Metafile, so Windows only, but great for including the images in MS Office). Or you could of course use any of the other formats mentioned in the print documentation.
Sometimes it's a pain in the neck to get the format of your image all right (especially with PDF output). Just take a look at the different properties of your figure and more specifically the PaperSize, PaperUnits and PaperPosition.
The easiest way, and I guess the best solution, is to save as a .fig file. You can do this by using saveas:
h = figure;
% your plot commands here
saveas(h,'mFile.fig');
Afterwards, you can reload the image with the openfig function:
openfig('mFile.fig');
Have to add this answer. This function is helping a lot.
This function saves a figure or single axes to one or more vector and/or bitmap file formats, and/or outputs a rasterized version to the workspace, with the following properties:
   - Figure/axes reproduced as it appears on screen
   - Cropped/padded borders (optional)
   - Embedded fonts (pdf only)
   - Improved line and grid line styles
   - Anti-aliased graphics (bitmap formats)
   - Render images at native resolution (optional for bitmap formats)
   - Transparent background supported (pdf, eps, png)
   - Semi-transparent patch objects supported (png only)
   - RGB, CMYK or grayscale output (CMYK only with pdf, eps, tiff)
   - Variable image compression, including lossless (pdf, eps, jpg)
   - Optionally append to file (pdf, tiff)
   - Vector formats: pdf, eps
   - Bitmap formats: png, tiff, jpg, bmp, export to workspace