Edge detection

Open In Colab

In [1]:
# to run in google colab
import sys
if 'google.colab' in sys.modules:
    import subprocess
    subprocess.call('apt-get install subversion'.split())
    subprocess.call('svn export https://github.com/YoniChechik/AI_is_Math/trunk/c_03_edge_detection/Bikesgray.jpg'.split())

# save plotly as html frames
import plotly.io as pio
if (pio.renderers.default != 'vscode') & (pio.renderers.default != 'colab'):
    pio.renderers.default = "iframe_connected"
In [2]:
import numpy as np
import cv2
from matplotlib import pyplot as plt
import plotly.express as px

figsize = (10,10)

Original image

In [3]:
img = cv2.imread("Bikesgray.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.figure(figsize=figsize)
plt.imshow(img, cmap='gray', vmin=0, vmax=255)
plt.title('Original image')
Out[3]:
Text(0.5, 1.0, 'Original image')

Magnitude and phase images

In [4]:
img = img.astype(float)

kernel = 1/8*np.array([
    [-1, 0, +1],
    [-2, 0, +2],
    [-1, 0, +1]])
sobel_x = cv2.filter2D(img, -1, kernel)

kernel = kernel.T
sobel_y = cv2.filter2D(img, -1, kernel)

mag_img = np.sqrt(sobel_x**2+sobel_y**2)

phase_img = cv2.phase(sobel_x, -sobel_y, angleInDegrees=True)

phase_img_masked = -100*np.ones(phase_img.shape)
TH_PRC = 0.15
th = mag_img.max()*TH_PRC
phase_img_masked = phase_img_masked*(mag_img <= th) + phase_img*(mag_img > th)


px.imshow(mag_img,title='Gradient magnitude')
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    916             method = get_real_method(obj, self.print_method)
    917             if method is not None:
--> 918                 method()
    919                 return True
    920 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _ipython_display_(self)
    842 
    843         if pio.renderers.render_on_display and pio.renderers.default:
--> 844             pio.show(self)
    845         else:
    846             print(repr(self))

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in show(fig, renderer, validate, **kwargs)
    387 
    388     # Mimetype renderers
--> 389     bundle = renderers._build_mime_bundle(fig_dict, renderers_string=renderer, **kwargs)
    390     if bundle:
    391         if not ipython_display:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in _build_mime_bundle(self, fig_dict, renderers_string, **kwargs)
    295                         setattr(renderer, k, v)
    296 
--> 297                 bundle.update(renderer.to_mimebundle(fig_dict))
    298 
    299         return bundle

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj, include, exclude)
    968 
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_html_(self)
    808         Customize html representation
    809         """
--> 810         bundle = self._repr_mimebundle_()
    811         if "text/html" in bundle:
    812             return bundle["text/html"]

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
Out[4]:
Figure({
    'data': [{'coloraxis': 'coloraxis',
              'hovertemplate': 'x: %{x}<br>y: %{y}<br>color: %{z}<extra></extra>',
              'name': '0',
              'type': 'heatmap',
              'xaxis': 'x',
              'yaxis': 'y',
              'z': array([[ 0.        ,  0.75      ,  3.75      , ...,  0.5       ,  1.25      ,
                            0.        ],
                          [ 1.25      ,  1.45773797,  2.9633174 , ...,  3.1868872 ,  3.20156212,
                            0.75      ],
                          [ 4.        ,  4.25      ,  3.53995056, ...,  2.        ,  3.74583102,
                            2.        ],
                          ...,
                          [ 0.25      , 13.61295339, 25.17935662, ...,  0.53033009,  0.        ,
                            0.5       ],
                          [ 0.        ,  9.56719656, 22.36137854, ...,  0.63737744,  0.63737744,
                            0.25      ],
                          [ 0.        ,  6.25      , 12.        , ...,  0.75      ,  0.75      ,
                            0.        ]])}],
    'layout': {'coloraxis': {'colorscale': [[0.0, '#0d0887'], [0.1111111111111111,
                                            '#46039f'], [0.2222222222222222,
                                            '#7201a8'], [0.3333333333333333,
                                            '#9c179e'], [0.4444444444444444,
                                            '#bd3786'], [0.5555555555555556,
                                            '#d8576b'], [0.6666666666666666,
                                            '#ed7953'], [0.7777777777777778,
                                            '#fb9f3a'], [0.8888888888888888,
                                            '#fdca26'], [1.0, '#f0f921']]},
               'template': '...',
               'title': {'text': 'Gradient magnitude'},
               'xaxis': {'anchor': 'y', 'constrain': 'domain', 'domain': [0.0, 1.0], 'scaleanchor': 'y'},
               'yaxis': {'anchor': 'x', 'autorange': 'reversed', 'constrain': 'domain', 'domain': [0.0, 1.0]}}
})
In [5]:
px.imshow(phase_img_masked,title='Gradient phase thresholeded')
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    916             method = get_real_method(obj, self.print_method)
    917             if method is not None:
--> 918                 method()
    919                 return True
    920 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _ipython_display_(self)
    842 
    843         if pio.renderers.render_on_display and pio.renderers.default:
--> 844             pio.show(self)
    845         else:
    846             print(repr(self))

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in show(fig, renderer, validate, **kwargs)
    387 
    388     # Mimetype renderers
--> 389     bundle = renderers._build_mime_bundle(fig_dict, renderers_string=renderer, **kwargs)
    390     if bundle:
    391         if not ipython_display:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in _build_mime_bundle(self, fig_dict, renderers_string, **kwargs)
    295                         setattr(renderer, k, v)
    296 
--> 297                 bundle.update(renderer.to_mimebundle(fig_dict))
    298 
    299         return bundle

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj, include, exclude)
    968 
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_html_(self)
    808         Customize html representation
    809         """
