title: How to remove small colored pixels from image using opencv
body: <p>I am working on a project in which i have to process the image and get the rgb values of colored lined as shown in image. i detect the lines and get the values by processing the image from left to right line by line. 
  but there any colored pixel occur before the full line. then my algorithm may produce wrong output for me. </p>
  
  <p>Here is the original</p>
  
  <p><a href="https://i.stack.imgur.com/neAUQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/neAUQ.jpg" alt="enter image description here"></a></p>
  
  <p>And here is the image after running my code in which some pixels are before and after the left line:  </p>
  
  <p><a href="https://i.stack.imgur.com/eOkaK.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eOkaK.jpg" alt="enter image description here"></a></p>
  
  <p>I implement filter like median filter which removes that small pixels and noise but that also change the color values. So filters become useless for me. I want another way to do this. </p>
  
  <p>Here is my code: </p>
  
  <pre><code>import cv2
  import numpy as np
  
  
  def image_processing(img):
      msg = ''
      try:
          img = cv2.imread(img)
          # img = cv2.resize(img, (650, 120), interpolation=cv2.INTER_AREA)
          img_hsv = cv2.cvtColor(255 - img, cv2.COLOR_BGR2HSV)
      except Exception as e:
          msg += 'Unable to read image.'
      else:
          lower_red = np.array([40, 0, 0])  # lower range for red values
          upper_red = np.array([95, 255, 255])  # upper range for red values
  
          # mask on red color lines to find them
          mask = cv2.inRange(img_hsv, lower_red, upper_red)
          # original image with just red color pixels all other pixels will be set to 0(black)
          color_detected_img = cv2.bitwise_and(img, img, mask=mask)
  
          # finding the pixel where color is detected in [colored_detected_img]
          img_dimensions = color_detected_img.shape  # height &amp; width of the image
          left_line_colors = []
          right_line_colors = []
          y_color_index = 10
          x_color_index = 0
          left_line_detected = False
          right_line_detected = False
  
          # getting left line values
          while x_color_index &lt; img_dimensions[1] - 1:
              x_color_index += 1
              if color_detected_img[y_color_index, x_color_index].all() &gt; 0:  # checking if color values are not 0 (0 is black)
                  left_line_detected = True
                  for y in range(img_dimensions[0] - 1):
                      # print(y, x_color_index)
                      left_line_colors.append(
                      color_detected_img[y,x_color_index].tolist())
              elif left_line_detected:
                  break
              else:
                  continue
  
          #  ---- Getting final results of left line ----
          try:
              left_line_colors = [l for l in left_line_colors if (l[0] != 0 and l[1] != 0 and l[2] != 0)]
              # adding all the rgb list values together i.e if -&gt; [[1, 2, 3], [2, 4, 1]] then sum -&gt; [3, 6, 4]
              sum_of_left_rgb = [sum(i) for i in zip(*left_line_colors)]
              left_rgb = [int(sum_of_left_rgb[0] / len(left_line_colors)),
                      int(sum_of_left_rgb[1] / len(left_line_colors)),
                      int(sum_of_left_rgb[2] / len(left_line_colors))]
              print(left_rgb[2], left_rgb[1], left_rgb[0])
          except:
              msg += 'No left line found.'
  
          cv2.imshow("Cropped", color_detected_img)
  
          cv2.waitKey(0)
          cv2.destroyAllWindows()
  
      return msg
  
  
  print(image_processing('C:/Users/Rizwan/Desktop/example_strip1.jpg'))
  </code></pre>
  
  <p>It should give me result</p>
  
  <p><a href="https://i.stack.imgur.com/bBep8.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bBep8.jpg" alt="enter image description here"></a></p>
  
  <p>But this result image i get by implementing medianblur filter and it changes the rgb values which produce wrong result.</p>