Python环境下开发OpenCV图像处理

默认已安装好Python和OpenCV库,可以执行下面的代码,实现一个基本的图像读取和保存。

import numpy as np
import matplotlib.pyplot as plt
import cv2    #OPENCV默认图像数据为BGR格式
print("opencv version = ",cv2.__version__)   #打印当前opencv的版本号

#读取图像文件
image = cv2.imread("C:\\works\\cat.jpg")

#显示图像的维度和数据类型
print(f"图像的维度:{image.shape}")
print(f"图像的数据类型:{image.dtype}")

#获取图像的大小
height   =  image.shape[0] #高度
width    =  image.shape[1] #宽度
channels =  image.shape[2] #像素的通道数- 1灰度图,3彩色图像BGR
print("height = ",height)
print("width  = ",width)
print("channels = ",channels)

half_height = height/2 #取一半大小
half_width  = width/2  #取一半大小
print("half_height = ",half_height)
print("half_width  = ",half_width)

#缩放图像,新的图像的大小为原来的一半
resized_img = cv2.resize(image,(round(half_width),round(half_height)))

#保存缩放后的图像
if resized_img is not None:
    cv2.imwrite("C:\\works\\output_cat
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值