--> 810         bundle = self._repr_mimebundle_()
    811         if "text/html" in bundle:
    812             return bundle["text/html"]

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
Out[5]:
Figure({
    'data': [{'coloraxis': 'coloraxis',
              'hovertemplate': 'x: %{x}<br>y: %{y}<br>color: %{z}<extra></extra>',
              'name': '0',
              'type': 'heatmap',
              'xaxis': 'x',
              'yaxis': 'y',
              'z': array([[-100.        , -100.        , -100.        , ..., -100.        ,
                           -100.        , -100.        ],
                          [-100.        , -100.        , -100.        , ..., -100.        ,
                           -100.        , -100.        ],
                          [-100.        , -100.        , -100.        , ..., -100.        ,
                           -100.        , -100.        ],
                          ...,
                          [-100.        , -100.        ,    6.84199858, ..., -100.        ,
                           -100.        , -100.        ],
                          [-100.        , -100.        ,   33.59444427, ..., -100.        ,
                           -100.        , -100.        ],
                          [-100.        , -100.        , -100.        , ..., -100.        ,
                           -100.        , -100.        ]])}],
    'layout': {'coloraxis': {'colorscale': [[0.0, '#0d0887'], [0.1111111111111111,
                                            '#46039f'], [0.2222222222222222,
                                            '#7201a8'], [0.3333333333333333,
                                            '#9c179e'], [0.4444444444444444,
                                            '#bd3786'], [0.5555555555555556,
                                            '#d8576b'], [0.6666666666666666,
                                            '#ed7953'], [0.7777777777777778,
                                            '#fb9f3a'], [0.8888888888888888,
                                            '#fdca26'], [1.0, '#f0f921']]},
               'template': '...',
               'title': {'text': 'Gradient phase thresholeded'},
               'xaxis': {'anchor': 'y', 'constrain': 'domain', 'domain': [0.0, 1.0], 'scaleanchor': 'y'},
               'yaxis': {'anchor': 'x', 'autorange': 'reversed', 'constrain': 'domain', 'domain': [0.0, 1.0]}}
})

Edge thinning

LoG filter

One way to implement edge thinning is to use LoG and then zero crossing finder. Below is an example of abs LoG. Look at the handlebar to see the zero crossings.

In [6]:
kernel = np.array([
    [-1, -1, -1],
    [-1, 8, -1],
    [-1, -1, -1]])
dst_LoG = cv2.filter2D(img, -1, kernel)

px.imshow(np.abs(dst_LoG),title='abs LoG')
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    916             method = get_real_method(obj, self.print_method)
    917             if method is not None:
