90 likes | 320 Views
CS100: Into to Computer Science. Image Processing in MatLab. MatLab is a powerful tool to manipulate graphics and images. Prepared by Fred Annexstein University of Cincinnati Some Rights Reserved. WORKING WITH IMAGES in MatLab. Basic Image functions: Reading: img1=imread('image1.jpg');
E N D
CS100: Into to Computer Science Image Processing in MatLab MatLab is a powerful tool to manipulate graphics and images Prepared by Fred Annexstein University of Cincinnati Some Rights Reserved
WORKING WITH IMAGES in MatLab Basic Image functions: Reading: img1=imread('image1.jpg'); Writing: imwrite(img1, 'image2.jpg'); Displaying: imshow(img1); Recall that Last time we did an Exercise: Find 2 images online and add them together. You only needed to crop them to the same size.
WORKING WITH IMAGES in MatLab RESIZING >> img=imread(‘my.jpg'); >> [rows, cols, colors]= size(img) % Increase the number of rows >> stretchfactor = 1.5 >> rowVec= linspace(1,rows,stretchfactor*rows); >> newrows=round(rowVec); % Decrease number of columns >> stretchfactor = 0.75; >> colVec= linspace(1,cols,stretchfactor*cols); >> newcols=round(colVec); >> newimag=img(newrows,:,:) >> imshow(newimg); >> newimag=newimg(:,newcols,:) >>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 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…..