Hey sorry for the late reply, but I had some issues.
So here are some intermediates for the initial code:
For the failure case I’ve got this image with the contours draw (they have white border) :
And these are the corresponding areas of them.
0.0
0.0
2.5
0.0
0.0
0.0
11.5
19.0
10.0
0.0
27.5
22.5
9.0
0.0
15.0
21.5
38.0
35.5
5.5
0.0
2.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
4.0
0.0
455.0
2.0
0.0
0.0
11.0
0.0
7.0
0.0
0.0
0.0
0.0
0.0
0.0
11.0
2.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
18556.0
16.0
2.0
12.0
2.0
2.0
2.0
2.0
4.0
2.0
2.0
2.0
7.0
7.0
2.0
2.0
2.0
5.5
2.0
2.0
4.0
2.0
4.0
5.5
4.0
5.5
2.0
4.0
2.0
4.0
2.0
6.0
2.0
2.0
2.0
38.0
2.0
2.0
441.5
0.0
0.0
2.0
2.0
24.0
4.0
0.0
The same with the successful one:
Contours found:
Contour areas:
0.0
10.0
0.0
0.0
0.0
49.5
4.0
0.0
0.0
0.0
28.0
2.0
0.0
0.0
0.0
18.5
125.5
13.0
0.5
0.0
1958.0
2.0
2.0
0.0
1667.5
10.0
1947.5
2.0
2.0
2.0
1879.0
2.0
18.0
2.0
2.0
4.0
8.0
2.0
2014.0
0.5
0.0
1.5
6.5
198.5
183.5
2.0
0.0
0.0
0.5
0.0
0.0
4.5
2117.5
6.0
2.0
0.0
0.0
4.0
1.5
1.5
0.5
1.5
11.0
273.5
12.0
22.0
7.0
4.0
7.0
0.5
20.0
45.5
8.5
54.5
It seems that something is going but with the contour finding and then this problem is transferred to the function where I’m filtering according to their area.
I’m wondering, what do you need these rectangles/ROIs for?
I’m just use the content of them for some RGB analysis.
P.S I’m going to test your alternative proposition and come back. Cheers.
EDIT:
@crackwitz , I tried your method but apparently I’m missing something. I turned the image into HSV color space and then created a plot of the color pixel to see how they separate from each other. From that plot, then I chose with a color picker a light blue and a dark blue. I turned them into HSV values here and then used the inRange() method to create a mask. After applying the mask to the original image I just got a total black.
Below is the code as well and the output images.
for image_file in input_files:
image = cv2.imread(images_path + '/' + image_file)
hsv_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
showImage(hsv_image)
# Create a graph to select the color values
h, s, v = cv2.split(hsv_image)
fig = plt.figure()
axis = fig.add_subplot(1, 1, 1, projection="3d")
pixel_colors = image.reshape((np.shape(image)[0]*np.shape(image)[1], 3))
norm = colors.Normalize(vmin=-1.,vmax=1.)
norm.autoscale(pixel_colors)
pixel_colors = norm(pixel_colors).tolist()
axis.scatter(s.flatten(), h.flatten(), v.flatten(), facecolors=pixel_colors, marker=".")
axis.set_xlabel("Hue")
axis.set_ylabel("Saturation")
axis.set_zlabel("Value")
plt.savefig("mygraph.png")
# Get a range of selected colors
dark_blue = (220.0000, 75.0000, 45.4902)
light_blue = (209.2500, 46.5116, 67.4510)
mask = cv2.inRange(hsv_image, light_blue, dark_blue)
result = cv2.bitwise_and(image, image, mask=mask)
plt.subplot(1, 2, 1)
plt.imshow(mask, cmap="gray")
plt.subplot(1, 2, 2)
plt.imshow(result)
plt.savefig("bluemask.png")
The dark blue that I selected is the #1d3a74 while the light one is #5c85ac.
Here is the 3D plot of the color separation:
https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/0/021ea47d4e4005dd2c598b7fae0e5598a29f4251.png
And this is what I get after applying the mask
https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/5/5eaa9a2ed647bdecb1f50b18b61545291546b917.png
Sorry for that link but forum does not allow me to add more than two link.
