Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Odd values of the dices did not return the centroid on python opencv

Hi. I have made an openCV code that determine the centroid of the dices. however, I noticed that the centroid of the odd values of the dices did not return a the centroid value. Also, when I tried to lower the are limits shown the code, it picked the centroid of the circles. You can find my code below, and I uploaded two images to clarify what I meant.

    import cv2
import numpy as np

#img = cv2.imread('IMG_1464.jpg',0)
#img = cv2.imread('IMG_1464.jpg',0)
#img3 = cv2.imread('IMG_1464.jpg',1)
img = cv2.imread('image-2.jpg',0)
cv2.imwrite('Ngray.png',img)
img3 = cv2.imread('image-2.jpg',1)
# don't use it's bad##gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
#don't use it's bad##thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,21001,1)
thresh = 225 #225
ret, thresh = cv2.threshold(img,thresh, 255,cv2.THRESH_BINARY)
cv2.imshow('Binary',thresh)
cv2.imwrite('NBinary.png',thresh)


_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img2 = img3.copy()
index = -1
thickness = 4
color = (255,0,255)
#these three lines shows the contour
cv2.drawContours(img2, contours, index, color, thickness)
cv2.imshow('contours',img2)
cv2.imwrite('Nfunn2.png',img2)
print(img.size)
#cv2.imshow('CV Threshhold', thresh)
#cv2.imwrite('img.png',img)
objects = np.zeros([img.shape[0], img.shape[1],3],'uint8')
row,col,channels=objects.shape #this will get the rows and cols

