Python中,如果你遇到了PIL图像保存有白边,CV2.polyline,fillpoly,参数问题,图像保存颜色发生异常这几个问题,这篇文章就能够解决你的疑惑。
第一个问题,plt图像保存有白边
首先,plt图像保存有白边,设置savefig里的参数和plt.tight_layout都无法真正的去除,plt适合画图表,有坐标值的这种。
即使按照这种方案https://blog.csdn.net/jifaley/article/details/79687000,也是没用的,为什么呢?首先,plt是封装了matlab中的plot包,发现matlab中也是有这个问题,matlab解决方案http://blog.sina.com.cn/s/blog_66d4b4620101fvph.html,因为plt.save()时是将整个窗口保存下来的
在python的引用格式:import matplotlib.pyplot as plt
#接下来在试了很多方法后无果
plt.axis('off')
subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)
#这样都是无法真正去除掉的
改用CV2就可以完美的解决这个问题,试一下后,不错~,因为opencv展示时,是按照图片的大小来展示的
im = cv2.imread('demo.jpg')
cv2.imshow('image',im)
cv2.imwrite('aa.jpg',img,[int(cv2.IMWRITE_JPEG_QUALITY),95])
cv2.waitKey(0)
cv2.destroyAllWindows()
第二个问题,CV2.polyline,fillpoly的参数
python中cv2.polyline和cv2.fillPoly参数如下:
很不错的参考链接关于poly的,https://blog.csdn.net/lkiller_hust/article/details/52949020
polyline中
第一个参数是img图像,表示你要在哪张图像上画线
pts表示的是点对,形式如下:
[[[334 33]]
[[ 44 255]]
[[444 222]]
[[ 33 333]]]
isClosed,布尔型,True表示的是线段闭合,False表示的是仅保留线段
color,线段颜色,格式是(R,G,B)值
thickness, 数值型,厚度,默认值为1,如果对封闭图形,正方形,三角形等传入-1,则会填充整个图形。
lineType,线型
shift,默认为0,
COCO_train2014_000000325939.jpg
举一个搞笑唧唧的例子
import cv2
import glob
import numpy as np
from PIL import Image
import matplotlib
import random
#im = np.array(Image.open('COCO_train2014_000000325939.jpg'))
im = cv2.imread('COCO_train2014_000000325939.jpg')
#w,h = im.size
#img = np.array(Image.new('RGB', (w, h), 'black'))
anns = [[184.83,393.26,186.02,387.29,186.62,386.1,195.57,381.92,197.36,380.73,200.93,377.75,205.71,374.17,
209.29,369.39,213.46,365.22,219.43,362.24,224.8,359.25,236.73,355.67,239.11,355.67,248.66,359.25,
252.83,362.83,257.01,365.82,261.78,367.6,274.31,370.59,288.62,375.96,301.15,378.94,310.7,382.52,
319.64,386.1,325.01,389.08,335.75,395.64,347.68,400.41,365.58,403.99,369.15,406.38,378.1,414.13,
388.24,420.1,400.17,427.26,409.72,433.82,410.91,435.61,389.44,427.26,376.91,426.66,368.56,426.66,
376.91,433.22,381.08,437.4,386.45,442.17,388.24,445.75,369.15,445.75,360.8,442.77,348.87,441.57,
333.96,442.17,322.63,442.77,310.7,443.96,299.96,446.94,291.61,451.71,290.41,454.1,289.22,463.05,289.22,473.79,
285.64,481.54,283.85,494.66,283.25,498.84,273.11,503.61,262.38,501.82,260.59,501.23,254.03,499.44,245.08,500.63,
230.76,497.65,235.53,495.86,245.08,495.26,252.83,494.07,259.39,491.68,262.38,487.51,262.97,483.93,265.36,479.15,
267.75,470.21,268.94,462.45,270.13,455.89,264.76,449.33,259.39,447.54,254.62,445.15,245.67,440.98,230.16,430.24,
224.2,426.66,218.83,422.48,215.25,413.54,214.06,409.96,213.46,406.38,213.46,398.03,212.87,393.26,207.5,390.87,
206.9,390.87,199.74,390.27,196.16,393.26,190.79,395.64,187.81,395.64],[334,33,44,255,444,222,33,333]
]
def showskt(img,anns):
#c = (np.random.random((1, 3))*0.6+0.4).tolist()[0]
if len(anns) == 0:
return 0
polygons = []
color = []
for ann in anns:
c = np.random.randint(0, 256, 3)
print(c)
#c = (np.random.random((1, 3)) * 0.6 + 0.4).tolist()[0]
pts = np.array(ann).reshape((int(len(ann) / 2), 2))
pts = pts.reshape((-1, 1, 2))
print(pts)
#img = cv2.fillPoly(im,np.int32([pts]),c,cv2.LINE_AA)
img = cv2.polylines(img, np.int32([pts]), True, c, -1, cv2.LINE_AA)
cv2.imshow('image',img)
cv2.imwrite('aa.jpg',img,[int(cv2.IMWRITE_JPEG_QUALITY),95])
cv2.waitKey(0)
cv2.destroyAllWindows()
showskt(im,anns)
看看效果吧,没有白边,线也画出来了,同样,分割的话,就去掉我注释掉的#
img = cv2.fillPoly(im,np.int32([pts]),c,cv2.LINE_AA)
就好了
混用CV和PIL图像读取,图像显示,发生保存颜色发生异常
这是由于plt和cv2图像的通道顺序是不一样的,所以交换第一通道和第三通道就可以了
import numpy as np
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('demo.jpg')
(r, g, b)=cv2.split(img)
img=cv2.merge([b,g,r])
plt.imshow(img)
plt.show()
网友评论