目录
安装:
pip install scikit-image
img_as_ubyte转为uint8格式,0-255,转为普通图像,举例:
>>> from skimage import img_as_ubyte
>>> image = np.array([0, 0.5, 1], dtype=float)
>>> img_as_ubyte(image)
WARNING:dtype_converter:Possible precision loss when converting from
float64 to uint8
array([ 0, 128, 255], dtype=uint8)
图像分割:
能区分比别的地方黑的物体,但是老鼠排除不了:
import matplotlib.pyplot as plt
from skimage import io, filters
# skimage提供了io模块,顾名思义,这个模块是用来图片输入输出操作的。
img = io.imread(r"E:\new\01.jpg")
val = filters.threshold_otsu(img)
mask=(img <= val*0.9)*1.0
# io.imsave("aa.jpg",mask)
plt.figure("lena.jpg")
plt.imshow(mask)
plt.axis('off')
plt.show()
裁剪图像并缩放:
def process(self, image, bbox):
''' process image with crop operation.
Args:
input: (h,w,3) array or str(image path). image value range:1~255.
image_info(optional): the bounding box information of faces. if None, will use dlib to detect face.
Returns:
pos: the 3D position map. (256, 256, 3).
'''
if image.ndim < 3:
image = np.tile(image[:, :, np.newaxis], [1, 1, 3])
left = bbox[0]
right = bbox[2]
top = bbox[1]
bottom = bbox[3]
old_size = (right - left + bottom - top) / 2
center = np.array([right - (right - left) / 2.0, bottom - (bottom - top) / 2.0 + old_size * 0.14])
size = int(old_size * 1.318)
# crop image
src_pts = np.array([[center[0] - size / 2, center[1] - size / 2], [center[0] - size / 2, center[1] + size / 2],
[center[0] + size / 2, center[1] - size / 2]])
DST_PTS = np.array([[0, 0], [0, self.resolution_inp - 1], [self.resolution_inp - 1, 0]])
tform = estimate_transform('similarity', src_pts, DST_PTS)
彩色图像相似度:
import cv2
from skimage.metrics import structural_similarity as compare_ssim
image1 = cv2.imread(r'F:\194536.jpg')
image2 = cv2.imread(r'F:\194536.jpg')
ssim_value = compare_ssim(image1, image2, multichannel=True,channel_axis=2)
print("SSIM value:", ssim_value)