for c in contours:
    cv2.drawContours(objects, [c],-1,color,-1)

    area = cv2.contourArea(c)
    perimeter = cv2.arcLength(c, True)
    #print(area,perimeter)

    M = cv2.moments(c)
    if area>=12000 and area<=100000: #>=660, <=100000
        value = map(lambda x: x/2, objects.shape)
        X = objects.shape[0]//2
        Y = objects.shape[1]//2


        if list(img.shape) >= [2445//2,2383]:
 #           print(list(objects.shape))

            if M['m00'] !=0:
                cx = int(M['m10']/M['m00'])
                cy = int(M['m01']/M['m00'])
            else:
                cx, cy = 0,0
       # if cx < (row/2) and cy<(col/2):


        cv2.circle(objects,(cx,cy),4,(255,0,0),-1)
        print('Area: {}, preimeter: {}'.format(area,perimeter))
        print('(',cx,',',cy,')')
        #print('(',cx,",",cy,")")
        #print(len(objects))




print(img.size)
print(img.shape)
cv2.imshow('NContours',objects)
cv2.imwrite('Nfunn.png',objects)
cv2.waitKey(0)
cv2.destroyAllWindows()

image description image description

Odd values of the dices did not return the centroid on python opencv

Hi. I have made an openCV code that determine the centroid of the dices. however, I noticed that the centroid of the odd values of the dices did not return a the centroid value. Also, when I tried to lower the are limits shown the code, it picked the centroid of the circles. You can find my code below, and I uploaded two images to clarify what I meant.

    import cv2
import numpy as np

#img = cv2.imread('IMG_1464.jpg',0)
#img = cv2.imread('IMG_1464.jpg',0)
#img3 = cv2.imread('IMG_1464.jpg',1)
img = cv2.imread('image-2.jpg',0)
cv2.imwrite('Ngray.png',img)
img3 = cv2.imread('image-2.jpg',1)
# don't use it's bad##gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
#don't use it's bad##thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,21001,1)
thresh = 225 #225
ret, thresh = cv2.threshold(img,thresh, 255,cv2.THRESH_BINARY)
cv2.imshow('Binary',thresh)
cv2.imwrite('NBinary.png',thresh)


_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img2 = img3.copy()
index = -1
thickness = 4
color = (255,0,255)
#these three lines shows the contour
cv2.drawContours(img2, contours, index, color, thickness)
cv2.imshow('contours',img2)
cv2.imwrite('Nfunn2.png',img2)
print(img.size)
#cv2.imshow('CV Threshhold', thresh)
#cv2.imwrite('img.png',img)
objects = np.zeros([img.shape[0], img.shape[1],3],'uint8')
row,col,channels=objects.shape #this will get the rows and cols

for c in contours:
    cv2.drawContours(objects, [c],-1,color,-1)

    area = cv2.contourArea(c)
    perimeter = cv2.arcLength(c, True)
    #print(area,perimeter)

    M = cv2.moments(c)
    if area>=12000 and area<=100000: #>=660, <=100000
        value = map(lambda x: x/2, objects.shape)
        X = objects.shape[0]//2
        Y = objects.shape[1]//2


        if list(img.shape) >= [2445//2,2383]:
 #           print(list(objects.shape))

            if M['m00'] !=0:
                cx = int(M['m10']/M['m00'])
                cy = int(M['m01']/M['m00'])
            else:
                cx, cy = 0,0
       # if cx < (row/2) and cy<(col/2):


        cv2.circle(objects,(cx,cy),4,(255,0,0),-1)
        print('Area: {}, preimeter: {}'.format(area,perimeter))
        print('(',cx,',',cy,')')
        #print('(',cx,",",cy,")")
        #print(len(objects))




print(img.size)
print(img.shape)
cv2.imshow('NContours',objects)
cv2.imwrite('Nfunn.png',objects)
cv2.waitKey(0)
cv2.destroyAllWindows()

image description image description

Odd values of the dices did not return the centroid on python opencv

Hi. I have made an openCV code that determine the centroid of the dices. however, I noticed that the centroid of the odd values of the dices did not return a the centroid value. Also, when I tried to lower the are limits shown the code, it picked the centroid of the circles. You can find my code below, and I uploaded two images to clarify what I meant.

    import cv2
import numpy as np

#img = cv2.imread('IMG_1464.jpg',0)
#img = cv2.imread('IMG_1464.jpg',0)
#img3 = cv2.imread('IMG_1464.jpg',1)
img = cv2.imread('image-2.jpg',0)
cv2.imwrite('Ngray.png',img)
img3 = cv2.imread('image-2.jpg',1)
# don't use it's bad##gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
#don't use it's bad##thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,21001,1)
thresh = 225 #225
ret, thresh = cv2.threshold(img,thresh, 255,cv2.THRESH_BINARY)
cv2.imshow('Binary',thresh)
cv2.imwrite('NBinary.png',thresh)


_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img2 = img3.copy()
index = -1
thickness = 4
color = (255,0,255)
#these three lines shows the contour
cv2.drawContours(img2, contours, index, color, thickness)
cv2.imshow('contours',img2)
cv2.imwrite('Nfunn2.png',img2)
print(img.size)
#cv2.imshow('CV Threshhold', thresh)
#cv2.imwrite('img.png',img)
objects = np.zeros([img.shape[0], img.shape[1],3],'uint8')
row,col,channels=objects.shape #this will get the rows and cols

for c in contours:
    cv2.drawContours(objects, [c],-1,color,-1)

    area = cv2.contourArea(c)
    perimeter = cv2.arcLength(c, True)
    #print(area,perimeter)

    M = cv2.moments(c)
    if area>=12000 and area<=100000: #>=660, <=100000
        value = map(lambda x: x/2, objects.shape)
        X = objects.shape[0]//2
        Y = objects.shape[1]//2


        if list(img.shape) >= [2445//2,2383]:
 #           print(list(objects.shape))

            if M['m00'] !=0:
                cx = int(M['m10']/M['m00'])
                cy = int(M['m01']/M['m00'])
            else:
                cx, cy = 0,0
       # if cx < (row/2) and cy<(col/2):


        cv2.circle(objects,(cx,cy),4,(255,0,0),-1)
        print('Area: {}, preimeter: {}'.format(area,perimeter))
        print('(',cx,',',cy,')')
        #print('(',cx,",",cy,")")
        #print(len(objects))




print(img.size)
print(img.shape)
cv2.imshow('NContours',objects)
cv2.imwrite('Nfunn.png',objects)
cv2.waitKey(0)
cv2.destroyAllWindows()

image description image description