%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % manipulating images - demo part 1 - useful for HW4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % read in an image img = (imread('2904052434_762e80bb2d_o.jpg')); size(img) % show the image imagesc(img); % get some coordinates from the image [x,y] = ginput(2); % x coords of input x % y coords of input y % make them integers x = round(x); y = round(y); % get out a small subimage subimg = img(y(1):y(2),x(1):x(2),:); imagesc(subimg) axis equal % set that part to black newimg = img; for i=y(1):y(2) for j=x(1):x(2) newimg(i,j,:) = [0 0 0]; end end imagesc(newimg); % or replace it with another img img2 = (imread('flower.jpg')); imagesc(img2); size(img2) % resize img2 so it fits in the hole we have made img2 = imresize(img2,[y(2)-y(1)+1,x(2)-x(1)+1]); newimg(y(1):y(2),x(1):x(2),:) = img2; newimg(find(newimg<0))=0; imagesc(newimg); img2 = (imread('flower.jpg')); [m,n,c] = size(img2); img2 = imresize(img2,.2); [m,n,c] = size(img2); newimg2 = img; newimg2(1:m,1:n,:) = img2; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % pixel representation - demo part 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% img = (imread('2904052434_762e80bb2d_o.jpg')); img1 = img(1:end,50:end,:); img2 = img(1:end,1:end-49,:); imagesc(img1); figure; imagesc(img2); % unshifted similarity average value diff sim1 = sum(sum(sum(abs(img-img))))/(size(img,1)*size(img,2)*size(img,3)); % shifted similarity - average value diff - large! sim2 = sum(sum(sum(abs(img1-img2))))/(size(img,1)*size(img,2)*size(img,3)); close all %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % color representation - demo part 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% img = (imread('2046794090_5b3c0c9cc1_o.jpg')); % reads in as uint8, convert to doubles img = (img); imagesc(img); % average color sumcolor = [0 0 0]; for i=1:size(img,1) for j=1:size(img,2) sumcolor = sumcolor+reshape(img(i,j,:),[1 3]); end end avecolor = sumcolor/(size(img,1)*size(img,2)); % let's look at it by putting it into a 10x10 image avecolorimg = zeros(10,10,3); for i=1:10 for j=1:10 avecolorimg(i,j,:) = avecolor; end end figure; imagesc(avecolorimg); % spatial grid of average colors newimg = zeros(size(img)); % break image up into 50x50 pixel regions for i=1:50:size(img,1) stopi = min(i+49,size(img,1)); for j=1:50:size(img,2) % calculate average color in each of the regions stopj = min(j+49,size(img,2)); avecolorr = mean(mean(img(i:stopi,j:stopj,1))); avecolorg = mean(mean(img(i:stopi,j:stopj,2))); avecolorb = mean(mean(img(i:stopi,j:stopj,3))); newimg(i:stopi,j:stopj,1) = avecolorr; newimg(i:stopi,j:stopj,2) = avecolorg; newimg(i:stopi,j:stopj,3) = avecolorb; end end imagesc(newimg); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % edges - demo part 4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% img1 = (imread('redumbrella.jpg')); img2 = (imread('grayumbrella2.jpg')); imagesc(img1); figure; imagesc(img2); BW1 = rgb2gray(img1); size(BW1); BW2 = rgb2gray(img2); size(BW2); figure(1); imagesc(BW1); colormap gray figure(2); imagesc(BW2); colormap gray filtx = [1 1 1; 0 0 0; -1 -1 -1]; %[-1; 0; 1]; filty = [-1 0 1; -1 0 1; -1 0 1]; %[-1 0 1]; % convolve img1 with filter Gx1 = conv2(BW1,filtx,'same'); Gy1 = conv2(BW1,filty,'same'); figure(1); imagesc(abs(Gx1)) colormap gray figure(2); imagesc(abs(Gy1)) colormap gray % convolve img2 with filter Gx2 = conv2(BW2,filtx,'same'); Gy2 = conv2(BW2,filty,'same'); figure(3); imagesc(abs(Gx2)) colormap gray figure(4); imagesc(abs(Gy2)) colormap gray % edge map edges1 = abs(Gx1+Gy1); edges2 = abs(Gx2+Gy2); figure(1); imagesc(edges1); figure(2); imagesc(edges2); imagesc(edges1+edges2); % edges close but not completely aligned. % allow for some variation in position by using a spatial grid % spatial grid of edge energy edgeenergy1 = zeros(ceil(size(edges1,1)/50),ceil(size(edges1,2)/50)); edgeenergy2 = zeros(ceil(size(edges2,1)/50),ceil(size(edges2,2)/50)); % break image up into 50x50 pixel regions icount=0; jcount=0; for i=1:50:size(edges1,1) icount=icount+1; stopi = min(i+49,size(edges1,1)); jcount=0; for j=1:50:size(edges1,2) jcount=jcount+1; % calculate average color in each of the regions stopj = min(j+49,size(edges1,2)); edgeenergy1(icount,jcount) = sum(sum(edges1(i:stopi,j:stopj))); edgeenergy2(icount,jcount) = sum(sum(edges2(i:stopi,j:stopj))); end end edgeenergy1 = edgeenergy1/sum(edgeenergy1(:)); edgeenergy2 = edgeenergy2/sum(edgeenergy2(:)); close all imagesc(edgeenergy1); figure; imagesc(edgeenergy2); sim = sum(sum(abs(edgeenergy1-edgeenergy2)))/(size(edgeenergy1,1)*size(edgeenergy2,2));