Thursday, April 16, 2020

How to display both percentage and value in a piechart in matlab

https://www.mathworks.com/matlabcentral/answers/518138-how-to-show-both-value-and-percentage-in-a-piechart Thanks to Ameer Hamza, we have the answer now: X = [1/3 2/3]; value = compose(['Val=%.3f'], X); percents = compose([newline 'P=%.3f%%'], X/sum(X)*100); pie(X, strcat(value, percents))

Wednesday, April 15, 2020

find vs logical indexing

people usually use "find" to get some values with a threshold, but the logically indexing is much easier and faster. This is the lesson I learned today: https://www.mathworks.com/help/matlab/matlab_prog/find-array-elements-that-meet-a-condition.html https://www.mathworks.com/matlabcentral/answers/66992-logical-indexing-is-usually-faster-than-find n = 1e6; r = rand(n,1); f = randn(n,1)+10; si = r>0.5; tic j = find(si); s1 = f(j); % with FIND toc tic s2 = f(si); % with logical indexing toc all(s1==s2) This is the best example I found, I usually used s1 method, but s2 is faster, if you dont' need the actual index number

Sunday, April 12, 2020

load and wirte tif stacks in matlab

It takes time to search for these codes, for convenience, let me keep a copy her for everyone. This will read from a tif stack (not many single files): Read part: fname = '20191229_fov02_14.tif'; info = imfinfo(fname); num_images = numel(info); B = imread(fname,1); for k = 2:num_images A = imread(fname, k); B = cat(3,B,A); end Save part: outputFileName = 'filteredbinary.tif'; imwrite(afterOp(:, :, 1), outputFileName, 'Compression','none'); for K=2:length(afterOp(1, 1, :)) imwrite(afterOp(:, :, K), outputFileName, 'WriteMode', 'append', 'Compression','none'); end

dilution, errosion, openning and closing

These are important basic concepts in image processing. see nice videoes here: https://www.youtube.com/watch?v=ZTbGlriKFtU https://www.youtube.com/watch?v=IcBzsP-fvPo

learning matlab image processing-1: imbinarize

Today I found a great function of matlab for image processing: imbinarize This function actually replaces the im2bw function. To get an idea of the difference, here is an example. If we just the old code and the new function in 'global'(default) model. There is not much difference: level = 0.57; Ithresh = im2bw(Igray,level); BW=imbinarize(Igray); imshowpair(Ithresh, BW, 'montage'); title('old (left) vs new (right,global mode)')
However, the really useful part is the 'adaptive' function for this binarizer: level = 0.57; Ithresh = im2bw(Igray,level); BW=imbinarize(Igray,'adaptive'); imshowpair(Ithresh, BW, 'montage'); title('old (left) vs new (right,global mode)')
This actually saves the effort to split the channels of RGB and segment them separately, as described here in the old version of matlab (https://www.youtube.com/watch?v=1-jURfDzP1s, a very nice presentation). Find out more details about this function in https://www.mathworks.com/help/images/ref/imbinarize.html Good thing is that it also works for 3D stacks. will give it try:)