文章目录
- 1、opencv与PIL打开的图片的区别
- 2、cv2.error: OpenCV(4.5.5) : -1: error: (-5:Bad argument) in function ‘rectangle‘
- 3、报错TypeError: Expected Ptr<cv::UMat> for argument ‘src‘
- 4、cv2.resize()
- 5、关于PIL说的不错的链接
- 6、cv2.flip()和cv2.rotate()
- 7、BUG:【cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'】
- 8、cv2如何改变窗口的大小
- 9、cv2.rectangle()
- 10、openCV查看像素值
- 11、使用PIL crop图片
- 12、PIL库中Image.new方法和paste方法
- 13、opencv cv2.LUT()
- 14、CV2打开图片与Image打开图片相互转换
- 15、bug:【UnicodeDecodeError: 'gbk' codec can't decode byte 0x90 in position 207: illegal multibyte sequence】
- 16、cv2.getPerspectiveTransform 透视变换
- 17、cv2.drawContours() 轮廓绘制
- 18、Image旋转图片
- 19、transforms.Compose()函数
- 20、PIL图像处理之ImageFilter
1、opencv与PIL打开的图片的区别
1.1 使用opencv打开图片
1、如果图片是中文路径,则在cv2.imdecode中使用np.fromfile,如果是英文路径则使用cv2.imread()
2、opencv中的im.shape:(1080, 1920, 3),1080是height,1920是width,3是通道,
3、暂时没使用过im.size
代码:
import cv2
import numpy as np
path = r"C:\Users\9ling\Desktop\窗帘\粤东莞城区花园支行柜台外环境1_20220818165350879.jpg"
im = cv2.imdecode(np.fromfile(path), 1)
print(im.shape) # (1080, 1920, 3)
print(im.size) # 6220800
cv2.imshow("im", im)
cv2.imwrite("./test.jpg", im)
cv2.waitKey()
cv2.destroyAllWindows()
输出:
(1080, 1920, 3)
6220800
1.2 使用PIL.Image打开图片
1、Image打开图片使用的内部方法open;
2、im.size=(1920, 1080) ,1920是width,1080是height
3、使用plt.imshow()方法显示图片,我在pycharm中运行始终没有显示,但在jupyter可以显示,暂时不知道为啥
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
path = r"C:\Users\9ling\Desktop\窗帘\粤东莞城区花园支行柜台外环境1_20220818165350879.jpg"
im = Image.open(path)
print(im.size) # (1920, 1080)
print(np.shape(im)) # (1080, 1920, 3)
im.save(r"C:\Users\9ling\Desktop\chuanglian.jpg")
plt.imshow(im)
im.close()
输出:
(1920, 1080)
(1080, 1920, 3)
参考:链接
2、cv2.error: OpenCV(4.5.5) : -1: error: (-5:Bad argument) in function ‘rectangle‘
我卸载了我目前版本的opencv,版本为: 4.6.0.66,然后下载了这个版本,
查看opencv的版本:
pip show opencv_python
卸载opencv:
pip uninstall opencv_python
安装opencv:
pip install opencv_python==4.4.0.46
参考:链接
3、报错TypeError: Expected Ptr<cv::UMat> for argument ‘src‘
参考:链接
4、cv2.resize()
cv2.resize(image, (width, height), interpolation=INTER_LINEAR)
要注意第2个参数是:(width, height)
5、关于PIL说的不错的链接
6、cv2.flip()和cv2.rotate()
参考:链接
7、BUG:【cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function ‘cv::imwrite’】
出现的原因如下:
1、图片路径写入中文;
2、写入图片本身的错误 ,如截取图片大小超界;
参考链接:链接
8、cv2如何改变窗口的大小
只增加如下代码即可:
# 改变窗口大小
cv2.namedWindow("im", 0)
cv2.resizeWindow("im", 1066, 600)
cv2.imshow("im", im)
参考:链接
9、cv2.rectangle()
参考:链接
10、openCV查看像素值
import cv2
img= cv2.imread('290.png') #定义图片位置
#img= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #转化为灰度图 因为我读取的直接是灰度标签图片就不用转化了
def onmouse(event, x, y, flags, param): #标准鼠标交互函数
if event==cv2.EVENT_MOUSEMOVE: #当鼠标移动时
print(img[y,x]) #显示鼠标所在像素的数值,注意像素表示方法和坐标位置的不同
def main():
cv2.namedWindow("img") #构建窗口
cv2.setMouseCallback("img", onmouse) #回调绑定窗口
while True: #无限循环
cv2.imshow("img",img) #显示图像
if cv2.waitKey() == ord('q'):break #按下‘q'键,退出
cv2.destroyAllWindows() #关闭窗口
if __name__ == '__main__': #运行
main()
参考:链接
11、使用PIL crop图片
from PIL import Image
import matplotlib.pyplot as plt
plt.figure()
img = Image.open('pic/1.png')
# 0:与左边界的距离, 10:与上边界的距离,
# img.size[0] / 3:还是与左边界的距离, img.size[1] / 4:还是与上边界的距离
img_new = img.crop((0, 10, img.size[0] / 3, img.size[1] / 4))
plt.subplot(1,2,1)
plt.imshow(img)
plt.subplot(1,2,2)
plt.imshow(img_new)
plt.show()
参考:链接
12、PIL库中Image.new方法和paste方法
13、opencv cv2.LUT()
代码:
import cv2
import numpy as np
# img=np.array(([[[1,2,3],[1,2,3],[1,2,3]],[[1,2,3],[1,2,3],[1,2,3]]])).astype(np.uint8)
# img=np.array(([[1,2,3,4,5,6]])).astype(np.uint8)
img=np.array([1,2,3,4,5,6]).astype(np.uint8)
print(img.shape) # (6,)
print(img.dtype) # uint8
gamma=0.8
# 映射表必须为0-255(改成其他会报错)
gamma_table = [np.power(x / 255.0, gamma) * 255.0 for x in range(256)]
# numpy数组默认数据类型为int32,需要将数据类型转换成opencv图像适合使用的无符号8位整型uint8,否则会报错
gamma_table = np.round(np.array(gamma_table)).astype(np.uint8)
print(gamma_table)
img_gamma = cv2.LUT(img, gamma_table)
print(img_gamma)
输出:
gamma_table:
[ 0 3 5 7 9 11 13 14 16 18 19 21 22 24 25 26 28 29
31 32 33 35 36 37 39 40 41 42 44 45 46 47 48 50 51 52
53 54 56 57 58 59 60 61 63 64 65 66 67 68 69 70 71 73
74 75 76 77 78 79 80 81 82 83 84 85 86 88 89 90 91 92
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
111 112 113 114 115 116 117 118 119 120 121 122 123 123 124 125 126 127
128 129 130 131 132 133 134 135 136 137 138 139 140 140 141 142 143 144
145 146 147 148 149 150 151 151 152 153 154 155 156 157 158 159 160 161
161 162 163 164 165 166 167 168 169 169 170 171 172 173 174 175 176 177
177 178 179 180 181 182 183 183 184 185 186 187 188 189 190 190 191 192
193 194 195 196 196 197 198 199 200 201 202 202 203 204 205 206 207 207
208 209 210 211 212 212 213 214 215 216 217 217 218 219 220 221 222 222
223 224 225 226 227 227 228 229 230 231 232 232 233 234 235 236 236 237
238 239 240 240 241 242 243 244 245 245 246 247 248 249 249 250 251 252
253 253 254 255]
img_gamma:
[[ 3]
[ 5]
[ 7]
[ 9]
[11]
[13]]
参考:链接
14、CV2打开图片与Image打开图片相互转换
代码:
#1.Image对象->cv2(np.adarray)
img = Image.open(path)
img_array = np.array(img)
#2.cv2(np.adarray)->Image对象
img = cv2.imread(path)
img_Image = Image.fromarray(np.uint8(img))
15、bug:【UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x90 in position 207: illegal multibyte sequence】
有时候打开的文件里面有中文,就会报UncodeDecodeError:‘gbk’ codec can’t decode byte的错误。
解决办法,在打开文件句柄的时候,增加encoding='UTF-8’就可以了。
fd = open('fileName', 'r', encoding='UTF-8')
16、cv2.getPerspectiveTransform 透视变换
参考:链接1,链接2,这个链接3,链接4也不错,此链接5也不错
17、cv2.drawContours() 轮廓绘制
参考:链接
18、Image旋转图片
19、transforms.Compose()函数
参考:链接1
20、PIL图像处理之ImageFilter
参考:链接1
版权声明:本文为qq_23022733原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。