Is is possible to determine from the header of a 1 bit PCX, if the palette should be used or not? - specifications

I've written a PCX decoder (an ImageIO plug-in in Java) and it works just fine, except for one detail:
I don't understand if and/or when I should use the two first colors of the palette in the header, or just use a fixed B/W palette for 1 bit data.
Is there an algorithm or heuristic that will work to determine if the palette should be used, or if a standard B/W should be used instead? Is there some setting in the header I'm missing? Or does one simply need to know this information in advance, before decoding?
The sample files I have, shows quite inconsistent behavior:
I have a sample file I can't share, where it does not matter: Version: 0, bpp: 1, planes: 1, palette info: 0
Using the header palette is fine, as it's the same as the default B/W.
Sample file, where the palette should probably not be used (but being version 2, perhaps is should?): Version: 2, bpp: 1, planes: 1, palette info: 0. Using the palette will make the background blue. Default B/W looks good.
I also have a sample file I can't share where I understand the palette should not be used: Version: 3, bpp: 1, planes: 1, palette info: 0
Here the palette is all black (all 0s), which makes sense as version 3 means no palette.
Sample file, where palette should be used (based on manual inspection of the colors): Version: 5, bpp: 1, planes: 1, palette info: 0.
Using the header palette is fine. Default B/W looks inverted.
Sample file, where the palette should not be used: Version: 5, bpp: 1, planes: 1, palette info: 0 (as the previous sample). Using the palette will make the train white on black background. Default B/W looks good.
Sample file, where it doesn't matter: Version: 5, bpp: 1, planes: 1, palette info: 0. Using the header palette is fine, as it's the same as the default B/W.
I also have a sample file written by Photoshop, that I can't share where the palette should not be used: Version: 5, bpp: 1, planes: 1, palette info: 0
Here the palette is just rubbish values (15,15,15, 14,14,14...0,0,0), making the image look all black. Default B/W looks good.
I've read pretty much all the documentation that I can find on the internet, including:
This pretty informative Dr. Dobbs article
PCX graphics explained
ZSoft PCX File Format Technical Reference Manual
PCX Format
...and a bunch of other, less useful pages, which don't seem to discuss the subject in question.

Related

Ground truth pixel labels in PASCAL VOC for semantic segmentation

