opencv基本操作
2022/9/14 23:17:31
本文主要是介绍opencv基本操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1-1.真彩色 24 位 BMP 图像每存储一个像素点需要几个字节?计算一幅大小为 1024× 768 的图像数据存储需要的字节数(不压缩)。
24位图像储存一个像素需要3个字节
print("一副1024*768的图像需要的字节数为:",1024*768*3)
一副1024*768的图像需要的字节数为: 2359296
1-2. 将灰度为256级的图像降低为8级(将图像重新量化)并编程运行显示结果。
- 使用到的库:
import cv2 import numpy as np import matplotlib.pyplot as plt
img = cv2.imread("cameraman.tif",0) w,h = img.shape img2 = np.ones((w,h),dtype=np.uint8) level = 256-8 #目标灰度级 for i in range(w): for j in range(h): img2[i][j] =int(img[i][j]*level/256)*256/level plt.subplot(121) plt.title("before") plt.imshow(img,cmap="gray") plt.subplot(122) plt.title("after") plt.imshow(img2,cmap="gray") plt.show() print(img,"\n-----------------------\n",img2)
[[156 159 158 ... 151 152 152] [160 154 157 ... 154 155 153] [156 159 158 ... 151 152 152] ... [114 132 123 ... 135 137 114] [121 126 130 ... 133 130 113] [121 126 130 ... 133 130 113]] ----------------------- [[155 158 157 ... 150 151 151] [160 153 156 ... 153 154 152] [155 158 157 ... 150 151 151] ... [113 131 122 ... 134 136 113] [120 125 129 ... 132 129 112] [120 125 129 ... 132 129 112]]
降低了8个灰度级从图片上看不出多大区别,从数值上能看出较小的变化
1-3.打开一幅真彩色图像,利用式Gray(i,j)=0.299R(i,j)+0.587G(i,j) +0.144*B(i,j)对其进行灰度化,并显示变换前后图像。
img = cv2.imread("misaka.jpg") img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)#BGR转RGB x,y,z = img.shape img2 = np.ones((x,y),dtype=np.uint8) for i in range(x): for j in range(y): img2[i][j] = 0.299*img[i][j][0]+0.587*img[i][j][1]+0.144*img[i][j][2] plt.subplot(121) plt.title("before") plt.imshow(img) plt.subplot(122) plt.title("after") plt.imshow(img2,cmap="gray") plt.show()
1-4.打开一幅真彩色图像,将绿色和蓝色通道进行互换,显示通道互换后的图像,并对结果进行说明。
r,g,b = cv2.split(img) #使用上一题图片 img_exg = cv2.merge([r,b,g]) #切换b,g位置后合并 plt.subplot(121) plt.title("before") plt.imshow(img) plt.subplot(122) plt.imshow(img_exg) plt.title("after") plt.show()
1-8. 已知一幅图像为
求其二维傅里叶变换,并绘制其频谱图。
f = [[0,1,0,2],[0,3,0,4],[0,5,0,6],[0,7,0,8]] f = np.float32(f) f_dft = cv2.dft(f) plt.subplot(121) plt.title("spatial") plt.imshow(f,"gray") plt.subplot(122) plt.title("frequency") plt.imshow(f_dft,cmap="gray") plt.show()
这篇关于opencv基本操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-01巧用 TiCDC Syncpoint 构建银行实时交易和准实时计算一体化架构
- 2024-05-01银行核心背后的落地工程体系丨Oracle - TiDB 数据迁移详解
- 2024-04-26高性能表格工具VTable总体构成-icode9专业技术文章分享
- 2024-04-16软路由代理问题, tg 无法代理问题-icode9专业技术文章分享
- 2024-04-16程序猿用什么锅-icode9专业技术文章分享
- 2024-04-16自建 NAS 的方案-icode9专业技术文章分享
- 2024-04-14ansible 在远程主机上执行脚本,并传入参数-icode9专业技术文章分享
- 2024-04-14ansible 在远程主机上执行脚本,并传入参数, 加上remote_src: yes 配置-icode9专业技术文章分享
- 2024-04-14ansible 检测远程主机的8080端口,如果关闭,则echo 进程已关闭-icode9专业技术文章分享
- 2024-04-14result 成功怎么写-icode9专业技术文章分享