title: to crop the rgb image based on the pixel value
body: <p>I am having an rgb image and I want to crop it from all sides. i have tried the below code on gray scale image it works but for RGB image it doesn't.
  My code is:</p>
  <pre><code>    import cv2 
      import numpy as np
      import glob,os
      from PIL import Image
      
      inputdir = &quot;/Users/sripdeep/Desktop/Projects/3 Norm_Sub_Norm/rgb/&quot;
      outpath = &quot;/Users/sripdeep/Desktop/Projects/3 Norm_Sub_Norm/rgb_norm/&quot;
      
      #filesname = sorted(glob.glob( inputdir + &quot;*.jpg&quot;))
      
      for img1 in sorted(glob.glob( inputdir + &quot;*.jpeg&quot;)):
          
          img_path = img1.split('/')[-1]
          imgstr = img_path.split('.')[0]
          
          print(imgstr)
          im1 = cv2.imread(img1,0)
          thresh = cv2.threshold(im1, 100, 255, cv2.THRESH_BINARY)[1]
          x, y, w, h = cv2.boundingRect(im1)           #  calculates nonzero pixels 
  #        print(x,y,w,h)
  #        a tuple (x, y, w, h) with (x, y) the upper left point 
  #        as well as width w and height h of the bounding rectangle.
  
          left = (x, np.argmax(thresh[:, x]))             # 
          right = (x+w-1, np.argmax(thresh[:, x+w-1]))    # 
          top = (np.argmax(thresh[y, :]), y)              # 
          bottom = (np.argmax(thresh[y+h-1, :]), y+h-1) 
          
          print('left: {}'.format(left))
          print('right: {}'.format(right))
          print('top: {}'.format(top))
          print('bottom: {}'.format(bottom))
          
  #        cropped__img = img[y1:y2, x1:x2]
          cropped__img = thresh[top[1]:bottom[1], left[0]:right[0]]
          cropped__img = cv2.resize(cropped__img, (256,256), interpolation=cv2.INTER_LINEAR)
          
          save_fname = os.path.join(outpath, os.path.basename(imgstr)+'.jpg')
          cv2.imwrite(save_fname, cropped__img)
  </code></pre>
  <p>I want my image to be cropped like this:</p>
  <p><a href="https://i.stack.imgur.com/isgIS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/isgIS.png" alt="enter image description here" /></a></p>
  <p>But I get output is:</p>
  <p><a href="https://i.stack.imgur.com/jUDNh.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jUDNh.jpg" alt="enter image description here" /></a></p>