三合一网站开发架构,wordpress 外链 图片,普通网站 多大空间,网站指向错误问题引入在小强学PythonOpenCV之#xff0d;1.4.2裁剪一节#xff0c;我们使用的是numpy数组切片功能实现图片区域的裁剪。那么#xff0c;如果我们想要裁剪图像中任意形状的区域时#xff0c;应该怎么办呢#xff1f;答案是#xff0c;使用掩膜(masking)。但是这一节我们…问题引入在小强学PythonOpenCV之1.4.2裁剪一节我们使用的是numpy数组切片功能实现图片区域的裁剪。那么如果我们想要裁剪图像中任意形状的区域时应该怎么办呢答案是使用掩膜(masking)。但是这一节我们先看一下掩膜的基础。图像的位运算。代码编写python脚本masking.py如下:# 导入库
import numpy as np
import argparse
import cv2# 构建参数解析器
ap argparse.ArgumentParser()
ap.add_argument(-i, --image, requiredTrue, helpPath to the image)
args vars(ap.parse_args())# 加载猫的图像
image cv2.imread(args[image])
cv2.imshow(Cat, image)# 创建矩形区域填充白色255
rectangle np.zeros(image.shape[0:2], dtype uint8)
cv2.rectangle(rectangle, (25, 25), (275, 275), 255, -1)
cv2.imshow(Rectangle, rectangle)# 创建圆形区域填充白色255
circle np.zeros(image.shape[0:2], dtype uint8)
cv2.circle(circle, (150, 150), 150, 255, -1)
cv2.imshow(Circle, circle)# 在此例二值图像中以下的0表示黑色像素值0, 1表示白色像素值255
# 位与运算与常识相同有0则为0, 均无0则为1
bitwiseAnd cv2.bitwise_and(rectangle, circle)
cv2.imshow(AND, bitwiseAnd)
cv2.waitKey(0)# 或运算有1则为1, 全为0则为0
bitwiseOr cv2.bitwise_or(rectangle, circle)
cv2.imshow(OR, bitwiseOr)
cv2.waitKey(0)# 非运算非0为1, 非1为0
bitwiseNot cv2.bitwise_not(circle)
cv2.imshow(NOT, bitwiseNot)
cv2.waitKey(0) # 异或运算不同为1, 相同为0
bitwiseXor cv2.bitwise_xor(rectangle, circle)
cv2.imshow(XOR, bitwiseXor)
cv2.waitKey(0)效果运行脚本:相信大家看到效果再结合代码可以很容易理解。裁剪下面我们利用OR结果有点像猫的头像轮廓把本课的主题图片中的猫的头像剪切出来。我们需要修改一下矩形区域的大小去掉下边的两个角。cv2.rectangle(rectangle, (25, 25), (275, 220), 255, -1)最终调整后的代码如下# 导入库
import numpy as np
import argparse
import cv2# 构建参数解析器
ap argparse.ArgumentParser()
ap.add_argument(-i, --image, requiredTrue, helpPath to the image)
args vars(ap.parse_args())# 加载猫的图像
image cv2.imread(args[image])
cv2.imshow(Cat, image)# 创建矩形区域填充白色255
rectangle np.zeros(image.shape[:2], dtype uint8)
cv2.rectangle(rectangle, (380, 100), (575, 200), 255, -1)
cv2.imshow(Rectangle, rectangle)# 创建圆形区域填充白色255
circle np.zeros(image.shape[:2], dtype uint8)
cv2.circle(circle, (475, 180), 105, 255, -1)
cv2.imshow(Circle, circle)# 或运算
bitwiseOr cv2.bitwise_or(rectangle, circle)
cv2.imshow(OR, bitwiseOr)
cv2.waitKey(0)mask bitwiseOr
cv2.imshow(Mask, mask)# Apply out mask -- notice how only the person in the image is cropped out
masked cv2.bitwise_and(image, image, maskmask)
cv2.imshow(Mask Applied to Image, masked)
cv2.waitKey(0)运行得到得到我们“近似”得到了猫的头像。总结1. 与或非异或运算与我们的常识类似。2. 掩膜操作就是两幅图像(numpy数组)的位运算操作。