this code finds the biggest color spot in images and returns a new image with the only spot found and the complementary color as background. My code uses recursive functions and works only for small images. i use a function that returns a list with all the coordinates of the pixels of the given color. another function modifies the Matrix and assings a boolean label (initially False). the third function return a list of tuples. in each tuple there will be another list of coordinates of the spots found and the lenght of the list.(es: list=[(400,[.........]),(100,[....])....]) this function calls a recursive function for the research of the adjacent pixels changing the boolean from False to True for avoiding to analize twice the same pixel
import immagini
def es1(fname, color, fnameout):
matrix= immagini.load(fname)
list=coordinate(matrix,color)
true_false(matrix)
dict=dictionary(matrix,list,color)
biggest_spot=max(dict)[1]
out=newIm(matrix,biggest_spot,color)
immagini.save(out,fnameout)
return len(biggest_spot)
def coordinate(matrix,color):
list=[]
for y in list(range(len(matrix))):
for x in list(range(len(matrix[0]))):
if matrix[y][x]==color:
list.append((y,x))
return list
def true_false(matrix):
for row in list(range(len(matrix))):
for col in list(range(len(matrix[0]))):
matrix[row][col]=[matrix[row][col], False]
def dicionary(matrix,list,color):
dictionary_spots=[]
for coord in list:
if matrix[coord[0]][coord[1]][0]==color and matrix[coord[0]][coord[1]][1]== False:
spot=[]
ric(matrix,coord[0],coord[1],color,spot)
dictionary_spots.append((len(spot), spot))
return dictionary_spots
def ric(matrix, row, col, color, spot):
if matrix[row][col][0]==color and matrix[row][col][1] == False:
matrix[row][col][1]=True
spot.append((row,col))
if row-1>0:
ric(matrix, row-1, col, color, spot)
if row+1<len(matrix):
ric(matrix, row+1, col, color, spot)
if col+1<len(matrix):
ric(matrix, row, col+1, color, spot)
if col-1>0:
ric(matrix, row, col-1, color, spot)
def newIm(matrix,spot,color):
out=[]
for row in list(range(len(matrix))):
row=[]
out.append(out)
for col in list(range(len(matrix[0]))):
for elem in out:
elem.append((255-color[0],255-color[1],255-color[2]))
for elem in spot:
out[elem[0]][elem[1]]=colore
return out
for small images my code works correctly but for bigger images i have this problem:
"RecursionError: maximum recursion depth exceeded in comparison"
how can I solve this problem? i can't use any library ecxept the one for loading and storing the image
Solution: Don't use recursion.
Another way to describe your problem is: I'm trying to find the largest "connected component."
Connected component labeling is a solved problem, and can be completed by making two passes over the image, where each pass iterates left-to-right across rows and then top-to-bottom for set of rows.
Here is a brief description of the algorithm (wiki is linked here):
In the first pass, you naively assign labels to each of the pixels based on the pixels to the North and the West of the current pixel (creating a new label if the values don't match). You also mark equivalences if the North and West pixels match in value but don't match by label.
In the second pass, you use the equivalencies you found in the first pass to change the label of pixels with equivalencies to the minimum label in the equivalency (this may be recursive). This will reduce the total number of labels in your image.
Once you have a labeled image, you can make a single pass to count the number of pixels in each connected component, and a second pass to change the pixels in components that aren't the largest.