from skimage import io,data
img=data.chelsea()
pixel=img[20,30,1]
pixel
np.uint8(129)
R=img[:,:,0]
io.imshow(R)
/tmp/ipykernel_252/1112094983.py:2: FutureWarning: `imshow` is deprecated since version 0.25 and will be removed in version 0.27. Please use `matplotlib`, `napari`, etc. to visualize images. io.imshow(R)
<matplotlib.image.AxesImage at 0x7f0eb296db50>
随机生成5000个椒盐:
import numpy as np
rows,cols,dims=img.shape
for i in range(5000):
x=np.random.randint(0,rows)
y=np.random.randint(0,cols)
img[x,y,:]=255
io.imshow(img)
/tmp/ipykernel_252/2464940474.py:8: FutureWarning: `imshow` is deprecated since version 0.25 and will be removed in version 0.27. Please use `matplotlib`, `napari`, etc. to visualize images. io.imshow(img)
<matplotlib.image.AxesImage at 0x7f0eb238f0e0>
roi=img[80:180,100:200,:]
io.imshow(roi)
/tmp/ipykernel_252/1133973099.py:2: FutureWarning: `imshow` is deprecated since version 0.25 and will be removed in version 0.27. Please use `matplotlib`, `napari`, etc. to visualize images. io.imshow(roi)
<matplotlib.image.AxesImage at 0x7f0eb241f890>
对多个像素点进行操作,使用数组切片方式访问。 切片方式返回的是以指定间隔下标访问该数组的像素值。 下面是有关灰度图像的一些例子:
img[i,:] = im[j,:] # 将第 j 行的数值赋值给第 i 行
img[:,i] = 100 # 将第 i 列的所有数值设为 100
img[:100,:50].sum() # 计算前 100 行、前 50 列所有数值的和
img[50:100,50:100] # 50~100 行,50~100 列(不包括第 100 行和第 100 列)
img[i].mean() # 第 i 行所有数值的平均值
img[:,-1] # 最后一列
img[-2,:] #(or im[-2]) # 倒数第二行
from skimage import color
img=data.coffee()
img_gray=color.rgb2gray(img)
rows,cols=img_gray.shape
for i in range(rows):
for j in range(cols):
if (img_gray[i,j]<=0.5):
img_gray[i,j]=0
else:
img_gray[i,j]=1
io.imshow(img_gray)
/tmp/ipykernel_252/679678783.py:12: FutureWarning: `imshow` is deprecated since version 0.25 and will be removed in version 0.27. Please use `matplotlib`, `napari`, etc. to visualize images. io.imshow(img_gray)
<matplotlib.image.AxesImage at 0x7f0eb24e5580>
# from skimage import io,data
img=data.chelsea()
reddish = img[:, :, 0] > 170
img[reddish] = [0, 255, 0]
io.imshow(img)
/tmp/ipykernel_252/4062863864.py:5: FutureWarning: `imshow` is deprecated since version 0.25 and will be removed in version 0.27. Please use `matplotlib`, `napari`, etc. to visualize images. io.imshow(img)
<matplotlib.image.AxesImage at 0x7f0eb24bd0d0>
这个例子先对 R
通道的所有像素值进行判断,如果大于 170
,则将这个地方的像素值变为 [0,255,0]
,
即G通道值为 255
,R
和 B
通道值为 0
。