1.opencv学习之截取部分图像,具体的代码如下:
import cv2 #opencv读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np
img=cv2.imread('cat.jpg')
#选取我们需要截取的部分。
cat=img[0:50,0:200]
cv2.imshow('cat',cat)
cv2.waitKey(1000)
cv2.destroyAllWindows()
2.在opencv中有三颜色通道,我们可以通过以下代码提取出BGR三颜色通道中的数据。
import cv2 #opencv读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np
img=cv2.imread('cat.jpg')
b,g,r=cv2.split(img)
print(r)
#输出结果如下
[[160 164 170 ... 185 184 183]
[126 131 137 ... 184 183 182]
[127 131 138 ... 183 182 181]
...
[198 193 178 ... 206 195 174]
[176 183 175 ... 188 144 125]
[190 190 157 ... 200 145 144]]
import cv2 #opencv读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np
img=cv2.imread('cat.jpg')
b,g,r=cv2.split(img)
#查看单通道的shape
print(r.shape)
#合并后查看其shape
img=cv2.merge((b,g,r))
print(img.shape)
3.保留单通道数据并展示。
import cv2 #opencv读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np
img=cv2.imread('cat.jpg')
b,g,r=cv2.split(img)
# 只保留R
cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,1] = 0
cv2.imshow('R',cur_img)
cv2.waitKey(1000)
cv2.destroyAllWindows()
import cv2 #opencv读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np
img=cv2.imread('cat.jpg')
b,g,r=cv2.split(img)
# 只保留G
cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,2] = 0
cv2.imshow('G',cur_img)
cv2.waitKey(1000)
cv2.destroyAllWindows()
import cv2 #opencv读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np
img=cv2.imread('cat.jpg')
b,g,r=cv2.split(img)
# 只保留B
cur_img = img.copy()
cur_img[:,:,1] = 0
cur_img[:,:,2] = 0
cv2.imshow('B',cur_img)
cv2.waitKey(1000)
cv2.destroyAllWindows()
本博主新开公众号,欢迎大家扫码关注支持。感谢