Hello OpenCV Forum,
I am pretty new with OpenCV and somewhat serious python software and I need your help. I am using it for simple island detection/labeling for my project involving generating CNC Milling Machine Tool Paths. The images that I am processing are created using OpenGL shaders, analyzing imported 3D meshes. The resolution of these images is arbitrary based on the size of the models imported and a scalar value. I am using a shader to isolate the pixels in the image that represent material that needs to be cut, and then feeding it to the code below:
no_alpha = cv2.cvtColor(island_data, cv2.COLOR_RGBA2RGB)
img = no_alpha.copy()
#cv2.imshow("Image", no_alpha)
#cv2.waitKey()
for color in range(1, 255):
seeds = np.argwhere(img[:, :, 2] > 250)
if seeds.size > 0:
seed_coord = seeds[0]
else:
print("gaben")
break
#try:
# seed_coord = next(i for i, val in np.ndenumerate(img[:, :, 2]) if val > 250)
#except:
# break
print(f"Seed Coord: {seed_coord}")
floodval = (0, color, 0)
lower_range = np.array([0, color, 0])
upper_range = np.array([0, color, 0])
print(img[seed_coord[0], seed_coord[1]])
cv2.floodFill(img, None, seedPoint=seed_coord, newVal=floodval)
print(img[seed_coord[0], seed_coord[1]])
mask = cv2.inRange(img, lower_range, upper_range)
print(seed_coord)
cv2.imshow("image", img)
cv2.waitKey()
cv2.imshow("image", mask)
cv2.waitKey()
This program is also available on Github. Here is branch and specific file I am working in if you need more context: pyThunder_Path/computeWorker.py at island_detection · Thorhian/pyThunder_Path · GitHub
I am having a couple of problems so far. The first is what is mentioned in the title. I’m trying to flood fill each island a different green color value (yes I am aware of the limit of 255). I look for a blue pixel in the image (or red pixel if you assume BGR, OpenGL uses RGB in my case). If one is found, I use it as a seed value for the floodfill. If one isn’t found, I break out of the for loop. I am going to save the masks generated by inRange after the flood fill is performed, but I haven’t done that yet since I’ve been debugging this for a while. For one of my test models, I enter this loop, and it finds, floods, and generates a good mask for the first island. The second iteration does the same thing. However, once I reach the third iteration, the seed coordinate never changes and continues to flood fill the same island from the second iteration over and over until the for loop maxes out at 255. I have checked for whether the image array and the seed array are writable by printing out their flags, and the image array happily lets the flood fill recolor that one island, and I’ve tried multiple ways of finding the first blue pixel in the image, but it keeps exhibiting this behavior. I’m at a loss.
For another model however, It errors out on the second iteration, floodfill stating that the seed point is outside of the image, when in reality it is a valid pixel (It can be accessed and prints out a sane pixel value before running the flood fill).
I just checked a different case where there is only one island, and it actually breaks out of the loop after flooding and masking that island. I have been trying to figure this out for many hours now, so I figured I should try asking people who know a lot more about OpenCV and Python than I do. I would be extremely grateful if this community could help me solve these issues. I am unsure if I am allowed to share files as a new person to the forum though.
If you want to try running it, I use pipenv to manage the virtual environment and dependencies. After installing the deps, the main.py file can be ran like this:
./main.py /path/to/target/CAD/model.stl /path/to/stock/model.stl 8 9.525
The first stl file is the model that the program is trying to create, and the second stl file is a model that represents the block of material that will be cut in real life to form the target model. The first number after the models is a depth of cut, and the second number is the diameter of the tool, all in millimeters.
Also, I am developing on Arch Linux if that means anything to you guys.
Sorry for the long post,
Thorhian