# opencv 学习
# 加载、修改、保存图像
- 加载图像用 cv::imread
- 修改图像用 cv::cvtColor
- 保存图像用 cv::imwrite
# 加载图像用 cv::imread
- imread——Mat 对象
- IMREAD_UNCHANGED (<0)—— 加载原图
- IMREAD_GRAYSCALE (0)—— 加载灰度图
- IMREAD_COLOR (>0)—— 加载 RGB 图
- opencv 支持 JPG、PNG、TIFF 等常见格式
# 显示图像 (cv::namedWindos 与 cv::imshow)
# 修改图像用 cv::cvtColor
- cv::cvtColor—— 三个参数 (源图像,转换后图像,源和目标色彩空间)
- cv::cvtColor(image,gray_image,COLOR_BGR2GRAY)
# 读取图片实操
import cv2 as cv | |
img = cv.imread("/home/wlp/1.jfif") | |
cv.namedWindow("Image") | |
cv.imshow("Image", img) | |
cv.waitKey(0)#等待 | |
cv.destroyAllWindows() |

# 识别二维码并输出
import cv2 | |
import numpy as np | |
import sys | |
import time | |
if len(sys.argv)>1:#判断外部是否 chuan | |
inputImage = cv2.imread(sys.argv[1]) | |
else: | |
inputImage = cv2.imread("/home/wlp/opcv.png")#提取图片 | |
# Display barcode and QR code location | |
def display(im, bbox): | |
n = len(bbox) | |
for j in range(n): | |
cv2.line(im, tuple(bbox[j][0]), tuple(bbox[(j + 1) % n][0]), (255, 0, 0), 3) | |
# Display results | |
cv2.imshow("Results", im) | |
qrDecoder = cv2.QRCodeDetector() | |
# Detect and decode the qrcode | |
data, bbox, rectifiedImage = qrDecoder.detectAndDecode(inputImage)#解码 | |
if len(data) > 0: | |
print("Decoded Data : {}".format(data))#格式输出 | |
display(inputImage, bbox) | |
rectifiedImage = np.uint8(rectifiedImage); | |
cv2.imshow("Rectified QRCode", rectifiedImage); | |
else:#输出原图 | |
print("QR Code not detected") | |
cv2.imshow("Results", inputImage) | |
cv2.waitKey(0)#等待直到用户按下一个按键之后退出 | |
cv2.destroyAllWindows()#关闭窗口 |

# numpy
# 改变 imshow 窗口大小
通过鼠标拖动
import cv2 | |
import numpy | |
img1= cv2.imread("/home/wlp/1.jfif") | |
width=1200 | |
height=800 | |
cv2.namedWindow("img", 0); # 0w | |
cv2.resizeWindow("img", int(width * (height - 80) / height), height - 80); | |
cv2.imshow("img", img1) | |
cv2.waitKey() |
