ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

opencv基本操作

2022-09-14 22:31:55  阅读:196  来源: 互联网

标签:... plt img title imshow cv2 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()


标签:...,plt,img,title,imshow,cv2,opencv,基本操作
来源: https://www.cnblogs.com/ryuta/p/16694839.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有