How can i find the pixel color range in an image that excludes outliers?
<p>I have this code that randomly generates 10 pixel locations with in the image dimensions. I then want to take each pixel location and find the GBR value and then find the largest g value, b value, r value and the smallest g value b value and r value. I then found some code that can take those color boundaries and create a mask image. I might want to incorporate  a mean g, b , r value and compute the standard deviation for each and use that to eliminate the outliers. </p>

<p>or instead of using the code i wrote to generate 10 random pixels if there a way to get the color range inside of +- 1 standard deviation of the mean color of the image? </p>

<p>see code below:</p>

<pre><code>    import cv2
import numpy as np
import random

name = "Highway"

img = cv2.imread(name + ".jpg")
inc = 10
n = 10
rpixel=[]

# grabs bottom center of image, keeps image ratio. the larger the inc is the smaller the imaged grabed

def bottomcenter(inc,img):

    y,x,z = img.shape
    h = x/2
    x1 = h - (x/inc)
    x2 = h + (x/inc)
    y1 = y - (y/inc)
    y2 = y 

    bcsample = img[y1:y2, x1:x2]

    return(bcsample)


# generates a random selection of pixels

def randompixel(img,n):

    y,x,z = img.shape

    for i in range (n):
        xrand = random.randrange(0,x)
        yrand = random.randrange(0,y)
        rpix = yrand,xrand
        rpixel.append(rpix)
    return(rpixel)



roadsample = bottomcenter(inc,img)

randompixel(roadsample,n)




for i in rpixel:

    #px=B.G.R   
    px = roadsample[i]

    for b,g,r in px: 
        ub = max(b)
        ug = max(g)
        ur = max(r)
        lb = min(b)
        lg = min(g)
        lr = min(r)

boundary = [([lb,lg,lr],[ub,ug,ur])]

print(boundary)

#for (lower, upper) in boundary:
    # create NumPy arrays from the boundaries
#   lower = np.array(lower, dtype = "uint8")
#   upper = np.array(upper, dtype = "uint8")

# find the colors within the specified boundaries and apply
# the mask
#mask = cv2.inRange(img, lower, upper)
#output = cv2.bitwise_and(img, img, mask = mask)

# save the images
#cv2.imwrite(name + "_roadarea.png", np.hstack([img, output]))
</code></pre>