opencv

imread()读图片

import numpy as np
import cv2

img = cv2.imread('1.jpg')
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

imwrite()保存图片

cv2.imwrite('1.jpg',img)

using Matplotlib

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('1.jpg')
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])
plt.show()

OpenCV follows BGR order, while matplotlib likely follows RGB order.

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('messi4.jpg')
b,g,r = cv2.split(img)
img2 = cv2.merge([r,g,b])
plt.subplot(121);plt.imshow(img) # expects distorted color
plt.subplot(122);plt.imshow(img2) # expect true color
plt.show()

cv2.imshow('bgr image',img) # expects true color
cv2.imshow('rgb image',img2) # expects distorted color
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2画各种图形

import cv2
import numpy as np


img = np.zeros((300,300,3), np.uint8)

cv2.line(img, (0,0),(200,200),(124,23,24),5)
cv2.rectangle(img,(33,0),(133,123),(0,255,0),3)
cv2.circle(img,(100,23),20,(0,0,255),-1)
cv2.ellipse(img,(130,130),(100,50),0,0,180,255,-1)
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,(0,255,255))
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(10,200), font, 2,(255,255,255),2,cv2.LINE_AA)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:
opencv_第1张图片

你可能感兴趣的:(python)