How to resolve the algorithm Image convolution step by step in the BBC BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Image convolution step by step in the BBC BASIC programming language

Table of Contents

Problem Statement

One class of image digital filters is described by a rectangular matrix of real coefficients called kernel convoluted in a sliding window of image pixels. Usually the kernel is square

K

k l

{\displaystyle K_{kl}}

, where k, l are in the range -R,-R+1,..,R-1,R. W=2R+1 is the kernel width. The filter determines the new value of a grayscale image pixel Pij as a convolution of the image pixels in the window centered in i, j and the kernel values: Color images are usually split into the channels which are filtered independently. A color model can be changed as well, i.e. filtration is performed not necessarily in RGB. Common kernels sizes are 3x3 and 5x5. The complexity of filtrating grows quadratically (O(n2)) with the kernel width. Task: Write a generic convolution 3x3 kernel filter. Optionally show some end user filters that use this generic one. (You can use, to test the functions below, these input and output solutions.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Image convolution step by step in the BBC BASIC programming language

Source code in the bbc programming language

      Width% = 200
      Height% = 200
      
      DIM out&(Width%-1, Height%-1, 2)
      
      VDU 23,22,Width%;Height%;8,16,16,128
      *DISPLAY Lena
      OFF
      
      DIM filter%(2, 2)
      filter%() = -1, -1, -1, -1, 12, -1, -1, -1, -1
      
      REM Do the convolution:
      FOR Y% = 1 TO Height%-2
        FOR X% = 1 TO Width%-2
          R% = 0 : G% = 0 : B% = 0
          FOR I% = -1 TO 1
            FOR J% = -1 TO 1
              C% = TINT((X%+I%)*2, (Y%+J%)*2)
              F% = filter%(I%+1,J%+1)
              R% += F% * (C% AND &FF)
              G% += F% * (C% >> 8 AND &FF)
              B% += F% * (C% >> 16)
            NEXT
          NEXT
          IF R% < 0 R% = 0 ELSE IF R% > 1020 R% = 1020
          IF G% < 0 G% = 0 ELSE IF G% > 1020 G% = 1020
          IF B% < 0 B% = 0 ELSE IF B% > 1020 B% = 1020
          out&(X%, Y%, 0) = R% / 4 + 0.5
          out&(X%, Y%, 1) = G% / 4 + 0.5
          out&(X%, Y%, 2) = B% / 4 + 0.5
        NEXT
      NEXT Y%
      
      REM Display:
      GCOL 1
      FOR Y% = 0 TO Height%-1
        FOR X% = 0 TO Width%-1
          COLOUR 1, out&(X%,Y%,0), out&(X%,Y%,1), out&(X%,Y%,2)
          LINE X%*2,Y%*2,X%*2,Y%*2
        NEXT
      NEXT Y%
      
      REPEAT
        WAIT 1
      UNTIL FALSE


  

You may also check:How to resolve the algorithm Multisplit step by step in the Delphi programming language
You may also check:How to resolve the algorithm Undefined values step by step in the Oz programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the Delphi programming language
You may also check:How to resolve the algorithm Machine code step by step in the Action! programming language
You may also check:How to resolve the algorithm Deal cards for FreeCell step by step in the Go programming language