Ask Your Question
1

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

asked 2018-11-02 10:20:13 -0600

saj gravatar image

updated 2018-11-02 10:21:29 -0600

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

edit retag flag offensive close merge delete

Comments

2

Smells like a commercial project. Are you willing to pay for help?

sjhalayka gravatar imagesjhalayka ( 2018-11-02 10:39:16 -0600 )edit

die würfel sind gezinkt ...

berak gravatar imageberak ( 2018-11-02 11:09:03 -0600 )edit

It's a senior design for mechatronics engineering, and no thanks, I am not paying for help :)

saj gravatar imagesaj ( 2018-11-02 11:30:05 -0600 )edit

Kk, just checking. :) Let me take a look at your source code for a while

sjhalayka gravatar imagesjhalayka ( 2018-11-02 14:49:09 -0600 )edit

Die <-- one. Dice <- two or more. Dices is not a word.

sjhalayka gravatar imagesjhalayka ( 2018-11-02 17:02:51 -0600 )edit

@saj Do you want to count number of dices on board?

supra56 gravatar imagesupra56 ( 2018-11-03 08:47:04 -0600 )edit

@saj. Are you willing to pay for help?

supra56 gravatar imagesupra56 ( 2018-11-03 09:28:09 -0600 )edit

I think they'll not pay... the impression I get is that it's for engineering school.

sjhalayka gravatar imagesjhalayka ( 2018-11-04 14:45:10 -0600 )edit

2 answers

Sort by » oldest newest most voted
1

answered 2018-11-03 09:25:25 -0600

supra56 gravatar image

updated 2018-11-03 10:55:04 -0600

OK! I solved it.

import cv2
import numpy as np

img = cv2.imread('die.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = 225  
ret, thresh = cv2.threshold(gray, thresh, 255, cv2.THRESH_BINARY)

_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img2 = img.copy()
index = -1
thickness = 2
color = (255,255,255)

cv2.drawContours(img2, contours, index, color, thickness)
print(img.size)

objects = np.zeros([img.shape[0], img.shape[1], 3], 'uint8')
row,col,channels=objects.shape

cx = 0
cy = 0
for c in contours:
    cv2.drawContours(objects, [c], -1, color, -1)
    area = cv2.contourArea(c)
    perimeter = cv2.arcLength(c, True)
    M = cv2.moments(c)
    if area >= 200 and area <= 100000: #>=660, <=100000
        if M['m00'] != 0:
            cx = int(M['m10']/M['m00'])
            cy = int(M['m01']/M['m00'])
    cv2.circle(objects,(cx, cy), 4, (0, 0, 255 ), -1)
    print('Area: {}, preimeter: {}'.format(area,perimeter))
    print('(',cx,',',cy,')')

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

image description

edit flag offensive delete link more

Comments

Right on. :D

sjhalayka gravatar imagesjhalayka ( 2018-11-04 14:35:49 -0600 )edit
0

answered 2018-11-02 17:01:11 -0600

sjhalayka gravatar image

OK. this code does not successfully draw the centroids. Where did I go wrong?

import cv2
import numpy as np

img = cv2.imread('die.jpg',0)
ret, thresh = cv2.threshold(img,225, 255,cv2.THRESH_BINARY)
cv2.imshow('Binary',thresh)

_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img2 = img.copy()
index = -1
thickness = 4
color = (255,0,255)

cv2.drawContours(img2, contours, index, color, thickness)
cv2.imshow('contours',img2)
print(img.size)

objects = np.zeros([img.shape[0], img.shape[1],3],'uint8')
row,col,channels=objects.shape

for c in contours:

    cv2.drawContours(objects, [c],-1,color,-1)

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

    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

        cx = 0
        cy = 0

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

            if M['m00'] !=0:
                cx = int(M['m10']/M['m00'])
                cy = int(M['m01']/M['m00'])

        cv2.circle(objects,(cx,cy),4,(0,127,    
        print('Area: {}, preimeter: {}'.format(area,perimeter))
        print('(',cx,',',cy,')')


print(img.size)
print(img.shape)
cv2.imshow('NContours',objects)
cv2.waitKey(0)
cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

1

Invalid syntax. Something missing in parameter.

cv2.circle(objects,(cx,cy),4,(0,127,
supra56 gravatar imagesupra56 ( 2018-11-03 05:41:59 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-11-02 10:20:13 -0600

Seen: 396 times

Last updated: Nov 03 '18