Seam carving is an algorithm for resizing images while keeping the most prominent and conspicuous pixels in the image. The important pixels in an image are usually those who are located over horizontal or vertical edges, so to throw away some pixels we first find horizontal and vertical edges and store their magnitude as pixel energy. Then we use dynamic programming to find a path which contains the minimum energy level and we drop those pixels. We iteratively do this until we got our image in the desired size.
our image is 720×480,
let’s drop some rows and resize it to 640×480:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
image=imread('images/Valve.PNG'); image=im2double(image); new_resolution_rows=480; new_resolution_cols=640; [original_resolution_rows, original_resolution_colsclc]=size(image); figure('Name','original'), imshow(image,[]); %this will remove the columns Valve640x480 = MySeamCarving(image,new_resolution_rows,new_resolution_cols); size(Valve640x480) figure('Name','Valve640x480'), imshow(Valve640x480,[]); imwrite(Valve640x480,'images/Valve640x480.jpg'); |
we can drop some columns and resize it to 720×320:
1 2 3 4 5 6 7 8 9 10 11 12 |
new_resolution_cols=720; new_resolution_rows=320; %this will remove the rows image=permute(image,[2 1 3]); Valve720x320= MySeamCarving(image,new_resolution_cols,new_resolution_rows); Valve720x320=permute(Valve720x320,[2 1 3]); figure('Name','Valve720x320'), imshow(Valve720x320,[]); imwrite(Valve720x320,'images/Valve720x320.jpg'); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
%%%%%%%%%%%%%%%%%%%%%%%% functionscore_matrix %%%%%%%%%%%%%%%%%%%%% function score_matrix=ScoreMatrix(red_channel,green_channel,blue_channel) x_derivative=[1 0 -1]; y_derivative=x_derivative'; GX_red_channel=conv2(red_channel,x_derivative,'same'); GY_red_channel=conv2(red_channel,y_derivative,'same'); red_E=sqrt(GX_red_channel.^2+ GY_red_channel.^2); GX_blue_channel=conv2(blue_channel,x_derivative,'same'); GY_blue_channel=conv2(blue_channel,y_derivative,'same'); blue_E=sqrt(GX_blue_channel.^2+ GY_blue_channel.^2); GX_green_channel=conv2(green_channel,x_derivative,'same'); GY_green_channel=conv2(green_channel,y_derivative,'same'); green_E=sqrt(GX_green_channel.^2+ GY_green_channel.^2); score_matrix=im2double(green_E+green_E+green_E); %%%%%%%%%%%%%%%%%%%%%%%% function resized_image %%%%%%%%%%%%%%%%%%%%% function resized_image=MySeamCarving(image,new_resolution_rows,new_resolution_cols) %Extracting red channel red_channel = image(:,:,1); %extracting green channel green_channel = image(:,:,2); %extracting blue channel blue_channel = image(:,:,3); % scoe_matrix=[ 0 5 1 ;6 8 4 ; 9 1 7 ]; %first we reduce the columns, the we remove rows [original_resolution_rows ,original_resolution_cols]=size(image); original_resolution_cols=original_resolution_cols/3; for resolution_cols=original_resolution_cols:-1:new_resolution_cols+1 % resolution_cols=resolution_cols-1; score_matrix=ScoreMatrix(red_channel,green_channel,blue_channel); M=double(zeros(size(score_matrix))); % M=im2double(zeros(size(score_matrix))); M(1,:)=score_matrix(1,:); [rows,cols]=size(score_matrix); M=padarray(M,[1 1],inf,'both'); for x=2:rows for y=1:cols M(x+1,y+1)=score_matrix(x,y)+ min( [M(x-1+1,y-1 +1) , M(x-1+1,y+1), M(x-1+1,y+1+1) ] ); end end I=rows+1; [m,J]=min(M(I,:)); M(I,J); %this is mimimum value of the last row, indexs=[I,J]; while I>2 three_elements_from_upper_row=[M(I-1,J-1),M(I-1,J), M(I-1,J+1) ]; [minIndex_i, minIndex_j]=find(three_elements_from_upper_row == min(three_elements_from_upper_row(:))); minIndex_j=minIndex_j(1); J=J+ -(2-minIndex_j); I=I-1; indexs=[indexs;[I,J]]; end indexs=flipud(indexs); New_M=[]; new_blue_channel=[]; new_red_channel=[]; new_green_channel=[]; for i=1:size(indexs) j=indexs(i,2); % row=M(i+1,:); % row(j) = [] ; % remove % New_M=[New_M; row ]; row=blue_channel(i,:); row(j-1) = []; new_blue_channel=[new_blue_channel;row]; row=red_channel(i,:); row(j-1) = []; new_red_channel=[new_red_channel;row]; row=green_channel(i,:); row(j-1) = []; new_green_channel=[new_green_channel;row]; end green_channel=new_green_channel; red_channel=new_red_channel; blue_channel=new_blue_channel; end resized_image = cat(3, red_channel, green_channel, blue_channel); |
[…] Carving. I have written a tutorial on that here and the recursive part is in the following […]