--> 918                 method()
    919                 return True
    920 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _ipython_display_(self)
    842 
    843         if pio.renderers.render_on_display and pio.renderers.default:
--> 844             pio.show(self)
    845         else:
    846             print(repr(self))

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in show(fig, renderer, validate, **kwargs)
    387 
    388     # Mimetype renderers
--> 389     bundle = renderers._build_mime_bundle(fig_dict, renderers_string=renderer, **kwargs)
    390     if bundle:
    391         if not ipython_display:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in _build_mime_bundle(self, fig_dict, renderers_string, **kwargs)
    295                         setattr(renderer, k, v)
    296 
--> 297                 bundle.update(renderer.to_mimebundle(fig_dict))
    298 
    299         return bundle

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj, include, exclude)
    968 
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_html_(self)
    808         Customize html representation
    809         """
--> 810         bundle = self._repr_mimebundle_()
    811         if "text/html" in bundle:
    812             return bundle["text/html"]

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
Out[6]:
Figure({
    'data': [{'coloraxis': 'coloraxis',
              'hovertemplate': 'x: %{x}<br>y: %{y}<br>color: %{z}<extra></extra>',
              'name': '0',
              'type': 'heatmap',
              'xaxis': 'x',
              'yaxis': 'y',
              'z': array([[ 28.,  33.,  13., ...,  21.,   5.,  18.],
                          [ 24.,  17.,  17., ...,  16.,   7.,  50.],
                          [  3.,   4.,  18., ...,  34.,  31.,  21.],
                          ...,
                          [  9.,  94.,   2., ...,   7.,   4.,   4.],
                          [ 10.,  23.,  30., ...,   5.,   7.,  13.],
                          [  8.,  57., 118., ...,  13.,  17.,  22.]])}],
    'layout': {'coloraxis': {'colorscale': [[0.0, '#0d0887'], [0.1111111111111111,
                                            '#46039f'], [0.2222222222222222,
                                            '#7201a8'], [0.3333333333333333,
                                            '#9c179e'], [0.4444444444444444,
                                            '#bd3786'], [0.5555555555555556,
                                            '#d8576b'], [0.6666666666666666,
                                            '#ed7953'], [0.7777777777777778,
                                            '#fb9f3a'], [0.8888888888888888,
                                            '#fdca26'], [1.0, '#f0f921']]},
               'template': '...',
               'title': {'text': 'abs LoG'},
               'xaxis': {'anchor': 'y', 'constrain': 'domain', 'domain': [0.0, 1.0], 'scaleanchor': 'y'},
               'yaxis': {'anchor': 'x', 'autorange': 'reversed', 'constrain': 'domain', 'domain': [0.0, 1.0]}}
})

NMS

Non maximum suppression (NMS) is another way for edge thinning.

NMS preliminary step: Quantizing the phase image

In [7]:
phase_img_q = phase_img.copy()
for i in range(mag_img.shape[0]):
    for j in range(mag_img.shape[1]):
        phase_img_q[i, j] = np.mod(phase_img_q[i, j]+22.5, 180)
        phase_img_q[i, j] = (phase_img_q[i, j])//45  # integer devider

phase_img_q_masked = -1*np.ones(phase_img.shape)
TH_PRC = 0.1
th = mag_img.max()*TH_PRC
phase_img_q_masked = phase_img_q_masked * \
    (mag_img <= th) + phase_img_q*(mag_img > th)

px.imshow(phase_img_q_masked, title='Gradient phase- quantized and thresholded')
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    916             method = get_real_method(obj, self.print_method)
    917             if method is not None:
--> 918                 method()
    919                 return True
    920 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _ipython_display_(self)
    842 
    843         if pio.renderers.render_on_display and pio.renderers.default:
--> 844             pio.show(self)
    845         else:
    846             print(repr(self))

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in show(fig, renderer, validate, **kwargs)
    387 
    388     # Mimetype renderers
--> 389     bundle = renderers._build_mime_bundle(fig_dict, renderers_string=renderer, **kwargs)
    390     if bundle:
    391         if not ipython_display:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in _build_mime_bundle(self, fig_dict, renderers_string, **kwargs)
    295                         setattr(renderer, k, v)
    296 
--> 297                 bundle.update(renderer.to_mimebundle(fig_dict))
    298 
    299         return bundle

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj, include, exclude)
    968 
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_html_(self)
    808         Customize html representation
    809         """
