├── ProposedAlgo.pdf ├── ShowAllFrames.pdf ├── foreman_10frames.zip ├── compareofFull16_24.pdf ├── CompareOfAllSearch24.pdf ├── FullSearch_10frames.pdf ├── LogarithmicSearch_10frames.pdf ├── ShowAllFrames.m ├── README.md ├── full__srch_allFrames.m ├── log_srch_allFrames.m └── CompareOfAllSearch.m /ProposedAlgo.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AK1194/Video-Compression-motion-estimation-block-video-encoder/HEAD/ProposedAlgo.pdf -------------------------------------------------------------------------------- /ShowAllFrames.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AK1194/Video-Compression-motion-estimation-block-video-encoder/HEAD/ShowAllFrames.pdf -------------------------------------------------------------------------------- /foreman_10frames.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AK1194/Video-Compression-motion-estimation-block-video-encoder/HEAD/foreman_10frames.zip -------------------------------------------------------------------------------- /compareofFull16_24.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AK1194/Video-Compression-motion-estimation-block-video-encoder/HEAD/compareofFull16_24.pdf -------------------------------------------------------------------------------- /CompareOfAllSearch24.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AK1194/Video-Compression-motion-estimation-block-video-encoder/HEAD/CompareOfAllSearch24.pdf -------------------------------------------------------------------------------- /FullSearch_10frames.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AK1194/Video-Compression-motion-estimation-block-video-encoder/HEAD/FullSearch_10frames.pdf -------------------------------------------------------------------------------- /LogarithmicSearch_10frames.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AK1194/Video-Compression-motion-estimation-block-video-encoder/HEAD/LogarithmicSearch_10frames.pdf -------------------------------------------------------------------------------- /ShowAllFrames.m: -------------------------------------------------------------------------------- 1 | clc 2 | clear all 3 | close all 4 | srcFiles = dir('E:\foreman_10frames\*.pgm'); % the folder in which ur images exists 5 | 6 | for j = 1 : length(srcFiles) 7 | filename = strcat('E:\foreman_10frames\',srcFiles(j).name); 8 | Im = imread(filename); 9 | figure, imshow(Im); 10 | end -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Video-Compression-motion-estimation-block-video-encoder 2 | This project is about the motion estimation block of a video encoder in video compression. The aim of this project is to find an optimum motion estimation algorithm which has a fair trade off between accuracy of frame estimation and the speed. 3 | I have implemented Full Search algorithm (FS), logarithmic search algorithm (LS) in MATLAB using 10 frames of Foreman. 4 | 5 | The sample video (10frames) is also uploaded here in the zip file. 6 | -------------------------------------------------------------------------------- /full__srch_allFrames.m: -------------------------------------------------------------------------------- 1 | clc 2 | clear all 3 | close all 4 | tstart=cputime; 5 | 6 | f_ref(1:300,1:300)=0; 7 | Im=imread('E:\foreman_10frames\f001.pgm'); 8 | f_ref(9:288,9:288)= Im(9:288,9:288); 9 | srcFiles = dir('E:\foreman_10frames\*.pgm'); 10 | 11 | f_p=zeros(300,3000); 12 | X=zeros(35,350); 13 | Y=zeros(35,350); 14 | SAD=zeros(1,9); 15 | for frameNo=1:9 16 | filename = strcat('E:\foreman_10frames\',srcFiles(frameNo+1).name); 17 | f_2(1:300,1:300)=0; 18 | Im2= imread(filename); 19 | f_2(9:288,9:288)= Im2(9:288,9:288); 20 | 21 | s=1; 22 | X_motion=zeros(35); 23 | Y_motion=zeros(35); 24 | f_pre(1:300,1:300)=0; 25 | for i=9:8:288 26 | t=1; 27 | for j=9:8:288 28 | 29 | img_abs=zeros(8,8); 30 | img_16=f_ref(i-4:i+7+4,j-4:j+7+4); 31 | img_8=f_2(i:i+7,j:j+7); 32 | 33 | for p=1:8 34 | for q=1:8 35 | 36 | img_abs(p,q)=sum(sum((img_16(p:p+7,q:q+7)- img_8).^2)); 37 | end 38 | end 39 | 40 | [M,I] = min(img_abs(:)); 41 | [row_cor, col_cor] = ind2sub(size(img_abs),I); 42 | 43 | 44 | f_pre(i:i+7,j:j+7)=img_16(row_cor:row_cor+7,col_cor:col_cor+7); 45 | 46 | X_motion(s,t)= row_cor -5; 47 | Y_motion(s,t)= col_cor -5; 48 | t=t+1; 49 | end 50 | 51 | s=s+1; 52 | end 53 | f_p(1:300, 1+(300*frameNo):300*(frameNo+1))=f_pre; 54 | X(1:35, 1+(35*(frameNo-1)):35*frameNo)=X_motion; 55 | Y(1:35, 1+(35*(frameNo-1)):35*frameNo)=Y_motion; 56 | 57 | residu1=abs(f_2-f_pre); 58 | SAD(frameNo)=(sum(sum((residu1).^2)))/90000; 59 | figure,imshow(uint8(residu1)); 60 | title('reduced residue after Full Search Operation'); 61 | figure,imshowpair(f_2,f_ref,'diff'); 62 | title('actual residue or difference between frames'); 63 | f_ref=f_2; 64 | end 65 | telapsed=cputime-tstart; 66 | Frame=[1 2 3 4 5 6 7 8 9]; 67 | figure,fullSearch=plot(Frame,SAD); 68 | title('Sum of Absolute Difference [SAD] Vs Frames Plot'); 69 | ylabel('SAD found in Full Search'); 70 | xlabel('Frame number'); 71 | display('time elapsed in search'); 72 | display(telapsed); -------------------------------------------------------------------------------- /log_srch_allFrames.m: -------------------------------------------------------------------------------- 1 | clc 2 | clear all 3 | close all 4 | tstart=cputime; 5 | f_ref(1:300,1:300)=0; 6 | Im=imread('E:\foreman_10frames\f001.pgm'); 7 | f_ref(9:288,9:288)= Im(9:288,9:288); 8 | 9 | srcFiles = dir('E:\foreman_10frames\*.pgm'); 10 | 11 | f_p=zeros(300,3000); 12 | 13 | X=zeros(35,350); 14 | Y=zeros(35,350); 15 | SAD=zeros(1,9); 16 | for frameNo=1:9 17 | filename = strcat('E:\foreman_10frames\',srcFiles(frameNo+1).name); 18 | f_2(1:300,1:300)=0; 19 | Im2= imread(filename); 20 | f_2(9:288,9:288)= Im2(9:288,9:288); 21 | f_pre(1:300,1:300)=0; 22 | 23 | s=1; 24 | X_motion= zeros(22,22); 25 | Y_motion=zeros(22,22); 26 | for i=9:8:288 27 | t=1; 28 | for j=9:8:288 29 | img_abs=[0 0 0 0 0]; 30 | img_16=f_ref(i-4:i+7+4,j-4:j+7+4); 31 | img_8=f_2(i:i+7,j:j+7); 32 | flag = 1; 33 | I=1; 34 | Rc=5; 35 | Cc=5; 36 | step_size=4; 37 | 38 | while flag 39 | r=[Rc,Rc-step_size,Rc,Rc,Rc+step_size]; 40 | c=[Cc,Cc,Cc-step_size,Cc+step_size,Cc]; 41 | 42 | for g=1:5 43 | if r(g)<=0 || r(g)>=10 44 | img_abs(g)=255*64*255; 45 | elseif c(g)<=0 || c(g)>=10 46 | img_abs(g)=255*64*255; 47 | elseif g==I && g-1>0 48 | img_abs(g)=255*64*255; 49 | else 50 | img_abs(g)=sum(sum((img_16(r(g):r(g)+7,c(g):c(g)+7)- img_8).^2)); 51 | end 52 | end 53 | 54 | 55 | [M,I] = min(img_abs); 56 | switch (I) 57 | case 1 58 | step_size=step_size/2; 59 | case 2 60 | Rc=Rc-step_size; 61 | case 3 62 | Cc=Cc-step_size; 63 | case 4 64 | Cc=Cc+step_size; 65 | case 5 66 | Rc=Rc+step_size; 67 | end 68 | if step_size<1 69 | flag=0; 70 | break; 71 | else 72 | continue; 73 | end 74 | end 75 | f_pre(i:i+7,j:j+7)=img_16(Rc:Rc+7,Cc:Cc+7); 76 | X_motion(s,t)= Rc-5; 77 | Y_motion(s,t)= Cc-5; 78 | 79 | t=t+1; 80 | end 81 | s=s+1; 82 | 83 | end 84 | f_p(1:300, 1+(300*frameNo):300*(frameNo+1))=f_pre; 85 | X(1:35, 1+(35*(frameNo-1)):35*frameNo)=X_motion; 86 | Y(1:35, 1+(35*(frameNo-1)):35*frameNo)=Y_motion; 87 | 88 | 89 | residu=abs(f_2-f_pre); 90 | SAD(frameNo)=(sum(sum((residu).^2)))/90000; 91 | figure,imshow(uint8(residu)); 92 | title('reduced residue after Logarithmic Search Operation'); 93 | figure,imshowpair(f_2,f_ref,'diff'); 94 | title('actual residue or difference between frames'); 95 | 96 | f_ref=f_2; 97 | end 98 | telapsed=cputime-tstart; 99 | Frame=[1 2 3 4 5 6 7 8 9]; 100 | figure,LogSearch=plot(Frame,SAD); 101 | title('Sum of Absolute Difference [SAD] Vs Frames Plot'); 102 | ylabel('SAD found in Logarithmic Search'); 103 | xlabel('Frame number'); 104 | display('time elapsed in search'); 105 | display(telapsed); 106 | -------------------------------------------------------------------------------- /CompareOfAllSearch.m: -------------------------------------------------------------------------------- 1 | %% Full Search 2 | clc 3 | clear all 4 | close all 5 | Frame=[1 2 3 4 5 6 7 8 9]; 6 | f_ref(1:300,1:300)=0; 7 | Im=imread('E:\foreman_10frames\f001.pgm'); 8 | f_ref(9:288,9:288)= Im(9:288,9:288); 9 | srcFiles = dir('E:\foreman_10frames\*.pgm'); 10 | 11 | f_p=zeros(300,3000); 12 | X=zeros(35,350); 13 | Y=zeros(35,350); 14 | SAD_Full=zeros(1,9); 15 | tstart_full=cputime; 16 | for frameNo=1:9 17 | filename = strcat('E:\foreman_10frames\',srcFiles(frameNo+1).name); 18 | f_2(1:300,1:300)=0; 19 | Im2= imread(filename); 20 | f_2(9:288,9:288)= Im2(9:288,9:288); 21 | 22 | s=1; 23 | X_motion=zeros(35); 24 | Y_motion=zeros(35); 25 | f_pre(1:300,1:300)=0; 26 | for i=9:8:288 27 | t=1; 28 | for j=9:8:288 29 | 30 | img_abs=zeros(8,8); 31 | img_16=f_ref(i-4:i+7+4,j-4:j+7+4); 32 | img_8=f_2(i:i+7,j:j+7); 33 | 34 | for p=1:8 35 | for q=1:8 36 | 37 | img_abs(p,q)=sum(sum((img_16(p:p+7,q:q+7)- img_8).^2)); 38 | end 39 | end 40 | 41 | [M,I] = min(img_abs(:)); 42 | [row_cor, col_cor] = ind2sub(size(img_abs),I); 43 | 44 | 45 | f_pre(i:i+7,j:j+7)=img_16(row_cor:row_cor+7,col_cor:col_cor+7); 46 | 47 | X_motion(s,t)= row_cor -5; 48 | Y_motion(s,t)= col_cor -5; 49 | t=t+1; 50 | end 51 | 52 | s=s+1; 53 | end 54 | f_p(1:300, 1+(300*frameNo):300*(frameNo+1))=f_pre; 55 | X(1:35, 1+(35*(frameNo-1)):35*frameNo)=X_motion; 56 | Y(1:35, 1+(35*(frameNo-1)):35*frameNo)=Y_motion; 57 | 58 | residu1=abs(f_2-f_pre); 59 | SAD_Full(frameNo)=(sum(sum((residu1).^2)))/90000; 60 | figure,imshow(uint8(residu1)); 61 | title('reduced residue after Full Search Operation'); 62 | figure,imshowpair(f_2,f_ref,'diff'); 63 | title('actual residue or difference between frames'); 64 | f_ref=f_2; 65 | end 66 | telapsed_full=cputime-tstart_full; 67 | figure,fullSearch=plot(Frame,SAD_Full); 68 | title('Sum of Absolute Difference [SAD] Vs Frames Plot'); 69 | ylabel('SAD found in Full Search'); 70 | xlabel('Frame number'); 71 | 72 | %% Logarithmic Search 73 | 74 | 75 | 76 | f_ref(1:300,1:300)=0; 77 | Im=imread('E:\foreman_10frames\f001.pgm'); 78 | f_ref(9:288,9:288)= Im(9:288,9:288); 79 | f_p=zeros(300,3000); 80 | X=zeros(35,350); 81 | Y=zeros(35,350); 82 | SAD_Log=zeros(1,9); 83 | tstart_log=cputime; 84 | for frameNo=1:9 85 | filename = strcat('E:\foreman_10frames\',srcFiles(frameNo+1).name); 86 | f_2(1:300,1:300)=0; 87 | Im2= imread(filename); 88 | f_2(9:288,9:288)= Im2(9:288,9:288); 89 | f_pre(1:300,1:300)=0; 90 | 91 | s=1; 92 | X_motion= zeros(22,22); 93 | Y_motion=zeros(22,22); 94 | for i=9:8:288 95 | t=1; 96 | for j=9:8:288 97 | img_abs=[0 0 0 0 0]; 98 | img_16=f_ref(i-4:i+7+4,j-4:j+7+4); 99 | img_8=f_2(i:i+7,j:j+7); 100 | flag = 1; 101 | I=1; 102 | Rc=5; 103 | Cc=5; 104 | step_size=4; 105 | 106 | while flag 107 | r=[Rc,Rc-step_size,Rc,Rc,Rc+step_size]; 108 | c=[Cc,Cc,Cc-step_size,Cc+step_size,Cc]; 109 | 110 | for g=1:5 111 | if r(g)<=0 || r(g)>=10 112 | img_abs(g)=255*64*255; 113 | elseif c(g)<=0 || c(g)>=10 114 | img_abs(g)=255*64*255; 115 | elseif g==I && g-1>0 116 | img_abs(g)=255*64*255; 117 | else 118 | img_abs(g)=sum(sum((img_16(r(g):r(g)+7,c(g):c(g)+7)- img_8).^2)); 119 | end 120 | end 121 | 122 | 123 | [M,I] = min(img_abs); 124 | switch (I) 125 | case 1 126 | step_size=step_size/2; 127 | case 2 128 | Rc=Rc-step_size; 129 | case 3 130 | Cc=Cc-step_size; 131 | case 4 132 | Cc=Cc+step_size; 133 | case 5 134 | Rc=Rc+step_size; 135 | end 136 | if step_size<1 137 | flag=0; 138 | break; 139 | else 140 | continue; 141 | end 142 | end 143 | f_pre(i:i+7,j:j+7)=img_16(Rc:Rc+7,Cc:Cc+7); 144 | X_motion(s,t)= Rc-5; 145 | Y_motion(s,t)= Cc-5; 146 | 147 | t=t+1; 148 | end 149 | s=s+1; 150 | 151 | end 152 | f_p(1:300, 1+(300*frameNo):300*(frameNo+1))=f_pre; 153 | X(1:35, 1+(35*(frameNo-1)):35*frameNo)=X_motion; 154 | Y(1:35, 1+(35*(frameNo-1)):35*frameNo)=Y_motion; 155 | 156 | 157 | residu=abs(f_2-f_pre); 158 | SAD_Log(frameNo)=(sum(sum((residu).^2)))/90000; 159 | figure,imshow(uint8(residu)); 160 | title('reduced residue after Logarithmic Search Operation'); 161 | figure,imshowpair(f_2,f_ref,'diff'); 162 | title('actual residue or difference between frames'); 163 | 164 | f_ref=f_2; 165 | end 166 | telapsed_log=cputime-tstart_log; 167 | figure,LogSearch=plot(Frame,SAD_Log); 168 | title('Sum of Absolute Difference [SAD] Vs Frames Plot'); 169 | ylabel('SAD found in Logarithmic Search'); 170 | xlabel('Frame number'); 171 | 172 | %% Full and Logarithmic Search 173 | 174 | 175 | f_ref(1:300,1:300)=0; 176 | Im=imread('E:\foreman_10frames\f001.pgm'); 177 | f_ref(9:288,9:288)= Im(9:288,9:288); 178 | 179 | f_p=zeros(300,3000); 180 | X=zeros(35,350); 181 | Y=zeros(35,350); 182 | fg=1; 183 | change=0; 184 | SAD_fullLog=zeros(1,9); 185 | tstart_fullLog=cputime; 186 | for frameNo=1:9 187 | filename = strcat('E:\foreman_10frames\',srcFiles(frameNo+1).name); 188 | f_2(1:300,1:300)=0; 189 | Im2= imread(filename); 190 | f_2(9:288,9:288)= Im2(9:288,9:288); 191 | 192 | X_motion=zeros(35); 193 | Y_motion=zeros(35); 194 | 195 | f_pre(1:300,1:300)=0; 196 | 197 | if fg==1 198 | 199 | s=1; 200 | for i=9:8:288 201 | t=1; 202 | for j=9:8:288 203 | 204 | img_abs=zeros(8,8); 205 | img_16=f_ref(i-4:i+7+4,j-4:j+7+4); 206 | img_8=f_2(i:i+7,j:j+7); 207 | 208 | for p=1:8 209 | for q=1:8 210 | 211 | img_abs(p,q)=sum(sum((img_16(p:p+7,q:q+7)- img_8).^2)); 212 | end 213 | end 214 | 215 | [M,I] = min(img_abs(:)); 216 | [row_cor, col_cor] = ind2sub(size(img_abs),I); 217 | 218 | 219 | f_pre(i:i+7,j:j+7)=img_16(row_cor:row_cor+7,col_cor:col_cor+7); 220 | 221 | X_motion(s,t)= row_cor -5; 222 | Y_motion(s,t)= col_cor -5; 223 | t=t+1; 224 | end 225 | s=s+1; 226 | end 227 | 228 | elseif fg==0 229 | s=1; 230 | X_motion= zeros(22,22); 231 | Y_motion=zeros(22,22); 232 | for i=9:8:288 233 | t=1; 234 | for j=9:8:288 235 | img_abs=[0 0 0 0 0]; 236 | img_16=f_ref(i-4:i+7+4,j-4:j+7+4); 237 | img_8=f_2(i:i+7,j:j+7); 238 | flag = 1; 239 | I=1; 240 | Rc=5; 241 | Cc=5; 242 | step_size=4; 243 | 244 | while flag 245 | r=[Rc,Rc-step_size,Rc,Rc,Rc+step_size]; 246 | c=[Cc,Cc,Cc-step_size,Cc+step_size,Cc]; 247 | 248 | for g=1:5 249 | if r(g)<=0 || r(g)>=10 250 | img_abs(g)=255*64*255; 251 | elseif c(g)<=0 || c(g)>=10 252 | img_abs(g)=255*64*255; 253 | elseif g==I && g-1>0 254 | img_abs(g)=255*64*255; 255 | else 256 | img_abs(g)=sum(sum((img_16(r(g):r(g)+7,c(g):c(g)+7)- img_8).^2)); 257 | end 258 | end 259 | 260 | 261 | [M,I] = min(img_abs); 262 | switch (I) 263 | case 1 264 | step_size=step_size/2; 265 | case 2 266 | Rc=Rc-step_size; 267 | case 3 268 | Cc=Cc-step_size; 269 | case 4 270 | Cc=Cc+step_size; 271 | case 5 272 | Rc=Rc+step_size; 273 | end 274 | if step_size<1 275 | flag=0; 276 | break; 277 | else 278 | continue; 279 | end 280 | end 281 | f_pre(i:i+7,j:j+7)=img_16(Rc:Rc+7,Cc:Cc+7); 282 | X_motion(s,t)= Rc-5; 283 | Y_motion(s,t)= Cc-5; 284 | 285 | t=t+1; 286 | end 287 | s=s+1; 288 | end 289 | 290 | end 291 | 292 | 293 | change=change+1; 294 | if change<3 295 | fg=0; 296 | elseif change==3 297 | change=0; 298 | fg=1; 299 | end 300 | 301 | f_p(1:300, 1+(300*frameNo):300*(frameNo+1))=f_pre; 302 | X(1:35, 1+(35*(frameNo-1)):35*frameNo)=X_motion; 303 | Y(1:35, 1+(35*(frameNo-1)):35*frameNo)=Y_motion; 304 | 305 | residu1=abs(f_2-f_pre); 306 | SAD_fullLog(frameNo)=(sum(sum((residu1).^2)))/90000; 307 | figure,imshow(uint8(residu1)); 308 | title('reduced residue after the Search Operation'); 309 | figure,imshowpair(f_2,f_ref,'diff'); 310 | title('actual residue or difference between frames'); 311 | f_ref=f_2; 312 | end 313 | telapsed_fullLog=cputime-tstart_fullLog; 314 | figure,FullLogSearch=plot(Frame,SAD_fullLog); 315 | title('Sum of Absolute Difference [SAD] Vs Frames Plot'); 316 | ylabel('SAD found in Hybrid of Full Search and Logarithmic Search'); 317 | xlabel('Frame number'); 318 | 319 | 320 | %% Comparision Among these Search Algorithm 321 | 322 | figure,plot(Frame,SAD_Full,'r-.o',Frame,SAD_fullLog,'k-*',Frame,SAD_Log,'g--s'); 323 | title('Sum of Absolute Difference [SAD] Vs Frames Plot'); 324 | ylabel('SAD found using Different Search Algorithms'); 325 | xlabel('Frame number'); 326 | 327 | timeelapsed=[19.2969,10.7813, 13.3281]; 328 | % Search=['full_Search','Log_Search', 'FullLog_Search']; 329 | figure,bar(timeelapsed); 330 | xlabel('fullSearch LogSearch FullLogSearch'); 331 | ylabel('time elapsed'); 332 | --------------------------------------------------------------------------------