【发布时间】:2014-03-21 21:03:45
【问题描述】:
我正在做一些图像处理,应用一些过滤器(例如 sobel 算子)来查找边缘。所以,在应用运算符之后,我这样做:
def classify_edges(magnitude, threshold=.75):
"""
Classifies a pixel as edge or not based on its magnitude, which is generated after the appliance of an operator
:param magnitude : the gradient magnitude of the pixel
:return : the pixel classification
"""
classification = Dt.Data()
e = 0
n = 0
for x, y in product(range(classification.rows), range(classification.cols)):
# Not edge
if magnitude[x][y] > threshold:
classification.data[x][y] = 1.0
n += 1
# Edge
else:
classification.data[x][y] = 0.0
e += 1
print e, n
return classification
我根据像素的大小为像素分配一个值(b 或 w),以获取边缘。因此,我遵循模式 1 = True,对于 is_edge,0 = False,对于 not_edge,并期望获得具有白色边缘和黑色背景的图像。但我注意到我得到了相反的结果,0 代表白色,1 代表黑色。我验证了这个打印值。因为我的边缘比背景少,所以我的边缘数量小于我的背景数量。我代表 e >> n,如下图所示,我的边缘是白色的,我的背景是黑色的。
这是我的绘图方法:
def generate_data_black_and_white_heat_map(data, x_axis_label, y_axis_label, plot_title, file_path, box_plot=False):
"""
Generate a heat map of the data
:param data : the data to be saved
:param x_axis_label : the x axis label of the data
:param y_axis_label : the y axis label of the data
:param plot_title : the title of the data
:param file_path : the name of the file
"""
plt.figure()
plt.title(plot_title)
plt.imshow(data.data, extent=[0, data.cols, data.rows, 0], cmap='binary')
plt.xlabel(x_axis_label)
plt.ylabel(y_axis_label)
plt.savefig(file_path + '.png')
plt.close()
我想知道 matplotlib 的黑色是 1,白色是 0,还是我做错了什么。我想这是由于我的 cmpa = 'binary'。有一些关于这种转换是如何完成的文档吗?
提前谢谢你。
【问题讨论】:
标签: python colors matplotlib