赞
踩
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
import cv2 import matplotlib.pyplot as plt import numpy as np # 导入图片 img = cv2.imread('cat.jpg') img = img[200:600,200:700] b,g,r = cv2.split(img) img = cv2.merge((r,g,b)) top_size,bottom_size,left_size,right_size = (50,50,50,50) # 边界填充 replicate = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_REPLICATE) reflect = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_REFLECT) reflect101 = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_REFLECT_101) wrap = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_WRAP) constant = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_CONSTANT) # 分别展示看到不同的效果 plt.subplot(231),plt.imshow(img,'gray'),plt.title('ORIGINAL') #原图 plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE') plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT') plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101') plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP') plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT') plt.show()
各个方法效果展示如下:
# 展示图片函数
import cv2
import numpy as np
def show_img(name,img_path):
cv2.imshow(name,img_path)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import matplotlib.pyplot as plt
import numpy as np
import show_img #该包为自己写的展示图片功能
cat = cv2.imread('cat.jpg')
dog = cv2.imread('dog.jpg')
# print(cat+dog) #查看两张图片的大小(不一样大会报错提示)
cat = cv2.resize(cat,(889,500)) #调整两张图片大小一致(将大图片改小)
# 图像融合
add_img = cv2.addWeighted(cat,0.4,dog,0.6,0)
show_img.show_img('add',add_img) #展示图片(可自己写展示图片那几行代码)
import cv2 import show_img import matplotlib.pyplot as plt import numpy as np cat = cv2.imread('../data/cat.jpg') cat = cat[200:700,200:700,:] #原图太大了,这里只展示部分效果 cat_gray = cv2.imread('../data/cat.jpg',cv2.IMREAD_GRAYSCALE) #变成灰色图 cat_gray = cat_gray[200:700,200:700] # show_img.show_img('cat',cat) #调整颜色通道 b,g,r = cv2.split(cat) cat = cv2.merge((r,g,b)) #图像阈值处理 ret,thresh1 = cv2.threshold(cat_gray,127,255,cv2.THRESH_BINARY) ret,thresh2 = cv2.threshold(cat_gray,127,255,cv2.THRESH_BINARY_INV) ret,thresh3 = cv2.threshold(cat_gray,127,255,cv2.THRESH_TRUNC) ret,thresh4 = cv2.threshold(cat_gray,127,255,cv2.THRESH_TOZERO) ret,thresh5 = cv2.threshold(cat_gray,127,255,cv2.THRESH_TOZERO_INV) titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV'] images = [cat,thresh1,thresh2,thresh3,thresh4,thresh5] # 展示不同方法进行的不同处理 for i in range(6): plt.subplot(2,3,i+1),plt.imshow(images[i],'gray') plt.title(titles[i]) plt.xticks([]),plt.yticks([]) plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。