title: trouble displaying image HSI converted to RGB python
body: <p>I've been working in a algorithm to convert RGB to HSI and vice-versa in python 3, which it display the resulted images and each channel using matplotlib.</p>
  
  <p>The trouble is displaying HSI to RGB resulted image: Each channel alone is being displayed correctly, but when it shows the tree channels together I get a weird image.</p>
  
  <p>By the way, when I save the resulted image with OpenCV it shows the image correctly.</p>
  
  <p><a href="https://i.stack.imgur.com/LZ5ht.png" rel="nofollow noreferrer">Resulted display</a></p>
  
  <p>What I did, but nothing changed:</p>
  
  <ul>
  <li><p>Round the values and if it pass 1, give 1 to the pixel</p></li>
  <li><p>In the conversion HSI to RGB, instead define R, G and B arrays with zeros, define arrays with ones</p></li>
  <li><p>In the conversion RGB to HSI, change the values between [0,360],[0,1],[0,1] to values between [0,360],[0,255],[0,255] rounded or not</p></li>
  <li><p>Instead use Jupyter notebook, use collab.research by google or Spider</p></li>
  <li><p>Execute the code on terminal, but it gives me blank windows</p></li>
  </ul>
  
  <p><strong>Function to display images:</strong></p>
  
  <pre><code>def show_images(T, cols=1):
    N = len(T)  
    fig = plt.figure()
    for i in range(N):
        a = fig.add_subplot(np.ceil(N/float(cols)), cols, i+1)
        try:
          img,title = T[i]
        except ValueError:
          img,title = T[i], "Image %d" % (i+1) 
        if(img.ndim == 2):
          plt.gray()
        plt.imshow(img)
        a.set_title(title)
        plt.xticks([0,img.shape[1]]), plt.yticks([0,img.shape[0]])
    fig.set_size_inches(np.array(fig.get_size_inches()) * N)
    plt.show()
  </code></pre>
  
  <p>Then the main function do this:</p>
  
  <pre><code>image = bgr_to_rgb(cv2.imread("rgb.png"))
  
  img1 = rgb_to_hsi(image)
  img2 = hsi_to_rgb(img1)
  
  show_images([(image,"RGB"),
           (image[:,:,0],"Red"),
           (image[:,:,1],"Green"),
           (image[:,:,2],"Blue")], 4)
  show_images([(img1,"RGB-&gt;HSI"),
           (img1[:,:,0],"Hue"),
           (img1[:,:,1],"Saturation"),
           (img1[:,:,2],"Intensity")], 4)
  show_images([(img2,"HSI-&gt;RGB"),
           (img2[:,:,0],"Red"),
           (img2[:,:,1],"Green"),
           (img2[:,:,2],"Blue")], 4)
  </code></pre>
  
  <p><strong>Conversion RGB to HSI:</strong></p>
  
  <pre><code>def rgb_to_hsi(img):
    zmax = 255 # max value
    # values in [0,1]
    R = np.divide(img[:,:,0],zmax,dtype=np.float)
    G = np.divide(img[:,:,1],zmax,dtype=np.float)
    B = np.divide(img[:,:,2],zmax,dtype=np.float)
  
    # Hue, when R=G=B -&gt; H=90
    a = (0.5)*np.add(np.subtract(R,G), np.subtract(R,B)) # (1/2)*[(R-G)+(R-B)]
    b = np.sqrt(np.add(np.power(np.subtract(R,G), 2) , np.multiply(np.subtract(R,B),np.subtract(G,B))))
    tetha = np.arccos( np.divide(a, b, out=np.zeros_like(a), where=b!=0) ) # when b = 0, division returns 0, so then tetha = 90
    H = (180/math.pi)*tetha # convert rad to degree
    H[B&gt;G]=360-H[B&gt;G]
  
    # saturation = 1 - 3*[min(R,G,B)]/(R+G+B), when R=G=B -&gt; S=0
    a = 3*np.minimum(np.minimum(R,G),B) # 3*min(R,G,B)
    b = np.add(np.add(R,G),B) # (R+G+B)
    S = np.subtract(1, np.divide(a,b,out=np.ones_like(a),where=b!=0))
  
    # intensity = (1/3)*[R+G+B]
    I = (1/3)*np.add(np.add(R,G),B)
  
    return np.dstack((H, zmax*S, np.round(zmax*I))) # values between [0,360], [0,255] e [0,255]
  </code></pre>
  
  <p><strong>Conversion HSI to RGB:</strong></p>
  
  <pre><code>  def f1(I,S): # I(1-S)
      return np.multiply(I, np.subtract(1,S))
    def f2(I,S,H): # I[1+(ScosH/cos(60-H))]
      r = math.pi/180
      a = np.multiply(S, np.cos(r*H)) # ScosH
      b = np.cos(r*np.subtract(60,H)) # cos(60-H)
      return np.multiply(I, np.add(1, np.divide(a,b)) )
    def f3(I,C1,C2): # 3I-(C1+C2)
      return np.subtract(3*I, np.add(C1,C2))
  
    def hsi_to_rgb(img):
      zmax = 255 # max value
      # values between[0,360], [0,1] and [0,1]
      H = img[:,:,0]
      S = np.divide(img[:,:,1],zmax,dtype=np.float)
      I = np.divide(img[:,:,2],zmax,dtype=np.float)
  
      R,G,B = np.ones(H.shape),np.ones(H.shape),np.ones(H.shape) # values will be between [0,1]
      # for 0 &lt;= H &lt; 120
      B[(0&lt;=H)&amp;(H&lt;120)] = f1(I[(0&lt;=H)&amp;(H&lt;120)], S[(0&lt;=H)&amp;(H&lt;120)])
      R[(0&lt;=H)&amp;(H&lt;120)] = f2(I[(0&lt;=H)&amp;(H&lt;120)], S[(0&lt;=H)&amp;(H&lt;120)], H[(0&lt;=H)&amp;(H&lt;120)])
      G[(0&lt;=H)&amp;(H&lt;120)] = f3(I[(0&lt;=H)&amp;(H&lt;120)], R[(0&lt;=H)&amp;(H&lt;120)], B[(0&lt;=H)&amp;(H&lt;120)])
  
      # for 120 &lt;= H &lt; 240
      H = np.subtract(H,120)
      R[(0&lt;=H)&amp;(H&lt;120)] = f1(I[(0&lt;=H)&amp;(H&lt;120)], S[(0&lt;=H)&amp;(H&lt;120)])
      G[(0&lt;=H)&amp;(H&lt;120)] = f2(I[(0&lt;=H)&amp;(H&lt;120)], S[(0&lt;=H)&amp;(H&lt;120)], H[(0&lt;=H)&amp;(H&lt;120)])
      B[(0&lt;=H)&amp;(H&lt;120)] = f3(I[(0&lt;=H)&amp;(H&lt;120)], R[(0&lt;=H)&amp;(H&lt;120)], G[(0&lt;=H)&amp;(H&lt;120)])
  
      # for 240 &lt;= H &lt; 360
      H = np.subtract(H,120)
      G[(0&lt;=H)&amp;(H&lt;120)] = f1(I[(0&lt;=H)&amp;(H&lt;120)], S[(0&lt;=H)&amp;(H&lt;120)])
      B[(0&lt;=H)&amp;(H&lt;120)] = f2(I[(0&lt;=H)&amp;(H&lt;120)], S[(0&lt;=H)&amp;(H&lt;120)], H[(0&lt;=H)&amp;(H&lt;120)])
      R[(0&lt;=H)&amp;(H&lt;120)] = f3(I[(0&lt;=H)&amp;(H&lt;120)], G[(0&lt;=H)&amp;(H&lt;120)], B[(0&lt;=H)&amp;(H&lt;120)])
  
      return np.dstack( ((zmax*R) , (zmax*G) , (zmax*B)) ) # values between [0,255]
  </code></pre>