Lecture 19 Chapter 13 Images continued. Outline from Chapter 12 -1 13.4 Operating on Images.

Post on 16-Jan-2016

215 views 0 download

transcript

Lecture 19

Chapter 13 Imagescontinued

Outline from Chapter 12 -1

13.4 Operating on Images

Modifying an Image• Locating an image within another image– Scene– Place an object into the scene

scene = imread ('C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water lilies.jpg');image(scene);%bumblebee?bee1 = imread('C:\Documents and Settings\therese\My Documents\CSE1100summer2009\bee1.jpg');image(bee1);beeSize = 75;clippedBee1 = bee1(1:beeSize, 1:beeSize, :);image(clippedBee1);%initialize scenebeeInScene = scene;beeInScene(1:beeSize, 1:beeSize, :) = clippedBee1;image(beeInScene)

Rotating an Image%rotate the bee, around the center of the imagerotatedBee = clippedBee1;for theta = linspace(0,-pi/2,4) for xBee = 1:beeSize for yBee = 1:beeSize centeredX = xBee-beeSize/2; centeredY = yBee-beeSize/2; newCenteredCoordinatesBee = rotation2D(theta)*[centeredX

centeredY]'; newCoordinatesBee = newCenteredCoordinatesBee + beeSize/2 newCoordinatesBee = round(newCoordinatesBee); if newCoordinatesBee(1)<1 newCoordinatesBee(1) = 1; end; if newCoordinatesBee(2)<1 newCoordinatesBee(2) = 1; end; if newCoordinatesBee(1)>beeSize newCoordinatesBee(1) = beeSize; end; if newCoordinatesBee(2)>beeSize newCoordinatesBee(2) = beeSize; end; rotatedBee(newCoordinatesBee(1,1), newCoordinatesBee(2,1), :) = clippedBee1(xBee, yBee, :); end end beeInScene = scene; beeInScene(lastXlocs, lastYlocs, :) = rotatedBee; image(beeInScene) pause(.1)end

Setting a Region within an Imagefor time = t x = 8*time; y = 5*time; xlocations = round((1:beeSize)+x); ylocations = round((1:beeSize)+y); beeInScene = scene; beeInScene(xlocations, ylocations, :) = clippedBee1; image(beeInScene) pause(.05) lastXlocs = xlocations; lastYlocs = ylocations;end

Color Adjustment Via Mask

• Use the image to develop a logical array• Use the logical array as a mask• Update the image where mask allows

Green in Moth

We can use the combination of the presence of high level of greenwith the lower levels of red and blue, to identify the moth, e.g., >150.

Develop the Mask

mask(mothSize,mothSize,3) = 0; %establish size, mask(:,:,3) = 0; %no changes to blue, yetmask(:,:,1) = 0; %no changes to red, yetmask(:,:,2) = clippedMoth1(:,:,2)>greenThreshold... & clippedMoth1(:,:,1)<redThreshold... & clippedMoth1(:,:,3)<blueThreshold; %if the green is > threshold, and blue and red

are below theirs%then allow green level at that pixel to change

Apply the Mask

greenerMoth = clippedMoth1;greenerMoth(mask) = uint8(255);mask(:,:,3)=mask(:,:,2);% transfer the locations to the blue

signalmask(:,:,2)= 0; %leave green alonegreenerMoth(mask) = uint8(0); %no bluemask(:,:,1)=mask(:,:,3); %transfer the locations to the red signalmask(:,:,3)=0; %leave the blue alonegreenerMoth(mask) = uint8(0); %no redimage(greenerMoth)

Enhanced Green

Find Yellow Edges

Arithmetic with Colorsyellow = gr + red;plot(red, 'r');hold onplot(gr, 'g');plot(bl, 'b');plot(yellow, 'y');

More Arithmetic with Colors

rat = yellow ./ bl;subplot(2,1,2)plot(rat,'--k');

Maybe 2.5 has meaning as athreshold; maybe 3.5

Edge Detection in Color

Outline from Chap 14

• 14.1 Physics of Sound• 14.2 Recording and Playback• 14.3 MATLAB Implementation• 14.4 Time Domain Operations– 14.4.1 Slicing and Concatenating Sound– 14.4.2 Musical Background– 14.4.3 Changing Sound Frequency Poorly– 14.4.4 Changing Sound Frequency Well

Physics of Sound

• Dynamic range: soft sounds and loud sounds have significantly different levels, so a logarithmic scale is used

• Frequencies serve as a basis set for describing waveforms. Spectral decomposition is extremely useful.

Recording and Playback

• Capturing the dynamic range: many significant bits– Approximately softest sound is 10 billion times

softer than the loudest comfortable sound– 16 bits is popular

• Capturing the high frequency content: high enough sampling rate.– Highest frequency heard said to be 20 kiloHertz– Sample at twice that suggests 40 kHz

In MATLAB -1• Read with

– Wavread for .wav files– Auread for .au files

– [Y,FS,NBITS]=WAVREAD(FILE) returns the sample rate (FS) in Hertz and the number of bits per sample (NBITS) used to encode the data in the file.

[b16, F16, n] = wavread('C:\Documents and Settings\therese\My Documents\CSE1100summer2009\chimes.wav');

