读取灰度照片
cv.cvtColor(需要转换的图片, cv.COLOR_BGR2GRAY)
如:gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
#导入cv模块
import cv2 as cv
#读取照片
src = cv.imread("d:/Python_image/vegetable.jpg") #读取图片
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) #灰度转换
#
#显示图片
cv.imshow("intput image",src) #展示输入图片
cv.imshow("output image",gray) #展示输出图片---灰度图像
#等待
cv.waitKey(0)
#释放内存
cv.destroyALLWindows()
修改照片尺寸
resize_img = cv.resize(需要转换的照片,dsize=(自定义长,自定义宽))
如:resize_img = cv.resize(src,dsize=(200,200))
#导入cv模块
import cv2 as cv
#读取照片
src = cv.imread("d:/Python_image/vegetable.jpg") #读取图片
resize_img = cv.resize(src,dsize=(200,200))
#显示图片
cv.imshow("intput image",src) #展示输入图片
cv.imshow("output image",resize_img) #展示输出图片---修改尺寸的图像
#打印大小
print('未修改:',src.shape)
print('修改后:',resize_img.shape)
#等待
cv.waitKey(0)
#释放内存
cv.destroyALLWindows()
绘制矩形,圆形
cv.rectangle(需要处理的图片,(起始点的横坐标,起始点的纵坐标,划线的横长度,划线的纵长度),color=(0,0,255),thickness=1)
如:cv.rectangle(src,(x,y,100,100),color=(0,0,255),thickness=1)
#导入cv模块
import cv2 as cv
#读取照片
src = cv.imread("d:/Python_image/vegetable.jpg") #读取图片
#坐标
x,y,w,h = 100,100,100,100
#绘制矩形
cv.rectangle(src,(x,y,100,100),color=(0,0,255),thickness=1)
#绘制圆形
cv.circle(src,center=(x+w,y+h),radius=100,color=(0,255,255),thickness=1)
cv.imshow("intput image",src) #展示输入图片
#等待
cv.waitKey(0)
#释放内存
cv.destroyALLWindows()
效果展示
![](https://img-blog.csdnimg.cn/img_convert/4c6c6ae78b2ea062500bd078ccc1cb91.png)