100 likes | 132 Views
Into to Computer Science. Introduction to MatLab: Image Processing. - MatLab stands for Matrix Laboratory. - Most of the programming operations have as input or output a matrix or a vector. - Images are often represented a matrices.
E N D
Into to Computer Science Introduction to MatLab: Image Processing - MatLab stands for Matrix Laboratory. - Most of the programming operations have as input or output a matrix or a vector. - Images are often represented a matrices. - MatLab is a powerful tool to manipulate graphics and images. Prepared by Fred Annexstein University of Cincinnati Some Rights Reserved
Overview Like Scratch, MatLab is an interpreted programming language. Unlike Scratch, Matlab is a language in which you type commands at the prompt. Try these MATRIX AND VECTOR OPERATIONS This is how we can define a vector >> v=[1, 2, 3] Matlab prints out the following v = 1 2 3 Similarly we can define a matrix >> M= [ 1 2 3; 4 5 6; 7 8 9] The result is: M = 1 2 3 4 5 6 7 8 9 If you want to suppress the MatLab output then you need to finish the line with semicolon as follows. >>M= [ 1 2 3; 4 5 6; 7 8 9];
Projection Say you want to extract some rows and columns of a matrix. This is called a projection. We simply give the subset of rows and columns as parameters, as follows >> M11=M(2:3 , 2:3) M11 = 5 6 8 9 To specify all elements in a given dimension one can use ':‘ So to get all rows but just columns 1 and 2, we type >> A= M( :, 1:2) A = 1 2 4 5 7 8
WORKING WITH IMAGES in MatLab Let’s talk about image files and their formats….. Color vs GrayScale Basic Image Processing functions: Reading in an image: >> img1=imread('Water lilies.jpg'); Displaying an image: >> imshow(img1); Finding out size of an image: >> size(img1); >> size(img1) ans = 600 800 3
WORKING WITH IMAGES in MatLab • Cropping an image: • >> imgsmall=img1(200:300,300:400,1:3); • >> imshow(imgsmall) • >> imgsmall=img1(150:250,350:450,1:3); • >> imshow(imgsmall) • >> size(imgsmall) • ans = • 101 101 3 • Exercise: 1. Find 2 images online • 2. Crop them to the same size • 3. Add the two images together. • 4. Display resulting image • Advanced Exercise: • Rescale images to same size then add them • See next slide to see HOWTO rescale
ReScaling We can rescale by changing the number of rows and columns, yet preserve the information in the image >> [rows, cols, colors]= size(img1) rows = 600 cols = 800 colors = 3 % Increase the number of rows >> stretchfactor = 1.5 >> rowVec= linspace(1,rows,stretchfactor*rows); >> newrows=round(rowVec); >> newimag=img1(newrows,:,:) >> imshow(newimg); % Decrease number of columns >> stretchfactor = 0.75; >> colVec= linspace(1,cols,stretchfactor*cols); >> newcols=round(colVec); >> newimag=newimg(:,newcols,:) >>imshow(newimg)
Example Program: Inverting an image To invert or to add two images we need to convert to double and then rescale the result back so that it looks like an image InvImg= 1 - double(IMG1)/255; NewImg = uint8(round(InvImg*255))) Imshow(NewImg);
WORKING WITH IMAGES in MatLab • Color Masking • Sometimes we want to replace pixels of an image of one or more colors with pixels from another image. It is useful to use a “blue or green screen” in some instances. • Find an image with a big plot of one color. First we will replace that color. And then we will find another image for pixel replacement. • Let us plot the color values of one chosen row…This will tell us the pixel values of the color we want to replace. • v = imread(‘myimg.jpg’) • image(v) • row= input(‘which row?’); • red = v(row,:,1); • green = v(row,:,2); • blue = v(row,:,3); • plot(red,’r’); • hold on • plot(green,’g’); • plot(blue,’b’);
WORKING WITH IMAGES in MatLab • Suppose we want to replace those values whose intensities exceed a threshold value of 160 in each color. • v= imread(‘myimg.jpg’); • thresh= 160 • layer = (v(:,:,1) > thresh) & (v(:,:,2) > thresh) (v(:,:,2) > thresh) • mask(:,:,1) = layer; • mask(:,:,2) = layer; • mask(:,:,3) = layer; • If you want to only mask a portion of the image you can use something like… • >> mask(700:end,:,:)= false; • Which sets the mask so that we do not affect rows 700 and above • To reset the color to red • >>newv = v; • >>newv(mask)(1) = 255 • Or to replace pixels from a different image w use…>> newv(mask) = w(mask); • Let us try to do this with a blue screen…..