user3592221
user3592221

Reputation: 71

Study a portion of the image and not the white background

Disclaimer: This question is related to an homework project.

I am working on an application to study a banana and tell if it is ripe or not. I have already created a window that allow to open an image, and open a popup saying if clicked pixel are ripe or not. I convert pixel rgb values to hsv and diagnose whether it is ripe or not.

snapshot of application

Now, I would like to provide an automated analysis of the whole picture (in analyseauto method). The problem is that I want studying banana pixels only, and not white. My question is how I can select just a part of pixels, so the banana and not the white background. Can I do that with getdata ?

Here are relevant piece of code.

def analyseauto():
    global image
    listTriplets=list(image.getdata()) # list of rgb values
    print(listTriplets)

#click callback
def pointeur(event):
    global image

    x=str(event.x)
    y=str(event.y)
    x1=int(x)
    y1=int(y)

    R,V,B=image.getpixel((x1,y1)) 

    #convert to HSV (my T hold hue, from french "teinte")
    #[...]
    #update side text field
    #[...]

    Mûre = 0
    PasMûre = 0
    Pourri = 0
    AnalyseManuelle = 0
    PixelNoBanane = 0
    x2=0

    #diagnose
    # m value (between 1 and 4) record pixel state
    if 0<=T<=360 and 0<=S<=100 and 0<=V<=100:
        if 40<= T <=56 and 50<= S <=100 and 70<= V <=100:
            print("Il s'agit d'un pixel Mûre ")
            Mûre = Mûre+1
            x2 = x2+1
            m=1
            m=int(m)

        if 50<= T <=100 and 70<= S <=100 and 20<= V <=70:
            print("Il s'agit d'un pixel pas Mûre")
            PasMûre = PasMûre+1
            x2 = x2+1
            m=2
            m=int(m)

        if 0<= T <=30 and 70<= S <=100 and 10<= V <=50:
            print("Il s'agit d'un pixel Pourri")
            Pourri = Pourri+1
            x2 = x2+1
            m=3
            m=int(m)

    if x2 == 0 :
        print ("le pixel n'est pas compris dans les echelles definis")
        PixelNoBanane = PixelNoBanane+1
        m=4
        m=int(m)

    # display popup with plain text answer

Full application code available on gist. Example of banana I use for testing: .

Upvotes: 0

Views: 212

Answers (1)

atlasologist
atlasologist

Reputation: 3964

I think using getdata is an acceptable way of doing what you want, but I think you should resize your image first. getdata will return a list, but at the current image size:

>>> banana = Image.open('banana.jpg')
>>> banana
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=847x567 at 0x2C26440>

That list will have pixel values of 480,249 pixels, which might be a little overkill.

If you resize it to 50x50:

>>> banana = banana.resize((50,50))

Then use getdata:

>>> banana = banana.resize((50,50))
>>> pixels = banana.getdata()
>>> len(pixels)
2500
>>> pixels = set(pixels) # remove duplicates
>>> len(pixels)
670

That seems more manageable. Then, just dump the (255,255,255) value from the set, and maybe any light gray colors, and get the average value to represent the color you want to test, and run your test as before (on just a single pixel's RGB value).

Hope I understood right and this helps.

Upvotes: 1

Related Questions