Your task is to implement the seam carving algorithm of Avidan & Shamir. This is a very clever algorithm that was developed for resizing images in a manner that is adaptive to the image content. Typical image resizing scales the image uniformly. The Avidan & Shamir algorithm attempts to remove the boring bits of the image while retaining the important bits. The cue that is used for determining what is important is very low-level, so the algorithm does not always produce good results. It is surprising, however, how well it does work. Apart from implementing the algorithm, your job is also to evaluate its utility.
Implement a function that shrinks an image using seam removal:
function [J] = seamRemove(I,n,isVertical)
where n is the number of seams to remove. isVertical=1 if seams are vertical and otherwise 0.
Write a content removal program to remove a specific region in an image. Use matlab function roipoly to select the region. You can also protect regions that you want to keep.
Show two successful results for object removal using your code.
mov = avifile('movie.avi'); frame.colormap = ; for i = 1:nframes, frame.cdata = someImage; mov = addframe(mov,frame); end mov = close(mov);
Note that the movies that matlab creates are enormous uncompressed AVI files. It is a good idea to transcode them into something like DivX or H.264 using something like Quicktime Pro or mencoder if you want to put them in your writeup or on the web.
The following code segment finds a horizontal seam for the image montains.jpg. Use your own image and try the code to see the result. You can use it as a basis for your seam removal function.
fname = 'montains.jpg'; im = imread(fname); im = im2double(im); [H,W,D] = size(im); img = rgb2gray(im); [fx, fy] = gradient(img); cmap = abs(fx) + abs(fy); scmap = imfilter(cmap, ones(5,5)/25, 'same', 'replicate'); cmap = cmap ./ (scmap + 1); % cmap is the cost of removing an image pixel prev_node = ones(H-2,W)*2; % Backpointer ac_im = ones(H,W); % The optimal (minimum) value table ac_im(1:H,1) = cmap(1:H,1); % First column initialization ac_im(1,:) = 1000; % Force the seam to stay inside ac_im(H,:) = 1000; % Dynamic Programming for n = 2 : W s1 = cmap(2:H-1,n) + ac_im(1:H-2, n-1); s2 = cmap(2:H-1,n) + ac_im(2:H-1, n-1); s3 = cmap(2:H-1,n) + ac_im(3:H, n-1); s = [s1, s2, s3]; [t, id] = min(s, , 2); prev_node(:,n) = id; ac_im(2:H-1,n) = t; end %Convert backpointer index to shift prev_node(prev_node == 1) = -1; prev_node(prev_node == 2) = 0; prev_node(prev_node == 3) = 1; % Get the seam sline = ; [t, id] = min(ac_im(2:H-1, W)); sline = [id+1, sline]; for n = W-1 : -1 : 1 id = id + prev_node(id, n); sline = [id+1, sline]; end %Show the seam imshow(im); hold on; plot(1:W, sline, 'r', 'linewidth', 5); hold off;