--> 810         bundle = self._repr_mimebundle_()
    811         if "text/html" in bundle:
    812             return bundle["text/html"]

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
Out[7]:
Figure({
    'data': [{'coloraxis': 'coloraxis',
              'hovertemplate': 'x: %{x}<br>y: %{y}<br>color: %{z}<extra></extra>',
              'name': '0',
              'type': 'heatmap',
              'xaxis': 'x',
              'yaxis': 'y',
              'z': array([[-1., -1., -1., ..., -1., -1., -1.],
                          [-1., -1., -1., ..., -1., -1., -1.],
                          [-1., -1., -1., ..., -1., -1., -1.],
                          ...,
                          [-1.,  0.,  0., ..., -1., -1., -1.],
                          [-1., -1.,  1., ..., -1., -1., -1.],
                          [-1., -1.,  0., ..., -1., -1., -1.]])}],
    'layout': {'coloraxis': {'colorscale': [[0.0, '#0d0887'], [0.1111111111111111,
                                            '#46039f'], [0.2222222222222222,
                                            '#7201a8'], [0.3333333333333333,
                                            '#9c179e'], [0.4444444444444444,
                                            '#bd3786'], [0.5555555555555556,
                                            '#d8576b'], [0.6666666666666666,
                                            '#ed7953'], [0.7777777777777778,
                                            '#fb9f3a'], [0.8888888888888888,
                                            '#fdca26'], [1.0, '#f0f921']]},
               'template': '...',
               'title': {'text': 'Gradient phase- quantized and thresholded'},
               'xaxis': {'anchor': 'y', 'constrain': 'domain', 'domain': [0.0, 1.0], 'scaleanchor': 'y'},
               'yaxis': {'anchor': 'x', 'autorange': 'reversed', 'constrain': 'domain', 'domain': [0.0, 1.0]}}
})

NMS main step

In [8]:
nms = mag_img.copy()

for i in range(1, mag_img.shape[0]-1):
    for j in range(1, mag_img.shape[1]-1):
        if phase_img_q[i, j] == 0 and (mag_img[i, j+1] > mag_img[i, j] or mag_img[i, j-1] > mag_img[i, j]):
            nms[i, j] = -50
        if phase_img_q[i, j] == 1 and (mag_img[i+1, j-1] > mag_img[i, j] or mag_img[i-1, j+1] > mag_img[i, j]):
            nms[i, j] = -50
        if phase_img_q[i, j] == 2 and (mag_img[i-1, j] > mag_img[i, j] or mag_img[i+1, j] > mag_img[i, j]):
            nms[i, j] = -50
        if phase_img_q[i, j] == 3 and (mag_img[i-1, j-1] > mag_img[i, j] or mag_img[i+1, j+1] > mag_img[i, j]):
            nms[i, j] = -50

px.imshow(nms, title='NMS')
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    916             method = get_real_method(obj, self.print_method)
    917             if method is not None:
--> 918                 method()
    919                 return True
    920 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _ipython_display_(self)
    842 
    843         if pio.renderers.render_on_display and pio.renderers.default:
--> 844             pio.show(self)
    845         else:
    846             print(repr(self))

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in show(fig, renderer, validate, **kwargs)
    387 
    388     # Mimetype renderers
--> 389     bundle = renderers._build_mime_bundle(fig_dict, renderers_string=renderer, **kwargs)
    390     if bundle:
    391         if not ipython_display:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in _build_mime_bundle(self, fig_dict, renderers_string, **kwargs)
    295                         setattr(renderer, k, v)
    296 
