www.slide4math.com

 

This is my version of explanation. I would suggest you to come up with your own explanation. The best way would be for you to try explain this to somebody else in your own words.

 

Following is my version of explanation, but this is just an example. You may come up with a better version.

 

 

 

2D Convolution - Left Edge

 

Convolution would be the one of the most important operations in engineering math. If you don't have any prior study and understanding on how Convolution works, it would not be easy to make sense out of it. For those who is not familiar with Convolution, I would suggest you to go through my descriptive note here.

 

The example in this page is to show how the convolution works for 2D data(i.e, 2D array data like an image). This 2D convolution has long been used for the purpose of various image processing (especially for edge detection), but it has become very popular and important concept since it is used by a famous neural network/machine learning algorithm called CNN.

 

As I always says, the best way to understand this kind of comlicated mathematical concept is to get a very simple example and answer key and then do it yourself with pen and paper. To help you to easily check if your calculation is correct or wrong, I used in this example a 2D data which is made up of only 1 or 0.

 

 

 

 

Followings are the code that I wrote in Octave to creates all the plots shown in this page. You may copy these code and play with these codes. Change variables and try yourself until you get your own intuitive understanding.

 

< Code 1 >

 

img = [0 0 0 0 0 0 0 0 0 0; ...

       0 0 0 0 1 1 1 0 0 0; ...

       0 0 0 1 1 1 1 0 0 0; ...

       0 0 1 1 1 1 1 0 0 0; ...

       0 0 0 0 1 1 1 0 0 0; ...

       0 0 0 0 1 1 1 0 0 0; ...

       0 0 0 0 1 1 1 0 0 0; ...

       0 1 1 1 1 1 1 1 1 0; ...

       0 1 1 1 1 1 1 1 1 0; ...

       0 0 0 0 0 0 0 0 0 0];       

       

flt = [-1 0 1; ...

      -2 0 2; ...

      -1 0 1];

      

hFig = figure(1,'Position',[300 300 1020 300]);

 

cx = 7;

cy = 7;

 

%--------------------------------------------------------------------

% Display Image

subplot(1,4,1);     

 

line([0 0],[13 14],'color','black');

line([0 1],[14 14],'color','black');

 

tx = 0;

ty = 0;

rdx = 1;

rdy = 1;

     

for j = 1:10

  for i = 1:10

     rx = tx + i*rdx;

     ry = ty + j*rdy;     

     if img(10-j+1,i) > 0

        c = 0;

        tStr = '1';

     else

        c = 1;

        tStr = '0';

     end  

     rectangle('Position',[rx ry rdx rdy],'FaceColor',[c c c]);

     if c == 1

        text(rx+0.3,ry+0.5,tStr,'color','black');

     else

        text(rx+0.3,ry+0.5,tStr,'color','yellow');

     end   

  end

end  

 

rectangle('Position',[cx+1 10-cy-1-1 3 3],'EdgeColor','red');

 

text(5,12,'image');     

axis([0 12 -4 16]);    

axis off;

 

%--------------------------------------------------------------------

% Display Filter

subplot(1,4,2);     

 

tx = 0;

ty = 0;

rdx = 1;

rdy = 1;

     

for j = 1:10

  for i = 1:10

     rx = tx + i*rdx;

     ry = ty + j*rdy;     

     c = 1;

     rectangle('Position',[rx ry rdx rdy],'FaceColor',[c c c]);  

  end

end  

 

tx = 0;

ty = 11;

rdx = 1;

rdy = 1;

 

     

for j = 1:3

  for i = 1:3

     rx = cx + tx + i*rdx;

     ry = -cy + ty - j*rdy;     

     c = 1;

     tStr = sprintf("%d",flt(j,i));

     rectangle('Position',[rx ry rdx rdy],'FaceColor',[c c c]);  

     if flt(j,i) < 0

       text(rx+0.0,ry+0.5,tStr,'color','black');

     else  

       text(rx+0.2,ry+0.5,tStr,'color','black');

     end   

  end

end  

 

rectangle('Position',[cx+1 10-cy-1-1 3 3],'EdgeColor','red');

 

text(5,12,'filter');

     

axis([0 12 -4 16]);    

axis off;

 

%--------------------------------------------------------------------

% Display Image x Filter

subplot(1,4,3);     

 

tx = 0;

ty = 0;

rdx = 1;

rdy = 1;

 

for j = 1:10

  for i = 1:10

     rx = tx + i*rdx;

     ry = ty + j*rdy;     

     c = 1;

     rectangle('Position',[rx ry rdx rdy],'FaceColor',[c c c]);  

  end

end

 

tx = 0;

ty = 11;

rdx = 1;

rdy = 1;

 

mFlt = [0 0 0;...

        0 0 0;

        0 0 0];

 

for j = 1:3

  for i = 1:3

      mFlt(j,i) = img(j+cy,i+cx) * flt(j,i);

  end

end

  

for j = 1:3

  for i = 1:3

     rx = cx + tx + i*rdx;

     ry = -cy + ty - j*rdy;    

     c = 1;

     tStr = sprintf("%d",mFlt(j,i));

     rectangle('Position',[rx ry rdx rdy],'FaceColor',[c c c]);  

     if mFlt(j,i) < 0

       text(rx+0.0,ry+0.5,tStr,'color','black');

     else  

       text(rx+0.2,ry+0.5,tStr,'color','black');

     end   

  end

end  

 

rectangle('Position',[cx+1 10-cy-1-1 3 3],'EdgeColor','red');

     

text(3,12,'image x filter');

     

axis([0 12 -4 16]);   

axis off;

 

%--------------------------------------------------------------------

% Display Image * Filter

subplot(1,4,4);    

tx = 0;

ty = 0;

rdx = 1;

rdy = 1;

sxMax = 0

     

for j = 1:10

  for i = 1:10

     rx = tx + i*rdx;

     ry = ty + j*rdy;     

     c = 1;

     rectangle('Position',[rx ry rdx rdy],'FaceColor',[c c c]);  

     %text(rx+0.2,ry+0.5,tStr,'color',[1 0 0]);

  end

end  

 

for sy = 0:cy

    if sy < cy

       sxMax = 7;

    else

       sxMax = cx;

    end   

  for sx = 0:sxMax

    mFlt = [0 0 0;...

            0 0 0;

            0 0 0];

    

    for j = 1:3

      for i = 1:3

          mFlt(j,i) = img(j+sy,i+sx) * flt(j,i);

      end

    end

    

    

    s = sum(reshape(mFlt,[],1));

    tStr = sprintf("%d",s);

    

    c = 0.5+(s/10);

    if s == 0

       rectangle('Position',[sx+1+1 10-sy-1 rdx rdy],'FaceColor',[0.1 0.1 0.1]);

    else  

       rectangle('Position',[sx+1+1 10-sy-1 rdx rdy],'FaceColor',[c 0 0.5-0.5*c]);

    end    

    

    if s < 0

       text(sx+1+1+0.0,10-sy-1+0.5,tStr,'color','yellow');

    else  

       text(sx+1+1+0.2,10-sy-1+0.5,tStr,'color','yellow');

    end

  end

end

 

rectangle('Position',[cx+1+1 10-cy-1 1 1],'EdgeColor','red');

 

text(3,12,'image * filter');

 

line([12 12],[-2 -1],'color','black');

line([11 12],[-2 -2],'color','black');

 

axis([0 12 -4 16]);     

axis off;