Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dice detection using DBSCAN
#5
It's been a long time! I got involved in a house renovation project here in Barbate, don't have time for anything lately!

I tried to do this with my own functions, but when I tried them on real photos, noise in the photos made that impossible!

Not sure how to post images here, but you can try this with your pictures. You just need to tweak the numbers to suit your images.

# bilateral filter preserves edges
# cv.bilateralFilter( src, d, sigmaColor, sigmaSpace[, dst[, borderType]] ) -> dst

I just put 2 dice together and took a photo with my mobile, then I cropped it so I just have the 2 dice and a small area around them.

import cv2 as cv

# a font for writing the number on the dice image
font = cv.FONT_HERSHEY_SIMPLEX
destination = 'cv2/images/steps/'
name = input('Enter the name of the picture you want to analyse ... ')
# write on this image when we have the contours
original_image = cv.imread(destination + name)
original_image.shape # (963, 1592, 3)
savename = 'final_output.png'

# get the image as greyscale
image = cv.imread(destination + name, cv.IMREAD_GRAYSCALE)

# have a look at the grey image
cv.imwrite(destination + 'grey.png', image)

# make the image black and white
# bilateral filter preserves edges
# cv.bilateralFilter(	src, d, sigmaColor, sigmaSpace[, dst[, borderType]]	) ->	dst
# https://docs.open# cv.org/3.4/d4/d86/group__imgproc__filter.html#ga9d7064d478c95d60003cf839430737ed
# the docs may be old tweak parameters to suit
#blur = cv.bilateralFilter(image,9,75,75)
blur = cv.bilateralFilter(image,5,75,75)
cv.imwrite(destination + 'blur.png', blur)
ret,thresh = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
cv.imwrite(destination + 'thresh.png', thresh) # some noise may be left
# now get clean contours
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
len(contours) # may not be correct may contain noise contours
    
# draw all the contours on the original image:
cv.drawContours(original_image, contours, -1, (0,255,0), 5)
cv.imwrite(destination + 'contours.png', original_image)

# have a look at the areas within the contours
for i in range(len(contours)):
    # Calculate contour area
    area = cv.contourArea(contours[i])
    print(f'The area of this contour is: {area}')

# by first looking at areas output you can see each die is about 370000+ pixels
# a contour with that area must be a die
areas = [cv.contourArea(contours[i]) for i in range(len(contours))]

# a dictionary to take the numbers
# the key is the area of each die
d = {num:[] for num in areas if num > 360000} # tweak the number to suit your pictures
# d = {376399.5: [], 382368.5: []}

# assign the spots to the dice
# the actual number on each die will be the length of d[key]
for num in areas:    
    if num > 360000:
        key = num
        continue
    # small imperfections cause small contours, filter them out
    # adjust the numbers to suit
    elif num < 55500 and num > 14500:
        d[key].append(num)
        
total = 0
for i in range(len(contours)):
    # Calculate contour area
    area = cv.contourArea(contours[i])
    print(f'The area of this contour is: {area}')
    if area > 310000:
        M = cv.moments(contours[i])
        centroid_x = int(M['m10'] / M['m00'])
        centroid_y = int(M['m01'] / M['m00'])
        dice_centre = (centroid_x, centroid_y)
        number = len(d[area])
        total = total + number
        print('\n***********************************************')
        print(f'The total value so far of the throw is {total}')
        print('\n***********************************************')
        cv.putText(original_image, str(number), dice_centre, font, 8,(0,255,0, 255),5,cv.LINE_AA)
        
print(f'Saving png as {destination + savename} ... ')
cv.imwrite(destination + savename, original_image)
Vrolijk kerstfeest!
Reply


Messages In This Thread
Dice detection using DBSCAN - by Termiik - Oct-15-2024, 09:50 PM
RE: Dice detection using DBSCAN - by Pedroski55 - Oct-17-2024, 03:06 PM
RE: Dice detection using DBSCAN - by Termiik - Oct-29-2024, 10:07 PM
RE: Dice detection using DBSCAN - by Pedroski55 - Oct-30-2024, 06:49 PM
RE: Dice detection using DBSCAN - by Pedroski55 - Dec-19-2024, 06:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Making a percentile dice roller and dice roller Fixer243 2 4,108 Sep-30-2018, 12:18 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020