Sorry, i dont get it…
I dont know what to use for labels and label
. I tried every return of CCwithstats
but i cant get results. With my try, i get results but work with OpenCv mask i think…
I mean the results i get make sense. They get bigger at beginning and smaller at end, as the height of the picture i sent. Ill post my code and return of one object, maybe you can help me to understand. Many lines are from internet, i just found out about CCwithstats when u told me. I understand what it does, but dont understand every single parameter. I think this is my problem.
Code:
# image
img4k = cv.imread(path_object)
img = cv.resize(img4k, dsize=(1920, 1080)) # img = img4k
imggray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imggray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)
# CC
analysis = cv.connectedComponentsWithStats(thresh, 4, cv.CV_32S)
(totalLabels, label_ids, values, centroid) = analysis
# Initialize a new image to store all the output components
output = np.zeros(imggray.shape, dtype="uint8")
# Loop through each component
for i in range(1, totalLabels):
# Area of the component
area = values[i, cv.CC_STAT_AREA]
if (area > 25000):
print('area: ', area)
componentMask = (label_ids == i).astype("uint8") * 255
output = cv.bitwise_or(output, componentMask)
# extract coordinate points
x = values[i, cv.CC_STAT_LEFT]
y = values[i, cv.CC_STAT_TOP]
w = values[i, cv.CC_STAT_WIDTH]
h = values[i, cv.CC_STAT_HEIGHT]
# bounding box
pt1 = (x, y)
pt2 = (x+ w, y+ h)
(X, Y) = centroid[i]
cv.rectangle(img,pt1,pt2, (255, 0, 255), 1)
cv.putText(img, "w={},h={}".format(w, h), (int(x),int(y)-int(h/1.6)), cv.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 1)
mask = output.copy()[y:y+h, x:x+w]
column_sums = mask.sum(axis=0)
print('column_sums:', column_sums)
cv.imshow("mask", mask)
cv.waitKey(0)
cv.imshow("Image", img)
cv.imshow("Filtered Components", output)
cv.waitKey(0)
result:
2nd component look very similar but even more elements