Wednesday, June 18, 2008

Mosaic

Original.



So I finally decided to start on the mosaic today but after looking through thousands of pictures I have taken myself, I realized that none of them would look good at the resolution we are using. So given that cartoons have solid colors and simple shapes, and the fact that I do enjoy watching them sometimes, I chose to create a mosaic of cartoons. I went to google images and did a search for cartoons. Then I selected about forty or so. My criteria was that they had to be very simple and have a limited amount of colors, and be a chararter I recognized. After shrinking them to a 30 by 30 size, I eliminated some of them because they became unrecognizable at that resolution. For the main image I chose a picture of Cartman because I figured even at low resolution he is pretty recognizable. I did not bother trying to pick pictures with average colors resembling the 27 colors we have, I just tried to pick enough different colors for the pictures. Since the shapes are so simple, even if the colors do not match well, it will still be very recognizable.

To match the colors I first tried to use the minimum absolute difference between the pixel color and the average color of each image, but the images I got were not as good as I wanted.

function [ inpic ] = mosaic( inpic,A )

for i=1:27
av(1,i,:) = sum(sum(A{i}))/900;
end


for k = 1:size(inpic,1)/30
for l=1:size(inpic,2)/30
pixel = inpic(30*k,30*l,:);
b=200000;
place = 1;
for i=1:27
dif = sum(abs(av(1,i,:)-pixel));
% ds = abs(av(1,i,:)-pixel);
% dif = ds(:,:,1)^2+ds(:,:,2)^2+ds(:,:,3)^2;
if (dif b = dif;
place = i;
end
end

inpic(30*k-29:30*k,30*l-29:30*l,:)=A{place};
end
end
end












Then I thought about standard deviation, and how we need to get the sum of the squared differences. I have always wondered about why not just get the sum of absolute differences, and I think this is a nice visual example of why this works better. Because having one of the three colors off significantly is worse than having 3 colors off a little, and same with points in a data set. I thought this relationship between Statistics and matching colors is pretty cool. So I used:







function [ inpic ] = mosaic( inpic,A )
for i=1:27
av(1,i,:) = sum(sum(A{i}))/900;
end
for k = 1:size(inpic,1)/30
for l=1:size(inpic,2)/30
pixel = inpic(30*k,30*l,:);
b=200000;
place = 1;
for i=1:27
%dif = sum(abs(av(1,i,:)-pixel));
ds = abs(av(1,i,:)-pixel);
dif = ds(:,:,1)^2+ds(:,:,2)^2+ds(:,:,3)^2;
if (dif less than b)
b = dif;
place = i;
end
end
inpic(30*k-29:30*k,30*l-29:30*l,:)=A{place};
end
end
end











I also like this method of choosing colors, because it is easily expandable to 64 and more colors. All I have to do is keep on adding images into the array, I could theoretically add as many as I want and it will do the matching for me.

I have to go to class now, will add the code later.

No comments: