我有一个python图像处理功能,它使用尝试来获取图像的主要颜色。我利用了在这里找到的功能https://github.com/tarikd/python-kmeans-dominant-colors/blob/master/utils.py
它可以工作,但是不幸的是,我不太了解它的作用,并且我了解到它np.histogram
相当慢,我应该使用cv2.calcHist
它,因为按照以下方法它的速度要快40倍:https : //docs.opencv.org/trunk/d1/db7/tutorial_py_histogram_begins .html
我想了解如何更新代码以使用cv2.calcHist
,或者更好地使用我必须输入的值。
我的功能
def centroid_histogram(clt):
# grab the number of different clusters and create a histogram
# based on the number of pixels assigned to each cluster
num_labels = np.arange(0, len(np.unique(clt.labels_)) + 1)
(hist, _) = np.histogram(clt.labels_, bins=num_labels)
# normalize the histogram, such that it sums to one
hist = hist.astype("float")
hist /= hist.sum()
# return the histogram
return hist
Run Code Online (Sandbox Code Playgroud)
该pprint
的clt
是这样的,不知道这是否有助于
KMeans(algorithm='auto', copy_x=True, init='k-means++', …
Run Code Online (Sandbox Code Playgroud) 假设我有一个具有某个维度的图像(1920, 1080, 3)
,我想将R,G,B值提取到单独的数组中R , G, B
.我试着这样做
for i in range(image.shape[0]):
for j in range(image.shape[1]):
B = np.append(B, image[i, j][0])
G = np.append(G, image[i, j][1])
R = np.append(R, image[i, j][2])
Run Code Online (Sandbox Code Playgroud)
但正如预期的那样,这是非常缓慢的,我怎样才能在内置函数中使用numpy?
我目前正在编写一个图像识别脚本,该脚本可以为我的国际象棋项目从棋盘图像中生成二维数组。然而,我发现很难找到哪些方块是空的:
到目前为止,我在应用高斯模糊后对图像使用了 Canny 边缘检测,得到了以下结果:
我使用的代码是:
sigma = 0.33
v = np.median(img)
img = cv2.GaussianBlur(img, (7, 7), 2) # we use gaussian blur on the image to make it clear.
lower = int(max(0, (1.0 - sigma) * v)) # we find the lower threshold.
upper = int(min(255, (1.0 + sigma) * v)) # we find the higher threshold.
img_edge = cv2.Canny(img, 50, 50) # we use the canny function to edge canny the image.
cv2.imshow('question', img_edge) # we show the image. …
Run Code Online (Sandbox Code Playgroud) 我有一个大小为 (513,513,3) 的分段输出图像,它携带的唯一值是 [0 200 255]。我想获得每种独特颜色下的像素总数。比如获取 0(黑色)的像素总数、200(绿色)的像素总数和 255(白色)的像素总数。
我尝试过这样的事情:
for i in range(img.shape[-1]):
mask = img[0, :, i].sum()
Run Code Online (Sandbox Code Playgroud)
但我觉得获取每种独特颜色下的像素数的方法是错误的。我是图像新手,请帮助解决这个问题。