我对图像处理有一个疑问,我被要求编程以去除图像的背景色,以便仅保留前景对象,但是,如果我想使用它,我将完全不知道如何启动它JavaScript,有什么提示吗? 还是我应该使用更好的编码语言? 干杯。 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在使用https://www.pyimagesearch.com/2014/05/26/opencv-python-k-me-color-clustering/中的出色代码(这不是我的!)。
我要解决的是,在绘制颜色时要删除某些颜色-在这种情况下,我要在绘制直方图时去除/不测量玩具的白色背景。 在其他情况下,当使用透明背景时,它会拉动灰色/白色选中的背景(例如https://i.stack.imgur.com/WLjsK.png ),因此我想同时移动这两种“颜色”。
有人可以请我朝着解决这个问题的正确方法前进吗?
非常感谢!!
import cv2
import numpy as np
from sklearn import metrics
import matplotlib.pyplot as plt
!wget "https://www.beatrix-potter-shop.co.uk/app/uploads/2017/07/PO1424_Benjamin.jpg"
# Load the image
image = cv2.imread("PO1424_Benjamin.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Resize it for efficiency (color ratios are the same)
h, w, _ = image.shape
w_new = int(100 * w / max(w, h) )
h_new = int(100 * h / max(w, h) )
image = cv2.resize(image, (w_new, h_new));
# Step 1: Clustering colours with K-Means
from sklearn.cluster import KMeans
# Reshape the image to be a list of pixels
image_array = image.reshape((image.shape[0] * image.shape[1], 3))
# Clusters the pixels
clt = KMeans(n_clusters = 7)
clt.fit(image_array)
# By Adrian Rosebrock
def centroid_histogram(clt):
# grab the number of different clusters and create a histogram
# based on the number of pixels assigned to each cluster
numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)
(hist, _) = np.histogram(clt.labels_, bins = numLabels)
# normalize the histogram, such that it sums to one
hist = hist.astype("float")
hist /= hist.sum()
# return the histogram
return hist
# Finds how many pixels are in each cluster
hist = centroid_histogram(clt)
# Sort the clusters according to how many pixel they have
zipped = list(zip(hist, clt.cluster_centers_))
zipped.sort(reverse=True, key=lambda x : x[0])
hist, clt.cluster_centers = zip(*zipped)
def plot_colors(hist, centroids):
# initialize the bar chart representing the relative frequency
# of each of the colors
bar = np.zeros((50, 300, 3), dtype = "uint8")
startX = 0
# loop over the percentage of each cluster and the color of
# each cluster
for (percent, color) in zip(hist, centroids):
# plot the relative percentage of each cluster
endX = startX + (percent * 300)
cv2.rectangle(bar, (int(startX), 0), (int(endX), 50),
color.astype("uint8").tolist(), -1)
startX = endX
# return the bar chart
return bar
# build a histogram of clusters and then create a figure
# representing the number of pixels labeled to each color
hist = centroid_histogram(clt)
bar = plot_colors(hist, clt.cluster_centers_)
# show our color bart
plt.figure()
plt.axis("on")
plt.imshow(bar)
plt.show()