I'm experimenting with FCN(Fully Convolutional Network), and trying to reproduce the results reported in the original paper (Long et al. CVPR'15).
In that paper the authors reported results on PASCAL VOC dataset. After downloading and untarring the train-val dataset for 2012 (http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar
), I noticed there are 2913 png files in the SegmentationClass and same number of files in SegmentationObject subdirectory.
The pixel values in these png files seem to be multiples of 32 (e.g. 0, 128, 192, 224...), which don't fall in the range between 0 and 20. I'm just wondering what's the correspondence between the pixel values and ground truth labels for pixels. Or am I looking at the wrong files?
Just downloaded Pascal VOC. The pixel values in the dataset are as follows:
0: background
[1 .. 20] interval: segmented objects, classes [Aeroplane, ..., Tvmonitor]
255: void category, used for border regions (5px) and to mask difficult objects
You can find more info on the dataset here.
The previous answer by captainist discusses png files saved with color palettes, I think it's not related to the original question. The linked tensorflow code simply loads a png that was saved with color map (palette), then converts it to numpy array (at this step the color palette is lost), then saves the array as a png again. The numerical values are not changed in this process, only the color palette is removed.
I know that this question was asked some time ago. But I raised myself a similar question when trying on PASCAL VOC 2012 with tensorflow deeplab.
If you look at the file_download_and_convert_voc2012.sh, there are lines marked by "# Remove the colormap in the ground truth annotations". This part process the original SegmentationClass files and produce the raw segmented image files, which have each pixel value between 0 : 20. (If you may ask why, check this post: Python: Use PIL to load png file gives strange results)
Pay attention to this magic function:
def _remove_colormap(filename):
"""Removes the color map from the annotation.
Args:
filename: Ground truth annotation filename.
Returns:
Annotation without color map.
"""
return np.array(Image.open(filename))
I have to admit that I do not fully understand the operation by
np.array(Image.open(filename))
I have shown here below a set of images for your referece (from above down: orignal image, segmentation class, and segmentation raw class)
The values mentioned in the original question look like the "color map" values, which could be obtained by getpalette() function from PIL Image module.
For the annotated values of the VOC images, I use the following code snip to check them:
import numpy as np
from PIL import Image
files = [
'SegmentationObject/2007_000129.png',
'SegmentationClass/2007_000129.png',
'SegmentationClassRaw/2007_000129.png', # processed by _remove_colormap()
# in captainst's answer...
]
for f in files:
img = Image.open(f)
annotation = np.array(img)
print('\nfile: {}\nanno: {}\nimg info: {}'.format(
f, set(annotation.flatten()), img))
The three images used in the code are shown below (left to right, respectively):
The corresponding outputs of the code are as follows:
file: SegmentationObject/2007_000129.png
anno: {0, 1, 2, 3, 4, 5, 6, 255}
img info: <PIL.PngImagePlugin.PngImageFile image mode=P size=334x500 at 0x7F59538B35F8>
file: SegmentationClass/2007_000129.png
anno: {0, 2, 15, 255}
img info: <PIL.PngImagePlugin.PngImageFile image mode=P size=334x500 at 0x7F5930DD5780>
file: SegmentationClassRaw/2007_000129.png
anno: {0, 2, 15, 255}
img info: <PIL.PngImagePlugin.PngImageFile image mode=L size=334x500 at 0x7F5930DD52E8>
There are two things I learned from the above output.
First, the annotation values of the images in SegmentationObject folder are assigned by the number of objects. In this case there are 3 people and 3 bicycles, and the annotated values are from 1 to 6. However, for images in SegmentationClass folder, their values are assigned by the class value of the objects. All the people belong to class 15 and all the bicycles are class 2.
Second, as mkisantal has already mentioned, after the np.array() operation, the color palette was removed (I "know" it by observing the results but I still don't understand the mechanism under the hood...). We can confirm this by checking the image mode of the outputs:
Both the SegmentationObject/2007_000129.png and SegmentationClass/2007_000129.png have image mode=P while
SegmentationClassRaw/2007_000129.png has image mode=L. (ref: The modes of PIL Image)

How to set the font settings in .pdf, publish via MATLAB and LATEX

While trying to publish a .pdf file for .m MATLAB code, equations (written in latex) are not properly rendered in appropriate (smooth) font, instead fonts looks scattered.
I did try to fix the problem by decreasing the font-size of the editor but that didn't work.
For example: the MATLAB code is:
%% (a) From above plot there are no signs of convergence of $\rho$
%%
% $x^2+e^{\pi i}$
then the .pdf file is not well written, in which equation's fonts are not smooth enough.
thanks for any suggestions.
I was having the same problem, with equation rendering quality being inadequate.
This is my procedure for fixing this in MATLAB R2013b.
1) In the MATLAB command prompt, enter:
edit publish
This should pop up the editor for you to edit 'publish.m'. Beware, the file may be read only. Under Linux, I use an external editor as superuser to edit it.
2) Go to line 811. You should see this:
temptext = text('Parent',tempaxes,'Position',[.5 .5], ...
'HorizontalAlignment','center','FontSize',22, ...
'Interpreter','latex');
Change the value of 'Fontsize' to something larger; I used 30.
3) Go to line 747. You should see this:
swapTexForImg(dom,equationNode,outputDir,fullFilename,equationText,newSize(2),newSize(1))
Change that to
swapTexForImg(dom,equationNode,outputDir,fullFilename,equationText,newSize(2)/scale,newSize(1)/scale)
where scale is the scale factor of your liking. Might have to play with it a bit until you get it right; I used 2.
4) Save the file. Also keep a backup of the original.
5) In the MATLAB command prompt, enter:
rehash toolboxcache
followed by:
clear functions
6) Run publish again.
This should do the job for the PDF. I haven't tried it for HTML, but it should also work. In case of HTMl, don't forget to delete the images created previously.

