2

この画像に python.I を使用して何枚のカードが存在するかを検出したいのですが、白いピクセルを試してみましたが、正しい結果が得られませんでした。ここに画像の説明を入力

私のコードは以下のとおりです。

import cv2
import numpy as np

img = cv2.imread('imaagi.jpg', cv2.IMREAD_GRAYSCALE)
n_white_pix = np.sum(img == 255)
print('Number of white pixels:', n_white_pix)

私は初心者です。そのため、道を見つけることができません。

4

2 に答える 2

3

このソリューションは、提供したイメージに関するものであり、実装は OpenCV にあります。

コード:

im = cv2.imread('C:/Users/Jackson/Desktop/cards.jpg', 1)

#--- convert the image to HSV color space ---
hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
cv2.imshow('H', hsv[:,:,0])
cv2.imshow('S', hsv[:,:,1])

#--- find Otsu threshold on hue and saturation channel ---
ret, thresh_H = cv2.threshold(hsv[:,:,0], 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
ret, thresh_S = cv2.threshold(hsv[:,:,1], 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

#--- add the result of the above two ---
cv2.imshow('thresh', thresh_H + thresh_S)

#--- some morphology operation to clear unwanted spots ---
kernel = np.ones((5, 5), np.uint8)
dilation = cv2.dilate(thresh_H + thresh_S, kernel, iterations = 1)
cv2.imshow('dilation', dilation)

#--- find contours on the result above ---
(_, contours, hierarchy) = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

#--- since there were few small contours found, retain those above a certain area ---
im2 = im.copy()
count = 0
for c in contours:
    if cv2.contourArea(c) > 500:
        count+=1
        cv2.drawContours(im2, [c], -1, (0, 255, 0), 2)

cv2.imshow('cards_output', im2)
print('There are {} cards'.format(count))

結果:

私が得た端末で:There are 6 cards

ここに画像の説明を入力

于 2018-07-18T13:21:03.737 に答える