Ask Your Question

saj's profile - activity

2020-10-03 04:31:15 -0600 received badge  Student (source)
2018-11-03 11:24:00 -0600 marked best answer 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

2018-11-03 11:24:00 -0600 received badge  Scholar (source)
2018-11-02 11:30:05 -0600 commented question Odd values of the dices did not return the centroid on python opencv

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

2018-11-02 10:21:29 -0600 edited question Odd values of the dices did not return the centroid on python opencv

Odd values of the dices did not return the centroid on python opencv Hi. I have made an openCV code that determine the c

2018-11-02 10:20:33 -0600 received badge  Editor (source)
2018-11-02 10:20:33 -0600 edited question Odd values of the dices did not return the centroid on python opencv

Odd values of the dices did not return the centroid on python opencv Hi. I have made an openCV code that determine the c

2018-11-02 10:20:13 -0600 asked a question Odd values of the dices did not return the centroid on python opencv

Odd values of the dices did not return the centroid on python opencv Hi. I have made an openCV code that determine the c

2018-09-13 00:41:13 -0600 asked a question (X,Y) coordinate using python openCV

(X,Y) coordinate using python openCV Dear all, It is my first time to work on openCV; in fact, I just heard of it two mo