--> 297                 bundle.update(renderer.to_mimebundle(fig_dict))
    298 
    299         return bundle

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj, include, exclude)
    968 
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_html_(self)
    808         Customize html representation
    809         """
--> 810         bundle = self._repr_mimebundle_()
    811         if "text/html" in bundle:
    812             return bundle["text/html"]

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
Out[8]:
Figure({
    'data': [{'coloraxis': 'coloraxis',
              'hovertemplate': 'x: %{x}<br>y: %{y}<br>color: %{z}<extra></extra>',
              'name': '0',
              'type': 'heatmap',
              'xaxis': 'x',
              'yaxis': 'y',
              'z': array([[  0.        ,   0.75      ,   3.75      , ...,   0.5       ,
                             1.25      ,   0.        ],
                          [  1.25      , -50.        , -50.        , ...,   3.1868872 ,
                             3.20156212,   0.75      ],
                          [  4.        ,   4.25      ,   3.53995056, ..., -50.        ,
                           -50.        ,   2.        ],
                          ...,
                          [  0.25      , -50.        ,  25.17935662, ..., -50.        ,
                           -50.        ,   0.5       ],
                          [  0.        , -50.        ,  22.36137854, ..., -50.        ,
                             0.63737744,   0.25      ],
                          [  0.        ,   6.25      ,  12.        , ...,   0.75      ,
                             0.75      ,   0.        ]])}],
    'layout': {'coloraxis': {'colorscale': [[0.0, '#0d0887'], [0.1111111111111111,
                                            '#46039f'], [0.2222222222222222,
                                            '#7201a8'], [0.3333333333333333,
                                            '#9c179e'], [0.4444444444444444,
                                            '#bd3786'], [0.5555555555555556,
                                            '#d8576b'], [0.6666666666666666,
                                            '#ed7953'], [0.7777777777777778,
                                            '#fb9f3a'], [0.8888888888888888,
                                            '#fdca26'], [1.0, '#f0f921']]},
               'template': '...',
               'title': {'text': 'NMS'},
               'xaxis': {'anchor': 'y', 'constrain': 'domain', 'domain': [0.0, 1.0], 'scaleanchor': 'y'},
               'yaxis': {'anchor': 'x', 'autorange': 'reversed', 'constrain': 'domain', 'domain': [0.0, 1.0]}}
})

double TH

In [9]:
nms_th = np.zeros(nms.shape)
TH_l = 3
TH_h = 13
nms_th[nms >= TH_h] = 2
nms_th[np.bitwise_and(TH_l <= nms, nms < TH_h)] = 1

px.imshow(nms_th, title='double TH')
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    916             method = get_real_method(obj, self.print_method)
    917             if method is not None:
--> 918                 method()
    919                 return True
    920 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _ipython_display_(self)
    842 
    843         if pio.renderers.render_on_display and pio.renderers.default:
--> 844             pio.show(self)
    845         else:
    846             print(repr(self))

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in show(fig, renderer, validate, **kwargs)
    387 
    388     # Mimetype renderers
--> 389     bundle = renderers._build_mime_bundle(fig_dict, renderers_string=renderer, **kwargs)
    390     if bundle:
    391         if not ipython_display:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in _build_mime_bundle(self, fig_dict, renderers_string, **kwargs)
    295                         setattr(renderer, k, v)
    296 
--> 297                 bundle.update(renderer.to_mimebundle(fig_dict))
    298 
    299         return bundle

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj, include, exclude)
    968 
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_html_(self)
    808         Customize html representation
    809         """
--> 810         bundle = self._repr_mimebundle_()
    811         if "text/html" in bundle:
    812             return bundle["text/html"]

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
Out[9]:
Figure({
    'data': [{'coloraxis': 'coloraxis',
              'hovertemplate': 'x: %{x}<br>y: %{y}<br>color: %{z}<extra></extra>',
              'name': '0',
              'type': 'heatmap',
              'xaxis': 'x',
              'yaxis': 'y',
              'z': array([[0., 0., 1., ..., 0., 0., 0.],
                          [0., 0., 0., ..., 1., 1., 0.],
                          [1., 1., 1., ..., 0., 0., 0.],
                          ...,
                          [0., 0., 2., ..., 0., 0., 0.],
                          [0., 0., 2., ..., 0., 0., 0.],
                          [0., 1., 1., ..., 0., 0., 0.]])}],
    'layout': {'coloraxis': {'colorscale': [[0.0, '#0d0887'], [0.1111111111111111,
                                            '#46039f'], [0.2222222222222222,
                                            '#7201a8'], [0.3333333333333333,
                                            '#9c179e'], [0.4444444444444444,
                                            '#bd3786'], [0.5555555555555556,
                                            '#d8576b'], [0.6666666666666666,
                                            '#ed7953'], [0.7777777777777778,
                                            '#fb9f3a'], [0.8888888888888888,
                                            '#fdca26'], [1.0, '#f0f921']]},
               'template': '...',
               'title': {'text': 'double TH'},
               'xaxis': {'anchor': 'y', 'constrain': 'domain', 'domain': [0.0, 1.0], 'scaleanchor': 'y'},
               'yaxis': {'anchor': 'x', 'autorange': 'reversed', 'constrain': 'domain', 'domain': [0.0, 1.0]}}
})

