目录
crop img
import cv2
image = cv2.imread("konglong.jpg")
cv2.imshow("Original", image)
print(image.shape)
cropImg = image[0:300, 0:400]
cv2.imshow("crop", cropImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
draw_circle
import cv2
img = cv2.imread("konglong.jpg",1)
cv2.circle(img,(100,100),5,(255,0,0),-1)
winname = 'example'
cv2.namedWindow(winname)
cv2.imshow(winname, img)
cv2.waitKey(0)
cv2.destroyWindow(winname)
drawfigure
import numpy as np
import cv2
# Create a black image
# img=np.zeros((512,512,3), np.uint8)
img = cv2.imread("konglong.jpg",1)
# Draw a diagonal blue line with thickness of 5 px
cv2.line(img,(0,0),(511,511),(255,0,0),5)
cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(300,100), font, 1,(0,0,0),2)
winname = 'example'
cv2.namedWindow(winname)
cv2.imshow(winname, img)
cv2.waitKey(0)
cv2.destroyWindow(winname)
flip
# 导入库
import argparse
import cv2
# 构造参数解析器
# ap = argparse.ArgumentParser()
# ap.add_argument("-i", "--image", required=True, help = "Path to the image")
# args = vars(ap.parse_args())
# 加载图像并显示
image = cv2.imread("konglong.jpg")
cv2.imshow("Original", image)
# 横向翻转图像
flipped = cv2.flip(image, 1)
cv2.imshow("Flipped Horizontally", flipped)
# 纵向翻转图像
flipped = cv2.flip(image, 0)
cv2.imshow("Flipped Vertically", flipped)
# 同时在横向和纵向翻转图像
flipped = cv2.flip(image, -1)
cv2.imshow("Flipped Horizontally & Vertically", flipped)
cv2.waitKey(0)
mutil_scal_image
import numpy as np
import cv2
image = cv2.imread("konglong.jpg")
cv2.imshow("Original", image)
print(image.shape)
scales = []
factor = 0.793700526
largest = min(2,4000/max(image.shape[0:2]))
scale = largest
minD = largest*min(image.shape[0:2])
print(minD)
while minD >=227:
scales.append(scale)
scale *= factor
minD *= factor
print(scales)
i = 0
for scale in scales:
scale_img = cv2.resize(image,((int(image.shape[0]*scale),int(image.shape[1]*scale))))
cv2.imwrite("scale"+str(i)+".jpg",scale_img)
i+=1
# cv2.waitKey(0)
openAndsave
import cv2
img = cv2.imread("konglong.jpg",1)
cv2.imshow('image',img)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
elif k == ord('s'):
cv2.imwrite('newkonglong.png',img)
cv2.destroyAllWindows()
rotation
# 导入库
import numpy as np
import argparse
import cv2
# 定义旋转rotate函数
def rotate(image, angle, center=None, scale=1.0):
# 获取图像尺寸
(h, w) = image.shape[:2]
# 若未指定旋转中心,则将图像中心设为旋转中心
if center is None:
center = (w / 2, h / 2)
# 执行旋转
M = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, M, (w, h))
# 返回旋转后的图像
return rotated
# 构造参数解析器
# ap = argparse.ArgumentParser()
# ap.add_argument("-i", "--image", required=True, help="Path to the image")
# args = vars(ap.parse_args())
# 加载图像并显示
image = cv2.imread("konglong.jpg")
cv2.imshow("Original", image)
print(image.shape)
# 将原图旋转不同角度
rotated = rotate(image, 45)
cv2.imshow("Rotated by 45 Degrees", rotated)
print(rotated.shape)
rotated = rotate(image, -45)
cv2.imshow("Rotated by -45 Degrees", rotated)
rotated = rotate(image, 90)
cv2.imshow("Rotated by 90 Degrees", rotated)
rotated = rotate(image, -90)
cv2.imshow("Rotated by -90 Degrees", rotated)
rotated = rotate(image, 180)
cv2.imshow("Rotated by 180 Degrees", rotated)
cv2.waitKey(0)
resize
# 导入库
"""
在一般情况下, cv2.INTER_NEAREST速度最快,但质量不高。如果资源非常有限,可以考虑使用。否则不选这个,尤其在上采样(增加)图像的大小时。
当增加(上采样)图像的大小时,可以考虑使用 cv2.INTER_LINEAR 和 cv2.INTER_CUBIC两种插值方法。 cv2.INTER_LINEAR 方法比 cv2.INTER_CUBIC 方法稍快,但无论哪个效果都不错。
当减小(下采样)的图像的大小,OpenCV的文档建议使用 cv2.INTER_AREA。PS.我感觉其实下采样各种方法都差不多,取速度最快的(cv2.INTER_NEAREST)就好。
"""
import argparse
import cv2
# 定义缩放resize函数
def resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# 初始化缩放比例,并获取图像尺寸
dim = None
(h, w) = image.shape[:2]
# 如果宽度和高度均为0,则返回原图
if width is None and height is None:
return image
# 宽度是0
if width is None:
# 则根据高度计算缩放比例
r = height / float(h)
dim = (int(w * r), height)
# 如果高度为0
else:
# 根据宽度计算缩放比例
r = width / float(w)
dim = (width, int(h * r))
# 缩放图像
resized = cv2.resize(image, dim, interpolation=inter)
# 返回缩放后的图像
return resized
# 构造参数解析器
# ap = argparse.ArgumentParser()
# ap.add_argument("-i", "--image", required=True, help="Path to the image")
# args = vars(ap.parse_args())
# 加载参数并显示(由于原图较大,我们先将原图缩小)
image = cv2.imread("konglong.jpg")
cv2.imshow("Original", image)
# 创建插值方法数组
methods = [
("cv2.INTER_NEAREST", cv2.INTER_NEAREST),
("cv2.INTER_LINEAR", cv2.INTER_LINEAR),
("cv2.INTER_AREA", cv2.INTER_AREA),
("cv2.INTER_CUBIC", cv2.INTER_CUBIC),
("cv2.INTER_LANCZOS4", cv2.INTER_LANCZOS4)]
# 循环执行插值方法数组中的各个方法
for (name, method) in methods:
# 放大3倍
resized = resize(image, width=image.shape[1] * 3, inter=method)
cv2.imshow("ZoomIn Method: {}".format(name), resized)
# 缩小2倍
resized = resize(image, width=image.shape[1] //2, inter=method)
cv2.imshow("ZoomOut Method: {}".format(name), resized)
cv2.waitKey(0)