Fastest way to find a 3X3 square of intigers in a string

72 Views Asked by At

I have a string with the length of 480000 , it represents a 800x600 frame. meaning 600 rows and 800 columns. i need to find a square of 3X3 were all the 9 values are in a specific range. i have the range itself. and return the index of the middle.

this is what i am doing right now:

    # find 3x3  minValue < square < maxVlaue
    for row in range(598):
        for col in range(798):
            if checkRow(frame,row,col,minValue,maxValue): 
                if checkRow(frame,row+1,col,minValue,maxValue):
                    if checkRow(frame,row+2,col,minValue,maxValue):
                    string = str(row+1),str(col+1)
                    print string
                    return 


def checkRow(Frame,row,col,minValue,maxValue):
    if (Frame[row*800+col])>minValue) and (Frame[row*800+col])<maxValue):
        if (Frame[row*800+col+1])>minValue) and (Frame[row*800+col+1])<maxValue):
            if (Frame[row*800+col+2])>minValue) and (Frame[row*800+col+2])<maxValue):
                return True 
    return False

this way i check each "pixel" until the square is found.

is there some kind of special function in python for this job? or maybe a faster and more efficient algorithm?

i thought about checking a pixel every 2 columns and skipping 2 rows. this way i can make sure i wont miss the 3x3 square, but this way when i hit a pixel , i need to check 9 different options for the square itself. so i am not sure how much faster it will be.

thanks

0

There are 0 best solutions below