Don't forget to delete the hdmi_out and hdmi_in when finished

Generic Kernal Filter Notebook

In this notebook, we have provided an user interface which allows user to generate various image filters by controlling the values in a 3x3 kernal matrix, the dividing factor and the bias value.

There are some examples at the following webpages:

http://lodev.org/cgtutor/filtering.html#Emboss

Alternatively you can search online for kernel image processing.

Import libraries and download base bitstream


In [1]:
from pynq.drivers.video import HDMI
from pynq import Bitstream_Part
from pynq.board import Register
from pynq import Overlay
Overlay("demo.bit").download()

Start streaming from device connected to HDMI input on the PYNQ Board


In [2]:
hdmi_in = HDMI('in')
hdmi_out = HDMI('out', frame_list=hdmi_in.frame_list)
hdmi_out.mode(3)
hdmi_out.start()
hdmi_in.start()

Creates user interface

In this section, we create 10 registers which hold the values of the Kernal matrix, the dividing factor and the bias value. We also create a slider which allows user to control the values store in the registers.


In [3]:
R0 =Register(0)
R1 =Register(1)
R2 =Register(2)
R3 =Register(3)
R4 =Register(4)
R5 =Register(5)
R6 =Register(6)
R7 =Register(7)
R8 =Register(8)
R9 =Register(9)
R10 =Register(10)
import ipywidgets as widgets


R0_s = widgets.IntSlider(
    value=1,
    min=-128,
    max=127,
    step=1,
    description='M_0:',
    disabled=False,
    continuous_update=True,
    orientation='vertical',
    readout=True,
    readout_format='i',
    slider_color='black'
)
R1_s = widgets.IntSlider(
    value=1,
    min=-128,
    max=127,
    step=1,
    description='M_1:',
    disabled=False,
    continuous_update=True,
    orientation='vertical',
    readout=True,
    readout_format='i',
    slider_color='black'
)
R2_s = widgets.IntSlider(
    value=1,
    min=-128,
    max=127,
    step=1,
    description='M_2:',
    disabled=False,
    continuous_update=True,
    orientation='vertical',
    readout=True,
    readout_format='i',
    slider_color='black'
)
R3_s = widgets.IntSlider(
    value=1,
    min=-128,
    max=127,
    step=1,
    description='M_3:',
    disabled=False,
    continuous_update=True,
    orientation='vertical',
    readout=True,
    readout_format='i',
    slider_color='black'
)

R4_s = widgets.IntSlider(
    value=1,
    min=-128,
    max=127,
    step=1,
    description='M_4:',
    disabled=False,
    continuous_update=True,
    orientation='vertical',
    readout=True,
    readout_format='i',
    slider_color='black'
)

R5_s = widgets.IntSlider(
    value=1,
    min=-128,
    max=127,
    step=1,
    description='M_5:',
    disabled=False,
    continuous_update=True,
    orientation='vertical',
    readout=True,
    readout_format='i',
    slider_color='black'
)

R6_s = widgets.IntSlider(
    value=1,
    min=-128,
    max=127,
    step=1,
    description='M_6:',
    disabled=False,
    continuous_update=True,
    orientation='vertical',
    readout=True,
    readout_format='i',
    slider_color='black'
)

R7_s = widgets.IntSlider(
    value=1,
    min=-128,
    max=127,
    step=1,
    description='M_7:',
    disabled=False,
    continuous_update=True,
    orientation='vertical',
    readout=True,
    readout_format='i',
    slider_color='black'
)

R8_s = widgets.IntSlider(
    value=1,
    min=-128,
    max=127,
    step=1,
    description='M_8:',
    disabled=False,
    continuous_update=True,
    orientation='vertical',
    readout=True,
    readout_format='i',
    slider_color='black'
)

R9_s = widgets.IntSlider(
    value=9,
    min=1,
    max=127,
    step=1,
    description='Factor:',
    disabled=False,
    continuous_update=True,
    orientation='vertical',
    readout=True,
    readout_format='i',
    slider_color='black'
)

