我正在尝试将numpy矩阵保存为灰度图像Image.fromarray.它似乎适用于随机矩阵,但不适用于特定的矩阵(应该出现一个圆圈).谁能解释我做错了什么?
from PIL import Image
import numpy as np
radius = 0.5
size = 10
x,y = np.meshgrid(np.linspace(-1,1,size),np.linspace(-1,1,size))
f = np.vectorize(lambda x,y: ( 1.0 if x*x + y*y < radius*radius else 0.0))
z = f(x,y)
print(z)
zz = np.random.random((size,size))
img = Image.fromarray(zz,mode='L') #replace z with zz and it will just produce a black image
img.save('my_pic.png')
Run Code Online (Sandbox Code Playgroud) 我目前正在应用Zhang-Suen细化算法来研究我想要稍后跟踪的一些细丝.这需要我输出灰度图像以便使用OpenCV识别对象.
import matplotlib
import matplotlib.pyplot as plt
import skimage.io as io
"load image data"
Img_Original = io.imread( './data/test1.bmp') # Gray image, rgb images need pre-conversion
"Convert gray images to binary images using Otsu's method"
from skimage.filter import threshold_otsu
Otsu_Threshold = threshold_otsu(Img_Original)
BW_Original = Img_Original < Otsu_Threshold # must set object region as 1, background region as 0 !
#...
"Apply the algorithm on images"
BW_Skeleton = zhangSuen(BW_Original)
# BW_Skeleton = BW_Original
"Display the results"
fig, ax = plt.subplots(1, …Run Code Online (Sandbox Code Playgroud)