# Color filtering of neighboring points

Good time of day, everyone! I’m an architecture final-year student and my task at the moment is to “filter” set of points and give them a predominant (>=2 out of 4) color value from their neighbors.

Deeper, my task is to make something like “image segmentation” without resorting to the training process and / or manual separation of the image (conditionally a building) into components (by color). Therefore, the above process should have ‘n’ number of iterations until the maximum result.

I have several years of experience in using grasshopper, but after a couple of attempts to do it in the “classic” way, I figured out that even at the first steps the computing components need several minutes, this is unacceptable … I assume that the best (only adequate) solution in this case is the coding, which, alas is beyond my knowledge…
In my desperation, I even turned to ChatGPT and got the following code:

``````import rhinoscriptsyntax as rs
import Rhino
import System.Drawing

def check_neighbors(point, color, iteration, points, colors):
neighbors = []
for p in points:
if rs.Distance(point, p) < iteration:
neighbors.append(p)

neighbor_colors = [colors[points.index(p)] for p in neighbors]
color_counts = {col:neighbor_colors.count(col) for col in neighbor_colors}
if max(color_counts.values()) >= 2:
return max(color_counts, key=color_counts.get)
return color

def update_colors(points, colors, iterations):
new_colors = []
for i, point in enumerate(points):
new_colors.append(check_neighbors(point, colors[i], iterations, points, colors))
return new_colors

def main(Pt, C, It):
points = [rs.coerce3dpoint(pt) for pt in Pt]
colors = [System.Drawing.Color.FromArgb(*c) for c in C]
for i in range(It):
colors = update_colors(points, colors, i+1)
return [[c.R, c.G, c.B] for c in colors]

``````

But there is no result unfortunately 