Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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()

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()
cv2.destroyAllWindows()[C:\fakepath\dices.jpg](/upfiles/15412603828832715.jpg)

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()[C:\fakepath\dices.jpg](/upfiles/15412603828832715.jpg)
cv2.destroyAllWindows()

image description