sudo apt-get install libopencv-dev on Ubuntu: sudo apt-get install python3-opencv 在linux的shell中輸入: pip install Opencv-python 即可開始安裝。
import cv2 # 讀取image.jpg圖檔 img = cv2.imread('image.png') # 以灰階的格式讀進圖形檔案 img_gray = cv2.imread('image.png' , cv2.IMREAD_GRAYSCALE) # 彩色圖片轉為灰階圖片 img2=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # imshow 立即在視窗中展示圖片 cv2.imshow('Image1', img) cv2.imshow('Image2', img_gray) cv2.imshow('Image3', img2) # 等待鍵盤輸入任何鍵就執行下面的指令(關閉所有視窗) cv2.waitKey(0) # 關閉所有視窗 cv2.destroyAllWindows() # 將灰階的圖片寫入一個新的檔案 cv2.imwrite('image_gray.png', img_gray) #---------------- 移除所有註解後的原始程式------------ import cv2 img = cv2.imread('image.png') img_gray = cv2.imread('image.png' , cv2.IMREAD_GRAYSCALE) img2=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.imshow('Image1', img) cv2.imshow('Image2', img_gray) cv2.imshow('Image3', img2) cv2.waitKey(0) cv2.destroyAllWindows() cv2.imwrite('image_gray.png', img_gray) |
image.png |
import cv2 import numpy img = cv2.imread('image.png') print(type(img)) print(img.shape) img_gray = cv2.imread('image.png' , cv2.IMREAD_GRAYSCALE) print(type(img_gray)) print(img_gray.shape) (Lx,Ly)=img_gray.shape print(Lx,Ly) for i in range(0,Lx,10): for j in range(0,Ly,10): print(i,j,img[i][j][0],img[i][j][1],img[i][j][2]) |
|
import cv2 import numpy img = cv2.imread('image.png') print(type(img)) print(img.shape) img_gray = cv2.imread('image.png' , cv2.IMREAD_GRAYSCALE) print(type(img_gray)) print(img_gray.shape) (Lx,Ly)=img_gray.shape print(Lx,Ly) for i in range(0,Lx): for j in range(0,Ly): #將畫素中的顏色換成紅色,因此會產生一條"直"的紅線 if(j>80 and j<100): img[i][j][0]=0 img[i][j][1]=0 img[i][j][2]=255 cv2.imshow('img red',img) cv2.waitKey(0) cv2.destroyAllWindows() |
|
import cv2 import numpy as np img = cv2.imread("image.png") (Lx,Ly)=(img.shape[0],img.shape[1]) Lx2=int(Lx/2); Ly2=int(Ly/2) # 擷取部分的窗格 img1=img[0:Lx2,0:Ly2] img2=img[Lx2:Lx,Ly2:Ly] print(img.shape,img1.shape,img2.shape) vstack=np.vstack((img1,img2)) cv2.imshow('img', img) cv2.imshow('img1', img1) cv2.imshow('img2', img2) cv2.imshow('img1,2 vstack', vstack) cv2.waitKey(0) cv2.destroyAllWindows() |
(160, 202, 3) (80, 101, 3) (80, 101, 3) |
numpy array 的 merge (合併) 使用 vertical stack : vstack 垂直合併兩個 array 使用 horizontal stack : hstack 水平合併兩個 array [Python+OpenCV] 垂直vconcat 和水平hconcat 影像拼接。
import cv2 import numpy as np img = cv2.imread("image.png") #read-in an image file # 擷取部分的窗格 img1=img[0:50,0:50] img2=img[50:100,0:50] img3=img[0:50,50:100] img4=img[50:100,50:100] print(img.shape,img1.shape,img2.shape) tmp1=np.vstack((img1,img2)) tmp2=np.vstack((img3,img4)) tmp=np.hstack((tmp1,tmp2)) cv2.imshow('img1', img1) cv2.imshow('img2', img2) cv2.imshow('tmp1', tmp1) cv2.imshow('tmp2', tmp2) cv2.imshow('tmp', tmp) # vconcat 垂影像拼接跟 hconcat 水平影像拼接 image_v = cv2.vconcat([img2, tmp2]) print(tmp1.shape,tmp.shape,image_v.shape) cv2.imshow('imgv', image_v) cv2.waitKey(0) cv2.destroyAllWindows() |
# 寫入不同圖檔格式 cv2.imwrite('output.png', img) cv2.imwrite('output.tiff', img) # 設定 JPEG 圖片品質為 90(可用值為 0 ~ 100) cv2.imwrite('output.jpg', img, [cv2.IMWRITE_JPEG_QUALITY, 90]) # 設定 PNG 壓縮層級為 5(可用值為 0 ~ 9) cv2.imwrite('output.png', img, [cv2.IMWRITE_PNG_COMPRESSION, 5]) 我們可以利用numpy的陣列畫出我們想要的圖形。
import numpy as np import cv2 L=40 (x,y)=(L,L) print(x,y,x*y) new=np.ones((x,y)) new=new*255 print(new.shape) for i in range(2): for j in range(x): new[j][i]=0; for i in range(y-2,y): for j in range(x): new[j][i]=0; for j in range(2): for i in range(y): new[j][i]=0; for j in range(x-2,x): for i in range(y): new[j][i]=0; cv2.imwrite('cvnp-g1.png', new) |
40 40 1600 (40, 40) |
import numpy as np import cv2 L=80 (x,y)=(L,L) print(x,y,x*y) new=np.ones((x,y)) new=new*255 print(new.shape) for i in range(2): for j in range(x): new[j][i]=0; for i in range(y-2,y): for j in range(x): new[j][i]=0; for j in range(2): for i in range(y): new[j][i]=0; for j in range(x-2,x): for i in range(y): new[j][i]=0; R=L/4; NC=400 t=np.arange(0,2*np.pi,2*np.pi/NC) x=L/2+R*(np.cos(t)) y=L/2+R*(np.sin(t)) ix=x.astype(int) iy=y.astype(int) for i in range(len(t)): print(i,t[i],ix[i],iy[i]) new[ix[i]][iy[i]]=125 cv2.imwrite('cvnp-g1.png', new) |
40 40 1600 (40, 40) |
import numpy as np import cv2 # 建立一張 512x512 的 RGB 圖片(黑色) img = np.zeros((256, 256, 3), np.uint8) # 將圖片用淺灰色 (200, 200, 200) 填滿 img.fill(200) # 在圖片上畫一條紅色的對角線,寬度為 5 px cv2.line(img, (0, 0), (255, 255), (0, 0, 255), 5) # 顯示圖片 cv2.imshow('My Image', img) cv2.imwrite('cv2line.png', img) # 按下任意鍵則關閉所有視窗 cv2.waitKey(0) cv2.destroyAllWindows() |
import numpy as np import cv2 img = np.zeros((256, 256, 3), np.uint8) img.fill(200) # 在圖片上畫一條紅色的對角線,寬度為 5 px # cv2.line(影像, 開始座標, 結束座標, 顏色, 線條寬度) cv2.line(img, (0, 0), (255, 255), (0, 0, 255), 5) #cv2.rectangle(影像, 頂點座標, 對向頂點座標, 顏色, 線條寬度) cv2.rectangle(img, (20, 60), (120, 160), (0, 255, 0), 2) #cv2.circle(影像, 圓心座標, 半徑, 顏色, 線條寬度) cv2.circle(img,(90, 210), 30, (0, 255, 255), 3) cv2.circle(img,(140, 170), 15, (255, 0, 0), -1) cv2.ellipse(img, (180, 200), (25, 55), 45, 0, 360, (205, 0, 255), 2) cv2.ellipse(img, (180, 200), (20, 50), 45, 0, 180, (255, 0, 255), -1) cv2.putText(img, 'ov05.py', (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, cv2.LINE_AA) cv2.waitKey(0) cv2.destroyAllWindows() |
mport numpy as np import cv2 from matplotlib import pyplot as plt # 使用 OpenCV 讀取圖檔 img_bgr = cv2.imread('square-color.png') cv2.imshow('colorful', img_bgr) while True: if cv2.waitKey(0) & 0xFF == 27: cv2.destroyAllWindows(); break (X,Y,Z)=img_bgr.shape print(img_bgr.shape) t=np.linspace(0,4*np.pi,100) x=X/2+X/10*np.cos(t) y=Y/3+Y/10*np.sin(t) # 將 BGR 圖片轉為 RGB 圖片 #img_rgb = img_bgr[:,:,::-1] # 或是這樣亦可 img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB) # 使用 Matplotlib 顯示圖片 plt.imshow(img_bgr) plt.plot(x,y,'r-',lw=3) plt.xlabel('x', fontsize=15) plt.show() |
import numpy as np import cv2 # 圖片檔案讀入 img=cv2.imread('image.png') # 在圖片上畫一條紅色的對角線,寬度為 5 px cv2.line(img, (0, 0), (255, 255), (0, 0, 255), 5) cv2.rectangle(img, (20, 60), (120, 160), (0, 255, 0), 2) cv2.circle(img,(90, 210), 30, (0, 255, 255), 3) cv2.ellipse(img, (180, 200), (25, 55), 45, 0, 360, (205, 0, 255), 2) cv2.putText(img,'ov06.py',(40, 80),cv2.FONT_HERSHEY_SIMPLEX,1,(0, 0, 0),1,cv2.LINE_AA) cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows() cv2.imwrite('ov06.png',img) |
# coding=big5 # Python program to use openCV functions import cv2 src = cv2.imread("square-1.png") #read-in an image file gray = cv2.imread("square-1.png",cv2.IMREAD_GRAYSCALE) #convert it to a gray scale print(type(src)) print(len(src)) print(src.shape) (x,y,z)=src.shape print((x,y,z)) for i in range(0,x,4): if(src[i][50][0] > 233 ): cc='White' elif(src[i][50][0] > 163 and src[i][50][2] < 220 ): cc='BLUE' else: cc='Black' print(i,50,src[i][50][0],src[i][50][1],src[i][50][2], cc) |
|