How to find the first biggest sqaure in image (Without external libraries)

81 views Asked by At

So, I have different images and I have to run on the image and find the first biggest square on the picture by the color that given as variable in the function. (That means if there are for example 2 square with the same amount of pixels so the first square is the chosen)

It's a mission that I have to do but I'm stuck and don't know how to get to the result :(

We using the function load from the file immagini.

filename = the image name

c = the RGB code.

my code is:

from immagini import *

def quadrato(filename,c):
#Get image from the user (load image)    
img = load(filename)

#print(img[y][x][r,g,b]) 
#Loop trough the image 
#Get the width and height of the image 

counter = []

for y in range(0, len(img[:])):
    for x in range(0, len(img[y][:])):
        #Scan for the R #Scan for the G #Scan for the B
        #If detecs all RGB are the same from the input
        if img[y][x][0] == c[0] and img[y][x][1] == c[1] and img[y][x][2] == c[2]:
            counter.append((x,y))
print (((len(counter)), (x ,y),img[y][x]))

So my code prints how many pixels with the requested color,the location of the pixel and his color.

for example (The number of pixels of the sqaure , (x,y) location)

The input ('Img1.png',(255,0,0)) , The output must be (30, (60, 50))

The input ('Img2.png',(0,0,255)) , The output must be (201,(54,240))

Thank you guys, I hope you understand me,it's a little hard for me to explain, If there is a problem in the post,tell me :)

1

There are 1 answers

0
Michael Butscher On

You should not think of single pixels but of horizontal lines at first.

For each y create a list of connected lines. If you find a pixel in searched color and the pixel to the left has same color, add found pixel to the last line.

Then go through y to find adjacent lines of same begin and end to build rectangles out of it.