Miscellaneous Matlab snippets

 

 

im = imread(‘im.jpg’);

imshow(im);

impixelinfo; // see the colors and positions on the figure itself

 

 

R = im(:,:,1); // take out the red channel as a 2d array

G = im(:,:,2); // same for green

B = im(:,:,3) // for blue

 

 

grayim = rgb2gray(im); // convert from color to grayscale

imwrite(grayim, ‘gray.jpg’); // write the image as a jpeg

 

 

newim = grayim – 50; // take 50 off of each pixel value

imshow(newim);

impixelinfo; // we see that white pixels become more gray

 

 

newim = grayim + 200; // we’ll have overflow now when we surpass 255

 

 

flipim = grayim(:,end:-1:1); // create new image that is reversed left to right

imshow(flipim);

 

 

diff = flipim – grayim;

diff2 = double(flipim) – double(grayim);

subplot(1,2,1); imagesc(diff);

subplot(1,2,2); imagesc(diff2);

 

 

min(diff(:))

min(diff2(:)) // will be negative because we first typecast to doubles

 

 

% imagesc is a function in the tutorial that is useful to view things that may be outside of [0,255] – it scales the data to use the full range of values.  If you call imagesc(x); on a 2d matrix x and get a colored output, you can instead show it in grayscale (sometimes easier to interpret) by calling ‘colormap gray;’