Hi, sylvain_tymop.
I made a simple example and I’m going to show you the possibilities.
Of course, the quality is not perfect.
And the result is just an image of objects.
The location and number of objects are not known.
After that, you can find the number of Contours using FindContours().
You need to use momentum to locate it.
I also think it is necessary to bring objects from a close distance together for split objects.
I hope you achieve the results you want.
Best Regard,
PS) Example, There was a mistake in color conversion, so I corrected it.
import numpy as np
import cv2
imgSRC = cv2.imread(“D:\Resources\Images\Color Pattern\Color Objects.png”)
if (imgSRC is None):
print(“Oops! Image not loaded.”)
exit()
maxy, maxx = imgSRC.shape[:2]
if (maxx == 0):
print(“Oops! Image not loaded.”)
exit()
imgMSK = np.zeros((maxy, maxx, 3), np.uint8)
imgHLS = cv2.cvtColor(imgSRC, cv2.COLOR_BGR2HLS)
sLow = 128
sHi = 255
lLow = 64
lHi = 180
for y in range(maxy):
for x in range(maxx):
H = imgHLS[y, x][0]
L = imgHLS[y, x][1]
S = imgHLS[y, x][2]
if (sLow <= S and S <= sHi):
if (lLow <= L and L <= lHi):
imgMSK[y, x][0] = 255
imgMSK[y, x][1] = 255
imgMSK[y, x][2] = 255
kernel = np.ones((3, 3), np.uint8)
imgMSK = cv2.erode(imgMSK, kernel, 1)
imgMSK = cv2.dilate(imgMSK, kernel, 3)
imgMSK = cv2.erode(imgMSK, kernel, 2)
imgRST = cv2.bitwise_and(imgHLS, imgMSK)
imgBGR = cv2.cvtColor(imgRST, cv2.COLOR_HLS2BGR)
cv2.imshow(“SRC”, imgSRC);
cv2.imshow(“RESULT”, imgBGR);
cv2.waitKey(0)
cv2.destroyAllWindows()