我正在尝试从照片中识别卡片.我设法做了我想要的理想照片,但我现在很难应用相同的程序,稍微不同的照明等.所以问题是关于使以下轮廓检测更健壮.
我需要分享我的代码的大部分内容,以便能够制作感兴趣的图像,但我的问题只涉及最后一个块和图像.
import numpy as np
import cv2
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import math
img = cv2.imread('image.png')
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
plt.imshow(img)
Run Code Online (Sandbox Code Playgroud)
然后检测到卡片:
# Prepocess
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(1,1),1000)
flag, thresh = cv2.threshold(blur, 120, 255, cv2.THRESH_BINARY)
# Find contours
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea,reverse=True)
# Select long perimeters only
perimeters = [cv2.arcLength(contours[i],True) for i in range(len(contours))]
listindex=[i for i in range(15) if perimeters[i]>perimeters[0]/2]
numcards=len(listindex)
# Show image
imgcont …
Run Code Online (Sandbox Code Playgroud) 定大小的ndarray (n, 3)
有n
1000左右,如何共同乘以每一行的所有元素,快?下面的(不优雅的)第二种解决方案在大约0.3毫秒内运行,可以改进吗?
# dummy data
n = 999
a = np.random.uniform(low=0, high=10, size=n).reshape(n/3,3)
# two solutions
def prod1(array):
return [np.prod(row) for row in array]
def prod2(array):
return [row[0]*row[1]*row[2] for row in array]
# benchmark
start = time.time()
prod1(a)
print time.time() - start
# 0.0015
start = time.time()
prod2(a)
print time.time() - start
# 0.0003
Run Code Online (Sandbox Code Playgroud)