visualising l*a*b space values in matlab or any software

I have table of l,a,b values and want to visualise these colors in matlab (or any other suitable software). Is there any quick way like series of rectangles filled with color values from the table?
There are several versions of the Lab color space, but presumably you're referring to most common, CIELAB. You can use imwrite in Matlab to create a TIFF image with 'cielab' specified for the 'Colorspace' option. I wouldn't trust Matlab as a viewer for the resultant images though. Photoshop in lab mode (from the menu bar: Image > Mode > Lab Color) would be a good choice if you want work with and see the closest thing to the actual CIELAB space. Other viewers/editors may convert to RGB or CMYK before rendering to the screen (likely without warning you), but maybe you don't mind. If you just want to convert from CIELAB to RGB, you might find these functions useful.
After lots of research, I found out there is a plugin called 'color inspector' that can be used along with ImageJ (all opensource tools). Have excellent capacity to view and analyse different color space. Even it has some color tools that matlab yet to have. here is imageJ: http://rsbweb.nih.gov/ij/download.html
and the plugin
http://rsb.info.nih.gov/ij/plugins/color-inspector.html
Hope this is useful to someone

How to export non-blurry eps images?

I'm exporting an image in Matlab using the eps format, but it smooths the image. Matlab does not blur the image using other formats such as png. I would like to know how to export a non-blurry image with eps format. Here is the resulting image using png:
And here is the resulting image using eps:
UPDATE:
The problem is reproducible on a Mac, and the issue is with the eps renderer rather than MATLAB. For e.g., saving imagesc(rand(20)) and viewing with Preview and GSview results in the following:
Preview screenshot
GSview screenshot
Clearly, the information is not lost. It is just not interpreted/read correctly by some EPS viewers. The solution is simple: use GSview to view your eps images. You can download it from here
On Macs especially, if your end application is latex/pdflatex, you will have to explicitly set it to use GS/GSview, because otherwise, it will default to the Quartz engine, which is baked into the OS.
PREVIOUS ANSWER:
I am unable to reproduce the behavior your described. Here is the code I used, tested using R2010b on WinXP 32-bit:
M = fspecial('gaussian',[20 20],5);
imagesc(M)
print('-dpng','a.png')
print('-depsc2','b.eps')
a.png
b.eps
Perhaps this is an issue with your EPS viewer...
not sure why it works but you can try doing the following:
eps2eps oldfile newfile
does the trick for me (on a mac os)
At first I thought you were doing something incorrectly, but then I remembered that this was an issue that was bothering the hell out of me a year or so ago. I couldn't come up with a way to "fix" this behaviour and from what I've researched, this is most likely a bug and several others have had this problem too and there is no known solution. Of course, I could be wrong about the last part and there might be solutions out there that have come out since I looked for them.
Any way, my workaround this problem was to use pcolor with shading flat instead of imagesc. When you export this to an eps format it preserves the image correctly. Example:
pcolor(rand(20));
shading flat
print('-depsc','figure.eps')
NOTE: You might see the appearance of thin, faint white lines along the anti-diagonals of each little square (depends on the OS & viewer). These are the edges of the graphics primitives that are used to render the image. However, this is not a flaw in MATLAB's export, but rather a fault in rendering in your EPS/PDF viewer. For e.g., with the default settings in Preview on my mac, these lines show up, whereas with the default in Adobe Reader 9.4, they don't appear.
If anyone is still interested in a workaround: Open the .eps-file with text editor and search for "interpolate". You'll probably find "/Interpolate true def" two or three times. Replace "true" with "false" and be happy :)
A note regarding Yoda's answer: in Preview in Mac OS X, you can make the thin white diagonal lines across each of the squares disappear by unchecking "Anti-alias text and screen art". Of course, the downside is that then any text (e.g. figure axes, etc) is not anti-aliased. Unfortunately, unchecking that has no effect on blurriness if you're using imagesc.
Another note is that if you use preview to make a pdf from your eps, the resulting pdf still displays correctly (non-blurry) when you open it in Acrobat.
I've been long struggling with this problem as well. So far, GSView is the only viewer I've found that displays the eps figures produced by Matlab (R2015b) correctly. eps2eps did not work for me (psutils 1.23).
The following eventually worked for me:
Export the figure to pdf, following the instructions here
pdf2ps file.pdf file.eps
I just wrote this simple drop-in replacement for imagesc. It doesn't support all but the most basic features, but I still hope it helps.
function h = imagesc4pdf(C)
[ny nx] = size(C);
px = bsxfun(#plus, [-0.5; 0.5; 0.5; -0.5], reshape(1:nx, [1 1 nx]));
py = bsxfun(#plus, [-0.5; -0.5; 0.5; 0.5], 1:ny);
n = numel(C);
px = reshape(repmat(px, [1 ny 1]), 4, n);
py = reshape(repmat(py, [1 1 nx]), 4, n);
h = patch(px, py, reshape(C,1,n), 'linestyle', 'none');
xlim([.5 nx+.5]);
ylim([.5 ny+.5]);
set(gca, 'ydir', 'reverse');
Apply opengl renderer to the figure
figure(gcf);
set(gcf,'renderer','opengl');
The blurring actually depends on the rendering software your viewer application or printer uses. To get good results all the time, make each pixel in your image an 8x8 block of pixels of the same color, i.e. resize the image like this:
im2 = imresize(im1, 8, 'nearest');
The blurring then only affects the pixels at the edge of each block. 8x8 blocks are best as they compress without nasty artifacts using DCT compression (sometimes used in eps files).
This page helped me a lot: http://tech.mof-mof.co.jp/blog/machine-learning-octave.html (written in Japanese, please use google translate for it)
And this is also helpful: Octave-Gnuplot-AquaTerm error: set terminal aqua enhanced title "Figure 1"...unknown terminal type"
I also answered at https://www.coursera.org/learn/machine-learning/discussions/weeks/2/threads/Dh-aRfqSEeaHSQ6l4xnh6g.
I reinstalled gnuplot like this:
$ brew cask install xquartz
$ brew cask install aquaterm
$ brew uninstall gnuplot
$ brew install gnuplot --with-aquaterm --with-x11 --with-qt # you can show other options by `$ brew options gnuplot`
You may edit ~/.octaverc like this:
setenv("GNUTERM", "qt")
and in octave window, after typing "system gnuplot", then
set pm3d interpolate 2, 2
After saving the file, open octave-cli.app, and type
imagesc(magic(3)), colorbar
I got this.

Graph Formatting Tools For Octave

I know that Matlab allows for you to format the graph after its created through the interface. However there isn't the same features in Octave. Is there a tool that goes between Octave and GnuPlot? If there isn't such a tool, is there a tool that will generate the formatting options?
I've heard of EasyPlot, but it isn't free.
I've discovered there are some formatting options on the GNU Plot graph after it has been generated through octave. If you press 'm' it's then possible to right click and get a menu with choices to format the plot (line styles/colour/background/print). However, for me it crashes a lot and changing the values doesn't seem to have much effect.
There is some other functionality by using these key presses..
m - allow menu on right-click
a - zoom to full window
p - previous zoom level
r - overlay ruler
g - overlay grid
b - toggle border
1 - toggle output reading format
5 - display radius measure tool (when ruler is displayed)
7 - format aspect ratio (useful to get square plots to not distort scale)
These are just the ones I've found by randomly testing the keyboard (!), so this is hardly exhaustive. But hope that helps.
I've used GNU plot in the past for some visualizations. I didn't find any front end interface to set things like colors or labels but it was easy enough to set some basic things on the command line. This site helped me out: http://t16web.lanl.gov/Kawano/gnuplot/intro/plotcalc-e.html
Octave uses Gnuplot as the default plotting backend, though it supports other options. It also supports most of the graph functions that Matlab does, including ones that change the plot after it was created.