在Python中使用 OpenCV 使用 Seam 雕刻调整图像大小
接缝雕刻是一种有效的图像处理技术,借助它可以调整图像的大小而无需从图像中删除重要元素。基本方法是从左到右或从上到下找到所有连续的低能量像素。选择区域后,将其从原始图像中移除,只留下图像的相关部分。能量图源自原始图像,代表图像的恰当细节。借助能量图,我们可以识别从右到左或从上到下分布的接缝。
Seam 雕刻与传统的调整大小方法有何不同?
缝雕刻不同于调整大小,因为在缝雕刻中,所有有价值的方面和元素仍然存在于图像中,但是调整图像的大小只是简单地复制到一个新的大小,这可能会导致丢失重要的细节。
下面是Seam雕刻技术的实现:
Python3
# import the necessary libraries
from skimage import transform
from skimage import filters
import cv2
# read the input image
img = cv2.imread('Seam.jpg')
# convert image from BGR
# to GRAY scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# filter out the gradient representation
# image to further process for
# seam carving algorithm
# filters.sobel() is used to
# find edges in an image
filtered = filters.sobel(gray.astype("float"))
for i in range(20, 180, 20):
# apply seam carve to the image,
# iterating over the image for
# multiple frames
# transform.seam_carve() can transform
# the seam of image vertically as
# well as horizontally
carved_image = transform.seam_carve(img,
filtered,
'vertical',
i)
# show the original image
cv2.imshow("original", img)
# show the carved image
cv2.imshow("carved", carved_image)
# print shape of both images
print("Shape of original image ",
img.shape)
print("Shape of Carved image ",
carved_image.shape)
# wait
cv2.waitKey(0)
输出
Shape of original Image (667, 1000, 3)
Shape of Carved image (667, 840, 3)
注意:此代码使用 scikit-image 版本 0.15.0。