目录
为什么需要填充边缘呢?我们以下图为例。
可以看到,左图在逆时针旋转45度之后原图的四个顶点在右图中已经看不到了,同时,右图的四个顶点区域其实是什么都没有的,因此我们需要对空出来的区域进行一个填充。右图就是对空出来的区域进行了像素值为(0,0,0)的填充,也就是黑色像素值的填充。除此之外,后续的一些图像处理方式也会用到边缘填充,这里介绍五个常用的边缘填充方法。
1 边界复制(BORDER_REPLICATE)
边界复制会将边界处的像素值进行复制,然后作为边界填充的像素值,如下图所示,可以看到四周的像素值都一样。
new_img=cv.warpAffine(img,M,(w,h),cv.INTER_LANCZOS4,borderMode=cv.BORDER_REPLICATE)
案例:
import cv2 as cv
face = cv.imread("./images/face.png")
# 定义旋转中心
h,w = face.shape[:2]
center = (w//2,h//2)
# 获取旋转矩阵
M = cv.getRotationMatrix2D(center,45,0.5)
# 使用仿射变换矩阵进行旋转
img1 = cv.warpAffine(face,M,(w,h),flags=cv.INTER_LANCZOS4)
# 边界复制
replicate = cv.warpAffine(face,M,(w,h),flags=cv.INTER_LANCZOS4,borderMode=cv.BORDER_REPLICATE)
cv.imshow("face",face)
cv.imshow("new1",img1)
cv.imshow("replicate",replicate)
cv.waitKey(0)
cv.destroyAllWindows()
输出:
2 边界反射(BOEDER_REFLECT)
根据原图的边缘进行反射。
new_img=cv.warpAffine(img,M,(w,h),cv.INTER_LANCZOS4,borderMode=cv.BORDER_REFLECT)
案例:
import cv2 as cv
face = cv.imread("./images/face.png")
# 定义旋转中心
h,w = face.shape[:2]
center = (w//2,h//2)
# 获取旋转矩阵
M = cv.getRotationMatrix2D(center,45,0.5)
# 使用仿射变换矩阵进行旋转
img1 = cv.warpAffine(face,M,(w,h),flags=cv.INTER_LANCZOS4)
# 边界反射
reflect = cv.warpAffine(face,M,(w,h),flags=cv.INTER_LANCZOS4,borderMode=cv.BORDER_REFLECT)
cv.imshow("face",face)
cv.imshow("new1",img1)
cv.imshow("reflect",reflect)
cv.waitKey(0)
cv.destroyAllWindows()
输出:
3 边界反射101(BORDER_REFLECT101)
与边界反射不同的是,不再反射边缘的像素点。
new_img=cv.warpAffine(img,M,(w,h),cv.INTER_LANCZOS4,borderMode=cv.BORDER_REFLECT_101)
案例:
import cv2 as cv
face = cv.imread("./images/face.png")
# 定义旋转中心
h,w = face.shape[:2]
center = (w//2,h//2)
# 获取旋转矩阵
M = cv.getRotationMatrix2D(center,45,0.5)
# 使用仿射变换矩阵进行旋转
img1 = cv.warpAffine(face,M,(w,h),flags=cv.INTER_LANCZOS4)
# 边界反射101
reflect101 = cv.warpAffine(face,M,(w,h),flags=cv.INTER_LANCZOS4,borderMode=cv.BORDER_REFLECT_101)
cv.imshow("face",face)
cv.imshow("new1",img1)
cv.imshow("reflect101",reflect101)
cv.waitKey(0)
cv.destroyAllWindows()
输出:
4 边界常数(BORDER_CONSTANT)
指定常数值作为边缘填充的像素值。
new_img=cv.warpAffine(img,M,(w,h),cv.INTER_LANCZOS4,borderMode=cv.BORDER_CONSTANT,borderValue=(0,0,255))
案例:
import cv2 as cv
face = cv.imread("./images/face.png")
# 定义旋转中心
h,w = face.shape[:2]
center = (w//2,h//2)
# 获取旋转矩阵
M = cv.getRotationMatrix2D(center,45,0.5)
# 使用仿射变换矩阵进行旋转
img1 = cv.warpAffine(face,M,(w,h),flags=cv.INTER_LANCZOS4)
# 边界常数
constant = cv.warpAffine(face,M,(w,h),flags=cv.INTER_LANCZOS4,borderMode=cv.BORDER_CONSTANT,borderValue=(255,0,0))
cv.imshow("face",face)
cv.imshow("new1",img1)
cv.imshow("constant",constant)
cv.waitKey(0)
cv.destroyAllWindows()
输出:
5 边界包裹(BORDER_WRAP)
复制图像进行滑动填充
new_img=cv.warpAffine(img,M,(w,h),cv.INTER_LANCZOS4,borderMode=cv.BORDER_WRAP)
案例:
import cv2 as cv
face = cv.imread("./images/face.png")
# 定义旋转中心
h,w = face.shape[:2]
center = (w//2,h//2)
# 获取旋转矩阵
M = cv.getRotationMatrix2D(center,45,0.5)
# 使用仿射变换矩阵进行旋转
img1 = cv.warpAffine(face,M,(w,h),flags=cv.INTER_LANCZOS4)
# 边界包裹
wrap = cv.warpAffine(face,M,(w,h),flags=cv.INTER_LANCZOS4,borderMode=cv.BORDER_WRAP)
cv.imshow("face",face)
cv.imshow("new1",img1)
cv.imshow("wrap",wrap)
cv.waitKey(0)
cv.destroyAllWindows()
输出: