few elements from Matlab programming related to basic image processing
Course homepage: http://cmp.felk.cvut.cz/cmp/courses/ZSO
... and update Matlab caches
clear all
rehash
2- or 3-dimensional array
imrgb = imread('Kravy_na_podzim.jpg'); imgray = rgb2gray(imrgb); whos('imrgb','imgray')
Name Size Bytes Class imgray 480x551 264480 uint8 array imrgb 480x551x3 793440 uint8 array Grand total is 1057920 elements using 1057920 bytes
figure(1); clf imshow(imrgb); title('RGB image') axis on figure(2); clf imshow(imgray) title('Gray scale image') axis on
For some image arithmetics it will be needed to change the default integer data type into double.
Matlab DOES recognize data types. Even if the values of the variables do not change the output (displaying and saving) could. Be aware of it! This caused many confusions in past.
for type uint8 Matlab expects values
and for type double it expects
imrgb_float = double(imrgb); imgray_float = double(imgray); figure(3); clf subplot(2,2,1); subimage(imrgb); title('RGB image with integers'); subplot(2,2,2); subimage(imrgb_float); title('RGB image with floats'); subplot(2,2,3); subimage(imgray); title('gray image with integers'); subplot(2,2,4); subimage(imgray_float); title('gray image with floats');
% saving bitmaps imwrite(imgray,'examples_of_saving.png'); % saving (printing) graphics (figures) % activate the proper figure figure(3); % save as png bitmap print -dpng figure3.png % or as function % save as color encapsulated postscript % eps file may get very big. However, it is very useful for publishing. print('-depsc','figure3.eps')
is pretty straightforward
% Subimage by specifing coordinates imgray_sub = imgray(300:400,50:350); % find all pixels where the intensity exceeds threshold threshold = 128; idx = imgray<threshold; % indexing by a logical conditions imgray_changed = imgray; imgray_changed(idx) = 0; figure(4); clf; imshow(imgray_sub); axis on; figure(5); clf; imshow(imgray_changed); axis on;
indexing of multidimensional data is slightly more complicated
% Subimage by specifing coordinates imrgb_sub = imrgb(300:400,50:350,:); % find all pixels where at least one from RGB values is lower threshold threshold = 70; idx = any(imrgb<threshold,3); % create the index matrix for indexing RGB image idxmat = repmat(idx,[1,1,3]); % indexing by a logical conditions imrgb_changed = imrgb; imrgb_changed(idxmat) = 0; % and more straighforward replacing of all pixels where % the RGB average exceeds 200 (check the sky and the rock) imrgb_changed(repmat(mean(imrgb,3)>200,[1,1,3])) = 255; figure(6); clf; imshow(imrgb_sub); axis on; figure(7); clf; imshow(imrgb_changed); axis on;