Iterative hysteresis

We will do the iterative process with connected components (CC):

  1. Take a mask of combined weak and strong edges and run CC algorithm on it.
  2. For each such CC group- test if there is intersection with ONLY strong edges mask.
  3. If intersection exist, then weak edges in CC group is actually strong edges, so unite the masks.
In [10]:
nms_weak_and_strong = np.zeros(nms_th.shape,dtype=np.bool)
nms_strong = np.zeros(nms_th.shape,dtype=np.bool)

nms_weak_and_strong[nms_th>0]=1
nms_strong[nms_th==2]=1

num_w_s_CCs, w_s_CC_mask = cv2.connectedComponents(nms_weak_and_strong.astype(np.uint8))

# for each CC group of weak and strong edge mask
for w_s_CC_i in range(1,num_w_s_CCs):

    # get MASK of weak_and_strong edge from index w_s_CC_i
    w_s_CC_mask_i = np.zeros(nms_th.shape,dtype=np.bool)
    w_s_CC_mask_i[w_s_CC_mask==w_s_CC_i]=1

    # if w_s_CC_mask_i has intersection with strong edges mask, add to strong edge mask
    if np.any(np.bitwise_and(w_s_CC_mask_i, nms_strong)):
        nms_strong = np.bitwise_or(w_s_CC_mask_i, nms_strong)

px.imshow(nms_strong, title='Canny final result')
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    916             method = get_real_method(obj, self.print_method)
    917             if method is not None:
--> 918                 method()
    919                 return True
    920 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _ipython_display_(self)
    842 
    843         if pio.renderers.render_on_display and pio.renderers.default:
--> 844             pio.show(self)
    845         else:
    846             print(repr(self))

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in show(fig, renderer, validate, **kwargs)
    387 
    388     # Mimetype renderers
--> 389     bundle = renderers._build_mime_bundle(fig_dict, renderers_string=renderer, **kwargs)
    390     if bundle:
    391         if not ipython_display:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in _build_mime_bundle(self, fig_dict, renderers_string, **kwargs)
    295                         setattr(renderer, k, v)
    296 
--> 297                 bundle.update(renderer.to_mimebundle(fig_dict))
    298 
    299         return bundle

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj, include, exclude)
    968 
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_html_(self)
    808         Customize html representation
    809         """
--> 810         bundle = self._repr_mimebundle_()
    811         if "text/html" in bundle:
    812             return bundle["text/html"]

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
Out[10]:
Figure({
    'data': [{'coloraxis': 'coloraxis',
              'hovertemplate': 'x: %{x}<br>y: %{y}<br>color: %{z}<extra></extra>',
              'name': '0',
              'type': 'heatmap',
              'xaxis': 'x',
              'yaxis': 'y',
              'z': array([[  0,   0,   0, ...,   0,   0,   0],
                          [  0,   0,   0, ...,   0,   0,   0],
                          [  0,   0,   0, ...,   0,   0,   0],
                          ...,
                          [  0,   0, 255, ...,   0,   0,   0],
                          [  0,   0, 255, ...,   0,   0,   0],
                          [  0, 255, 255, ...,   0,   0,   0]], dtype=uint8)}],
    'layout': {'coloraxis': {'colorscale': [[0.0, '#0d0887'], [0.1111111111111111,
                                            '#46039f'], [0.2222222222222222,
                                            '#7201a8'], [0.3333333333333333,
                                            '#9c179e'], [0.4444444444444444,
                                            '#bd3786'], [0.5555555555555556,
                                            '#d8576b'], [0.6666666666666666,
                                            '#ed7953'], [0.7777777777777778,
                                            '#fb9f3a'], [0.8888888888888888,
                                            '#fdca26'], [1.0, '#f0f921']]},
               'template': '...',
               'title': {'text': 'Canny final result'},
               'xaxis': {'anchor': 'y', 'constrain': 'domain', 'domain': [0.0, 1.0], 'scaleanchor': 'y'},
               'yaxis': {'anchor': 'x', 'autorange': 'reversed', 'constrain': 'domain', 'domain': [0.0, 1.0]}}
})

cv2 Canny

let's see the results from the default canny of cv2

In [11]:
res = cv2.Canny(img.astype(np.uint8),105,120)
px.imshow(res,title='cv2.Canny final result')
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    916             method = get_real_method(obj, self.print_method)
    917             if method is not None:
--> 918                 method()
    919                 return True
    920 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _ipython_display_(self)
    842 
    843         if pio.renderers.render_on_display and pio.renderers.default:
--> 844             pio.show(self)
    845         else:
    846             print(repr(self))

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in show(fig, renderer, validate, **kwargs)
    387 
    388     # Mimetype renderers
--> 389     bundle = renderers._build_mime_bundle(fig_dict, renderers_string=renderer, **kwargs)
    390     if bundle:
    391         if not ipython_display:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_renderers.py in _build_mime_bundle(self, fig_dict, renderers_string, **kwargs)
    295                         setattr(renderer, k, v)
    296 
--> 297                 bundle.update(renderer.to_mimebundle(fig_dict))
    298 
    299         return bundle

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj, include, exclude)
    968 
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_html_(self)
    808         Customize html representation
    809         """
--> 810         bundle = self._repr_mimebundle_()
    811         if "text/html" in bundle:
    812             return bundle["text/html"]

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\basedatatypes.py in _repr_mimebundle_(self, include, exclude, validate, **kwargs)
    832         for renderer in renderers_list:
    833             if isinstance(renderer, MimetypeRenderer):
--> 834                 bundle.update(renderer.to_mimebundle(fig_dict))
    835         return bundle
    836 

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in to_mimebundle(self, fig_dict)
    566 
    567         # Build filename using ipython cell number
--> 568         filename = self.build_filename()
    569 
    570         # Make directory for

c:\users\chech\appdata\local\programs\python\venv_cv_course_38\lib\site-packages\plotly\io\_base_renderers.py in build_filename(self)
    608     def build_filename(self):
    609         ip = IPython.get_ipython() if IPython else None
--> 610         cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    611         filename = "{dirname}/figure_{cell_number}.html".format(
    612             dirname=self.html_directory, cell_number=cell_number

IndexError: list index out of range
Out[11]:
Figure({
    'data': [{'coloraxis': 'coloraxis',
              'hovertemplate': 'x: %{x}<br>y: %{y}<br>color: %{z}<extra></extra>',
              'name': '0',
              'type': 'heatmap',
              'xaxis': 'x',
              'yaxis': 'y',
              'z': array([[  0,   0,   0, ...,   0,   0,   0],
                          [  0,   0,   0, ...,   0,   0,   0],
                          [  0,   0,   0, ...,   0,   0,   0],
                          ...,
                          [  0,   0, 255, ...,   0,   0,   0],
                          [  0,   0, 255, ...,   0,   0,   0],
                          [  0,   0,   0, ...,   0,   0,   0]], dtype=uint8)}],
    'layout': {'coloraxis': {'colorscale': [[0.0, '#0d0887'], [0.1111111111111111,
                                            '#46039f'], [0.2222222222222222,
                                            '#7201a8'], [0.3333333333333333,
                                            '#9c179e'], [0.4444444444444444,
                                            '#bd3786'], [0.5555555555555556,
                                            '#d8576b'], [0.6666666666666666,
                                            '#ed7953'], [0.7777777777777778,
                                            '#fb9f3a'], [0.8888888888888888,
                                            '#fdca26'], [1.0, '#f0f921']]},
               'template': '...',
               'title': {'text': 'cv2.Canny final result'},
               'xaxis': {'anchor': 'y', 'constrain': 'domain', 'domain': [0.0, 1.0], 'scaleanchor': 'y'},
               'yaxis': {'anchor': 'x', 'autorange': 'reversed', 'constrain': 'domain', 'domain': [0.0, 1.0]}}
})