function myImage=seamcarving(myImage,numSeams) function CostFunct=computeCostFunction(myImage) CostFunct=zeros(size(myImage,1),size(myImage,2)); for x=1:size(myImage,3) % Changing this to a -2 makes for interesting habits weightMatrix = [1 0 -1; 1 0 -1; 1 0 -1;]; CostFunct=CostFunct+abs(filter2(weightMatrix, myImage(:,:,x))); end end [myImage,map]=imread('retargetme_2.jpg'); numSeams=200; myImage=im2double(myImage); demo = nargout ==0; if demo final=image(myImage); origImage=myImage; end % for the number of seams to be removed for seamCount=1:numSeams % Figure the cost function on the matrix CostFunct=computeCostFunction(myImage); %find shortest path in G cfImage=CostFunct; %for each pixel for x = 2:size(cfImage,1) tempIM = cfImage(x-1,:); tempIM2 = tempIM(1:end-1) <= tempIM(2:end); tempIM2 = tempIM(2:end) <= tempIM(1:end-1); cfImage(x,:) = cfImage(x,:)+tempIM; end %Scoot the image inwards when removing line map=zeros(size(cfImage,1),1); [minx,map(end)] = min(cfImage(end,:)); locMinX=find(cfImage(end,:) == minx); map(end)=locMinX(ceil(rand*length(locMinX))); % Find the min x and y and store the seam line for x=size(cfImage,1)-1:-1:1 [minx,miny]=min(cfImage(x,max(map(x+1)-1,1):min(map(x+1)+1,end))); map(x)=miny+map(x+1)-1-(map(x+1)>=1); myImage(x,map(x),:)=bitand(x,1); end %remove the seam from the image for x=1:size(myImage,1) myImage(x,map(x):end-1,:)=myImage(x,map(x)+1:end,:); end myImage(:,end,:)=[]; if demo set(final,'CDATA',myImage); drawnow; end end if demo set(final,'CDATA',[myImage origImage]) end if nargout==0 clear myImage end end