StevanovicMilan

10.5

Sep 18th, 2021
4,636
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. Učitati sliku lena_color.png i prevesti je u HSV sistem boja (engl. Hue-Saturation-Value).
  3.  
  4. 1. Povećati vrednost H komponente za 0.33, tri puta za redom. Nakon svake promene prikazati sliku.
  5. 2. Prikazati slike nastale modifikacijom S komponente za faktor u intervalu od -0.6 do 0.6 sa korakom 0.2.
  6. 3. Ekvalizovati histogram V komponente. Prikazati sliku nakon promene.
  7. '''
  8.  
  9. import plotly.express as px
  10. from skimage import io
  11.  
  12. img = io.imread('lena_color.png')
  13. fig = px.imshow(img)
  14. fig.show()
  15.  
  16. from skimage import color
  17.  
  18. img_hsv = color.rgb2hsv(img)
  19.  
  20. fig = px.imshow(img_hsv[:,:,0], color_continuous_scale='gray')
  21. fig.show()
  22.  
  23. fig = px.imshow(img_hsv[:,:,1], color_continuous_scale='gray')
  24. fig.show()
  25.  
  26. fig = px.imshow(img_hsv[:,:,2], color_continuous_scale='gray')
  27. fig.show()
  28.  
  29. img_rgb = color.hsv2rgb(img_hsv)
  30.  
  31. fig = px.imshow(img_rgb)
  32. fig.show()
  33.  
  34. '''
  35. Povećati vrednost H komponente za  1/3 , tri puta za redom.
  36. Nakon svake promene prikazati sliku.
  37. '''
  38. import plotly.graph_objects as go
  39. import numpy as np
  40.  
  41. # Create figure
  42. fig = go.Figure()
  43.  
  44. increment = 1/3
  45.  
  46. # Add traces, one for each slider step
  47. for i in range(3):
  48.     img_hsv[:,:,0] = (img_hsv[:,:,0] + increment) % 1
  49.     out = color.hsv2rgb(img_hsv)
  50.  
  51.     # Izlazna slika je 'float' slika i radi prikaza u colab-u pretvoricemo u uint8 slike
  52.     out = (255*out).astype('uint8')
  53.  
  54.     fig.add_trace(go.Image(z=out, name="faktor = "+str((i+1)*increment), visible=false))
  55.  
  56.     # Make the 1st visible
  57.     active_idx = 0
  58.     fig.data[active_idx].visible = True
  59.  
  60.     # Create and add slider
  61.     steps = []
  62.     for i in range(len(fig.data)):
  63.         step = dict(method="update",
  64.                     args=[{"visible": [False] * len(fig.data)},
  65.                           {"title": "Faktor : " + str((i+1)*increment)}])
  66.         step["args"][0]["visible"][i] = True # Toggle i'th trace to "visible"
  67.         steps.append(step)
  68.  
  69.     sliders = [dict(active=active_idx, steps=steps)]
  70.  
  71.     fig.update_layout(sliders = sliders)
  72.     fig.show()
  73.  
  74. # Create figure
  75. fig = go.Figure()
  76.  
  77. s_factor = [-0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6]
  78.  
  79. # Add traces, one for each slider step
  80. for i in s_factor:
  81.     img_hsv = color.rgb2hsv(img)
  82.  
  83.     img_hsv[:, :, 1] = np.maximum(np.minimum(img_hsv[:, :, 1] + i, 1), 0)
  84.     out = color.hsv2rgb(img_hsv)
  85.  
  86.     # izlazna slika je 'float' slika i radi prikaza u colab-u pretvoricemo u uint8 slike
  87.     out = (255 * out).astype('uint8')
  88.  
  89.     fig.add_trace(go.Image(z=out, name="faktor = " + str(i), visible=False))
  90.  
  91. # Make the center visible
  92. active_idx = 3
  93. fig.data[active_idx].visible = True
  94.  
  95. # Create and add slider
  96. steps = []
  97. for i in range(len(fig.data)):
  98.     step = dict(method="update",
  99.                 args=[{"visible": [False] * len(fig.data)},
  100.                       {"title": "Faktor : " + str(s_factor[i])}])
  101.  
  102.     step["args"][0]["visible"][i] = True  # Toggle i'th trace to "visible"
  103.     steps.append(step)
  104.  
  105. sliders = [dict(active=active_idx, steps=steps)]
  106.  
  107. fig.update_layout(sliders=sliders)
  108. fig.show()
  109.  
  110. from skimage import exposure
  111.  
  112. img_hsv = color.rgb2hsv(img)
  113. img_hsv[:,:,2] = exposure.equalize_hist(img_hsv[:,:,2])
  114.  
  115. out = color.hsv2rgb(img_hsv)
  116. fig = px.imshow(out)
  117. fig.show()
  118.  
RAW Paste Data