R10_s = widgets.IntSlider(
    value=0,
    min=0,
    max=255,
    step=1,
    description='Bias:',
    disabled=False,
    continuous_update=True,
    orientation='vertical',
    readout=True,
    readout_format='i',
    slider_color='black'
)

def update_r0(*args):
    R0.write(R0_s.value)
R0_s.observe(update_r0, 'value')
def update_r1(*args):
    R1.write(R1_s.value)
R1_s.observe(update_r1, 'value')
def update_r2(*args):
    R2.write(R2_s.value)
R2_s.observe(update_r2, 'value')
def update_r3(*args):
    R3.write(R3_s.value)
R3_s.observe(update_r3, 'value')
def update_r4(*args):
    R4.write(R4_s.value)
R4_s.observe(update_r4, 'value')
def update_r5(*args):
    R5.write(R5_s.value)
R5_s.observe(update_r5, 'value')
def update_r6(*args):
    R6.write(R6_s.value)
R6_s.observe(update_r6, 'value')
def update_r7(*args):
    R7.write(R7_s.value)
R7_s.observe(update_r7, 'value')
def update_r8(*args):
    R8.write(R8_s.value)
R8_s.observe(update_r8, 'value')
def update_r9(*args):
    R9.write(R9_s.value)
R9_s.observe(update_r9, 'value')
def update_r10(*args):
    R10.write(R10_s.value)
R10_s.observe(update_r10, 'value')

Continue to create user interface


In [4]:
from IPython.display import clear_output
from ipywidgets import Button, HBox, VBox

words = ['HDMI Reset', 'Kernal Filter']
items = [Button(description=w) for w in words]


def on_hdmi_clicked(b):
    hdmi_out.stop()
    hdmi_in.stop()
    hdmi_out.start()
    hdmi_in.start()
def on_Kernal_clicked(b):
    Bitstream_Part("Generic_Filter_p.bit").download()
    R3_s.disabled = False;
    R0.write(1)
    R1.write(1)
    R2.write(1)
    R3.write(1)
    R4.write(1)
    R5.write(1)
    R6.write(1)
    R7.write(1)
    R8.write(1)
    R9.write(9)
    R10.write(0)
    R0_s.description='M_0'
    R0_s.value = 1
    R0_s.max = 127
    R1_s.description='M_1'
    R1_s.value = 1
    R1_s.max = 127
    R2_s.description='M_2'
    R2_s.value = 1
    R2_s.max = 127
    R3_s.description='M_3'
    R3_s.value = 1
    R3_s.max = 127
    R4_s.description='M_4'
    R4_s.value = 1
    R4_s.max = 127
    R5_s.description='M_5'
    R5_s.value = 1
    R5_s.max = 127
    R6_s.description='M_6'
    R6_s.value = 1
    R6_s.max = 127
    R7_s.description='M_7'
    R7_s.value = 1
    R7_s.max = 127
    R8_s.description='M_8'
    R8_s.value = 1
    R8_s.max = 127
    R9_s.description='Factor'
    R9_s.value = 9
    R9_s.max = 127
    R10_s.description='Bias'
    R10_s.value = 0
    R10_s.max = 255

items[0].on_click(on_hdmi_clicked)
items[1].on_click(on_Kernal_clicked)

User interface instruction

At this point, the streaming may not work properly. Please run the code section below. Afterwards, press the 'HDMI Reset" button to reset the HDMI input and output. The streaming should now work properly.

In order to start applying filter on the stream, press the 'Kernal Filter' button. The kernal filter is default as a Box Blur filter.

Afterwards, users can change to any kernal filter they want by changing the value on the slider.

Each values is denoted by the equation below:

[M0 M1 M2]

[M3 M4 M5]

[M6 M7 M8] / Factor + Bias


In [5]:
HBox([VBox([items[0], items[1]]),R0_s,R1_s,R2_s,R3_s,R4_s,R5_s,R6_s,R7_s,R8_s,R9_s,R10_s])

In [6]:
hdmi_in.stop()
hdmi_out.stop()
del hdmi_in
del hdmi_out

In [ ]: