Python与OpenCV图像简单操作
准备工作
OpenCv的安装
打开命令行输入 pip install opencv-python(前提是有python环境)
题目一
将示例代码修改为能够批量重命名目标文件、批量删除文件等
代码如下:
import os
def rename():#文件重命名
path = "D:\Picture\yiban\itwenty" # 目标文件夹路径
filelist = os.listdir(path)
i = 1
for file in filelist:
old = os.path.join(path,file)
new = os.path.join(path,"name" + str(i) +".png")
os.rename(old,new)
i = i + 1
def delete():#文件的批量删除
path = "D:\Picture\yiban\itwenty" # 目标文件路径
i = 1
for root, dirs, files in os.walk(path):
for name in files:
if 'name' in name:
os.remoev(os.path.join(root, name))
#rename()
delete()
运行结果
(修改前)
(修改后)
题目二
自行实现几个图片基本操作并将改动后的图片保存在同一个文件夹中
代码如下
(截取图片)
import cv2
img1 = cv2.imread("1.jpg")
img2 = cv2.imread("2.jpg")
img3 = cv2.imread("5.jpg")
frame1 = img1[200:300, 200:300]
frame2 = img2[200:300, 200:300]
frame3 = img3[200:300, 200:300]
#保存图片
cv2.imwrite("frame1.jpg", frame1)
cv2.imwrite("frame2.jpg", frame2)
cv2.imwrite("frame3.jpg", frame3)
cv2.imshow("test1", frame1)
cv2.imshow("test2", frame2)
cv2.imshow("test3", frame3)
cv2.waitKey(0)
(图片翻转)
import cv2
img = cv2.imread("1.jpg")
flipCode1 = 1#左右翻转
filpCode2 = 0#上下翻转
filpCode3 = -1#先上下翻转,再左右翻转
img1 = cv2.flip(img, flipCode1)
img2 = cv2.flip(img, filpCode2)
img3 = cv2.flip(img, filpCode3)
#保存修改图片
cv2.imwrite("filp1.jpg",img1)
cv2.imwrite("filp2.jpg",img2)
cv2.imwrite("filp3.jpg",img3)
#显示图片
cv2.imshow("test1",img)
cv2.imshow("test2",img1)
cv2.imshow("test3",img2)
cv2.imshow("test3",img3)
cv2.waitKey(0)
(缩放图片)
import cv2
img = cv2.imread("5.jpg",cv2.IMREAD_COLOR)
img1 = cv2.resize(img, (600, 600))#图片的尺寸
img2 = cv2.resize(img, None, fx=0.5, fy=0.5)
cv2.imwrite("resize1.jpg", img1)
cv2.imwrite("resize2.jpg", img2)
cv2.imshow("test", img)
cv2.imshow("resize1", img1)
cv2.imshow("resize2", img2)
cv2.waitKey(0)
(转为灰度)
import cv2
img = cv2.imread("2.jpg")#图片格式:BGR(W,H,C)
cv2.imshow("BGR", img)
#转换为RBG
img1 = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
cv2.imshow("RGB",img)
cv2.imwrite("cvtColor1.jpg",img1)
img2 = cv2.cvtColor(img1,cv2.COLOR_RGB2GRAY)
cv2.imwrite("cvtColor2.jpg",img2)
cv2.imshow("GRAY",img2)
cv2.waitKey(0)
题目三
画一只猪
代码如下
import cv2
import numpy as np
#设置画布大小
img = 255*np.ones((350,520,3),np.uint8)
font = cv2.FONT_HERSHEY_DUPLEX
#文本
cv2.putText(img, "My Pig", (25,25), font, 0.8, (150 ,150 , 0), 1)
#左耳
cv2.line(img ,(85,76),(75,50),(203,192,255),2)
cv2.line(img ,(75,50),(75,85),(203,192,255),2)
#右耳
cv2.line(img ,(95,76),(110,50),(203,192,255),2)
cv2.line(img ,(110,50),(115,85),(203,192,255),2)
#身体
cv2.ellipse(img,(200,110),(80,40),180,0,360,(203,192,255),-1)
#头
cv2.circle(img,(100,100),30,(203,192,255),-1)
#眼睛
cv2.circle(img,(90,93),4,(0,0,0),-1)
cv2.circle(img,(110,93),4,(0,0,0),-1)
#猪鼻子
cv2.ellipse(img,(100,110),(15,9),180,0,360,(0,0,0),1)
cv2.circle(img,(94,110),4,(0,0,0),1)
cv2.circle(img,(106,110),4,(0,0,0),1)
#尾巴
cv2.line(img ,(280,110),(350,115),(203,192,255),2)
#脚
cv2.rectangle(img, (160,140), (170,160), (203, 192,255),2)
cv2.rectangle(img, (170,125), (180,180), (203, 192,255),2)
cv2.rectangle(img, (240,140), (250,160), (203, 192,255),2)
cv2.rectangle(img, (250,135), (260,180), (203, 192,255),2)
cv2.imshow("Draw", img)
cv2.waitKey(0)
结果图
总结
在实践中对图像操作更加掌握,本次学习的难点还是对于图像的绘制,在绘制之前可以先将图像分成几个部分,将各部分点的坐标准确地写出来,大致的图形出来之后,再进行不断地调整,是一个比较繁琐的过程,但也能够培养认真严谨的学习态度。