有时候需要读取图片,并添加标注,然后保存该图片。
如果用opencv 的imwrite函数不可行,因为imwrite函数需要输入参数是numpy数组。
此时用plt savefig函数可以保存该图片,但是savefig函数前面不能出现plt.show()函数。
代码案例:
import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片
import numpy as np
import cv2
lena = cv2.imread('/home/data/test.png',2)
print(lena.shape,lena.dtype)
plt.imshow(lena,'gray')
plt.show()
img = lena
h, w = img.shape[:2]
pixelSequence = img.reshape([h * w, ])
numberBins = 256
histogram, bins, patch = plt.hist(pixelSequence, numberBins,
facecolor='black', histtype='bar')
plt.xlabel("gray label")
plt.ylabel("number of pixels")
plt.axis([0, 255, 0, np.max(histogram)])
plt.show()
lena.shape #(512, 512, 3)
circle = []
#print(lena.min(),lena.max(),lena[:][500])
f1 = img
#print(f1.max(),f1.min())
thresh_points = 10 ##标记的点数目上限
thresh_rad = 50 ##标记的点之间距离阈值
def label_ij(f1,thresh_points,thresh_rad,circle):
for i in range(f1.shape[0]):
for j in range(f1.shape[1]):
if f1[i][j] == 255:
if len(circle)!= 0 and len(circle)
if (circle[k][0]-i)**2 + (circle[k][1]-j)**2
elif k == len(circle)-1: ###前面所有标记点距离该点都大于阈值,k表示轮流比较到了前面所有标记点的最后一个点
circle.append([i,j])
elif len(circle)==0:
circle.append([i,j])
else:
return circle
else:
pass
circle = label_ij(f1,thresh_points,thresh_rad,circle)
print(len(circle))
plt.figure()
#plt.plot(circle[:][0], circle[:][1], 'g*-')
for i in range(len(circle)):
plt.scatter(circle[i][0], circle[i][1],c='green',edgecolor='none',s=10)
plt.imshow(img,'gray') # 显示图片函数原型: 省略第二个参数,默认绘制彩图
plt.axis('on')
#plt.show()
#cv2.imwrite('/home/zhongjia/plasmabubble/data/test2.png',img)
plt.savefig('/home/zhongjia/plasmabubble/data/test2.png', dpi = 100)
#print(lena.shape,type(lena))
打开保存的结果图片:
当出现plt.shaow()函数时:
import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片
import numpy as np
import cv2
lena = cv2.imread('/home/data/test.png',2)
print(lena.shape,lena.dtype)
plt.imshow(lena,'gray')
plt.show()
img = lena
h, w = img.shape[:2]
pixelSequence = img.reshape([h * w, ])
numberBins = 256
histogram, bins, patch = plt.hist(pixelSequence, numberBins,
facecolor='black', histtype='bar')
plt.xlabel("gray label")
plt.ylabel("number of pixels")
plt.axis([0, 255, 0, np.max(histogram)])
plt.show()
lena.shape #(512, 512, 3)
circle = []
#print(lena.min(),lena.max(),lena[:][500])
f1 = img
#print(f1.max(),f1.min())
thresh_points = 10 ##标记的点数目上限
thresh_rad = 50 ##标记的点之间距离阈值
def label_ij(f1,thresh_points,thresh_rad,circle):
for i in range(f1.shape[0]):
for j in range(f1.shape[1]):
if f1[i][j] == 255:
if len(circle)!= 0 and len(circle)
if (circle[k][0]-i)**2 + (circle[k][1]-j)**2
elif k == len(circle)-1: ###前面所有标记点距离该点都大于阈值,k表示轮流比较到了前面所有标记点的最后一个点
circle.append([i,j])
elif len(circle)==0:
circle.append([i,j])
else:
return circle
else:
pass
circle = label_ij(f1,thresh_points,thresh_rad,circle)
print(len(circle))
plt.figure()
#plt.plot(circle[:][0], circle[:][1], 'g*-')
for i in range(len(circle)):
plt.scatter(circle[i][0], circle[i][1],c='green',edgecolor='none',s=10)
plt.imshow(img,'gray') # 显示图片函数原型: 省略第二个参数,默认绘制彩图
plt.axis('on')
plt.show()
#cv2.imwrite('/home/zhongjia/plasmabubble/data/test2.png',img)
plt.savefig('/home/zhongjia/plasmabubble/data/test2.png', dpi = 100)
#print(lena.shape,type(lena))
打开保存的结果图片:
可见保存图片是一片空白,plt.show()函数证明前面图像内容结束,后续代码已经与前面图像无关,此时保存图片内容为空。