sound(b16, F16)[bb16, FF16, n] = wavread('C:\Documents and Settings\therese\My Documents\

CSE1100summer2009\ding.wav');sound(bb16, FF16)[bbb16, FFF16, n] = wavread('C:\Documents and Settings\therese\My Documents\

CSE1100summer2009\tada.wav');sound(bbb16, FFF16)

In MATLAB -2

• Concatenation

longSound = [b16; bb16; bbb16];sound(longSound,FFF16)

In MATLAB -3

• Examining the time domain waveform

In MATLAB - 4

• Selecting in the time domainpiece1Start = round(length(b16)*(1/4));piece1End = round(length(b16)*(3/4));piece1 = b16(piece1Start:piece1End,:);

Modify Frequency by ResamplingHere the Resampling Is by Subset

[piano, piano16, n3] = wavread('C:\Documents and Settings\therese\My Documents\CSE1100summer2009\piano.wav');

sound(piano, piano16)note = piano;half = 2^(1/12);whole = half^2;for index = 1:8 sound(note, piano16); pause(.5); if (index==3)||(index ==7) mult = half; else mult = whole; end note = note(ceil(1:mult:end));end;

File from http://chronos.ece.mismi.edu/~dasp/samples/samples.html

Frequency Domain

• Different from time domain• Express our time waveform as a sum of other,

simpler time waveforms.• f(t) = sum of sin and cos functions, each with an

amplitude, and a frequency.• Then we can modify the amplitudes of the

individual sins and cosines, where the modification depends upon the frequency, to obtain useful effects (like taking the hissy noise out of damaged records).

Waveform Composition

• An algorithm, the fast Fourier Transform (FFT), calculates the coefficients of the components of the decomposition of a time-domain waveform.

• Each frequency used in the decomposition has a coefficient.

• If the coefficients are plotted vs. frequency, that is a spectrum.

In MATLAB

• FFT operates on time domain to generate frequency component coefficients.

• iFFT operates on frequency domain coefficients to generate time domain representation.

Example Using FFTdt = 1/400;pts = 10000;f = 8;t = (1:pts)*dt; %sampling timesx1 = sin(2*pi*f*t);x2 = 3*sin(2*pi*5*f*t);x3 = 7*sin(2*pi*11*f*t);x=x1+x2+x3;subplot (3,1,1)plot(t(1:end/25),x(1:end/25));title('Time Domain Sine Waves');ylabel('Amplitude');xlabel('Time (Sec)');Y =fft(x); %Y gets the coefficientsdf = 1/ t(end); %the frequency

spacingfmax = df * pts /2;f = (1:pts)*2*fmax/pts; %frequencies

subplot(3,1,2);plot(f, real(Y));title('Real Part');xlabel('Frequency (Hz)');ylabel('Energy');subplot(3,1,3);plot(f, imag(Y));title('Imaginary Part');xlabel('Frequency (Hz)');ylabel('Energy');

Comparing the Timbre of Instruments

Code for Previous disp('starting timbre')[trumpet, F16, n1] = wavread('C:\Documents and Settings\therese\My

Documents\CSE1100summer2009\trumpet.wav');sound(trumpet, F16)%F16ptsTrumpet = length(trumpet);[cello, FF16, n2] = wavread('C:\Documents and Settings\therese\My

Documents\CSE1100summer2009\cello.wav');sound(cello, FF16)%FF16ptsCello = length(cello);[piano, FFF16, n3] = wavread('C:\Documents and Settings\therese\My

Documents\CSE1100summer2009\piano.wav');sound(piano, F16)%FFF16ptsPiano = length(piano);dtTrumpet = 1/F16;dtCello = 1/FF16;dtPiano = 1/FFF16;f = 8;tTrumpet = (1:ptsTrumpet)*dt; %sampling timestCello = (1:ptsCello)*dt; %sampling timestPiano = (1:ptsPiano)*dt; %sampling timespause(tcello(ptsCello));

subplot (3,1,1)plot(tTrumpet(1:round(end/25)),trumpet(1:round(end/

25)),'y');hold onplot(tCello(1:round(end/25)),cello(1:round(end/25)),'r');plot(tPiano(1:round(end/25)),piano(1:round(end/25)),'g');title('Time Domain Trumpet');ylabel('Amplitude');xlabel('Time (Sec)');Ytrumpet =fft(trumpet); %Y gets the coefficientsYcello = fft(cello);Ypiano = fft(piano);dfTrumpet = 1/ tTrumpet(end); %the frequency spacingdfCello = 1/ tCello(end); %the frequency spacingdfPiano = 1/ tPiano(end); %the frequency spacingfmaxTrumpet = dfTrumpet * ptsTrumpet /2;fmaxCello = dfCello * ptsCello /2;fmaxPiano = dfPiano * ptsPiano /2;fTrumpet = (1:ptsTrumpet)*2*fmaxTrumpet/ptsTrumpet;

%frequenciesfCello = (1:ptsCello)*2*fmaxCello/ptsCello; %frequenciesfPiano = (1:ptsPiano)*2*fmaxPiano/ptsPiano; %frequenciessubplot(3,1,2);plot(fTrumpet, real(Ytrumpet),'y');hold onplot(fCello, real(Ycello),'r');plot(fPiano, real(Ypiano),'g');title('Real Part');xlabel('Frequency (Hz)');ylabel('Energy');

More Code

• subplot(3,1,3);• plot(fTrumpet, imag(Ytrumpet));• hold on• plot(fCello, imag(Ycello),'r');• plot(fPiano, imag(Ypiano),'g');• title('Imaginary Part');• xlabel('Frequency (Hz)');• ylabel('Energy');