Wednesday, August 28, 2013

ImageMagick: batch resize and DPI change

mogrify -resize 59.06% -density 150x150 *.tif
mogrify -resize 1024x1234 -density 150x150 *.tif
If the images are in grayscale than the above will convert them to RGB. Thus one can use the following: mogrify -resize 59.06% -density 150x150 -colorspace gray -layers flatten *.tif
mogrify -resize 1024x1234 -density 150x150 -colorspace gray -layers flatten *.tif

Thursday, August 15, 2013

MATLAB: Get absolute path of a file or a folder

function absPath = absfilepath(inPath)
% absfilepath(inPath)
% Get absolute path of a file or a folder
%
% Java File class is used.
%
% EXAMPLE
% absfilepath('.') %reults in '/mnt/c/Users/me/MATLAB'
%
javaFileObj = java.io.File(inPath);
absPath = char(javaFileObj.getCanonicalPath());
view raw absfilepath.m hosted with ❤ by GitHub

Wednesday, August 14, 2013

Matlab: split vector into number of parts of roughly the same size

function chuckCell = splitvect(v, n)
% Splits a vector into number of n chunks of the same size (if possible).
% In not possible the chunks are almost of equal size.
%
% based on http://code.activestate.com/recipes/425044/
chuckCell = {};
vectLength = numel(v);
splitsize = 1/n*vectLength;
for i = 1:n
%newVector(end + 1) =
idxs = [floor(round((i-1)*splitsize)):floor(round((i)*splitsize))-1]+1;
chuckCell{end + 1} = v(idxs);
end
assert(sum(cellfun(@numel, chuckCell)) == vectLength, ...
'More or less elements after split')
view raw splitvect.m hosted with ❤ by GitHub
%split into 3 parts
chunckCell=splitvect(1:10, 3);
chunckCell{:}
ans = 1 2 3 ans = 4 5 6 7 ans = 8 9 10