【OpenCV】Chapter10.色彩转换与图像绘制
最近想對OpenCV進行系統學習,看到網上這份教程寫得不錯,于是跟著來學習實踐一下。
【youcans@qq.com, youcans 的 OpenCV 例程, https://youcans.blog.csdn.net/article/details/125112487】
程序倉庫:https://github.com/zstar1003/OpenCV-Learning
色彩轉換
顏色空間轉換
常見的色彩空間包括:GRAY 色彩空間(灰度圖像)、XYZ 色彩空間、YCrCb 色彩空間、HSV 色彩空間、HLS 色彩空間、CIELab 色彩空間、CIELuv 色彩空間、Bayer 色彩空間等。
色彩空間名詞解釋:
- RGB:紅色(Red)、綠色(Green)、藍色(Blue);
- HSV/HSB:色調(Hue)、飽和度(Saturation)和明度(Value/Brightness);
- HSl:色調(Hue)、飽和度(Saturation)和灰度(Intensity);
- HSL:包括色調(Hue)、飽和度(Saturation)和亮度(Luminance/Lightness)
常見色彩空間轉換,這里只列舉兩個常見的。
-
RGB -> GRAY
注意RGB可以轉灰度,灰度不能轉RGB
轉換公式:gray = 0.299 x R + 0.587 x G + 0.114 x B -
RGB -> HSV
RGB轉HSV公式為
OpenCV提供了函數cv.cvtColor()可以將圖像從一個顏色空間轉換為另一個顏色空間。
cv.cvtColor(src, code [, dst, dstCn]]) → dst
參數說明:
- src:輸入圖像,nparray 多維數組,8位無符號/ 16位無符號/單精度浮點數格式
- code:顏色空間轉換代碼,詳見 ColorConversionCodes
- dst:輸出圖像,大小和深度與 src 相同
- dstCn:輸出圖像的通道數,0 表示由src和code自動計算
示例程序:
""" 顏色空間轉換 """ import cv2 as cv import matplotlib.pyplot as plt import numpy as npimgBGR = cv.imread("../img/img.jpg", flags=1)imgRGB = cv.cvtColor(imgBGR, cv.COLOR_BGR2RGB) # BGR 轉換為 RGB, 用于 PyQt5, matplotlib imgGRAY = cv.cvtColor(imgBGR, cv.COLOR_BGR2GRAY) # BGR 轉換為灰度圖像 imgHSV = cv.cvtColor(imgBGR, cv.COLOR_BGR2HSV) # BGR 轉換為 HSV 圖像 imgYCrCb = cv.cvtColor(imgBGR, cv.COLOR_BGR2YCrCb) # BGR轉YCrCb imgHLS = cv.cvtColor(imgBGR, cv.COLOR_BGR2HLS) # BGR 轉 HLS 圖像 imgXYZ = cv.cvtColor(imgBGR, cv.COLOR_BGR2XYZ) # BGR 轉 XYZ 圖像 imgLAB = cv.cvtColor(imgBGR, cv.COLOR_BGR2LAB) # BGR 轉 LAB 圖像 imgYUV = cv.cvtColor(imgBGR, cv.COLOR_BGR2YUV) # BGR 轉 YUV 圖像# 調用matplotlib顯示處理結果 titles = ['BGR', 'RGB', 'GRAY', 'HSV', 'YCrCb', 'HLS', 'XYZ', 'LAB', 'YUV'] images = [imgBGR, imgRGB, imgGRAY, imgHSV, imgYCrCb,imgHLS, imgXYZ, imgLAB, imgYUV] plt.figure(figsize=(10, 8)) for i in range(9):plt.subplot(3, 3, i + 1), plt.imshow(images[i], 'gray')plt.title(titles[i])plt.xticks([]), plt.yticks([]) plt.tight_layout() plt.show()顏色反轉
圖像顏色反轉也稱為反色變換,是像素顏色的逆轉,將黑色像素點變白色,白色像素點變黑色,像素位置不變。
RGB圖片實現顏色反轉非常容易,一種簡單的思路就是對每個像素點用255-顏色值。但是這樣處理的效率不高。
OpenCV提供了一個查表函數cv.LUT可以快速實現像素值的改變。其本質就是先對每個0-255的像素灰度值建立一個變換字典,這樣處理像素值就只需要從字典里去查找對應的數據進行替換,而無需再去運算。
下面的示例程序比較了兩種方法的執行效率。
""" 圖像顏色反轉 """ import cv2 as cv import matplotlib.pyplot as plt import numpy as npimg = cv.imread("../img/img.jpg", flags=1) h, w, ch = img.shape # 圖片的高度, 寬度 和通道數timeBegin = cv.getTickCount() imgInv = np.empty((w, h, ch), np.uint8) # 創建空白數組 for i in range(h):for j in range(w):for k in range(ch):imgInv[i][j][k] = 255 - img[i][j][k] timeEnd = cv.getTickCount() time = (timeEnd - timeBegin) / cv.getTickFrequency() print("圖像反轉(for 循環實現): {} s".format(round(time, 4)))timeBegin = cv.getTickCount() transTable = np.array([(255 - i) for i in range(256)]).astype("uint8") invLUT = cv.LUT(img, transTable) timeEnd = cv.getTickCount() time = (timeEnd - timeBegin) / cv.getTickFrequency() print("圖像反轉(LUT 查表實現): {} s".format(round(time, 4)))plt.figure(figsize=(9, 6)) plt.subplot(131), plt.title("img"), plt.axis('off') plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB)) plt.subplot(132), plt.title("imgInv"), plt.axis('off') plt.imshow(cv.cvtColor(imgInv, cv.COLOR_BGR2RGB)) plt.subplot(133), plt.title("invLUT"), plt.axis('off') plt.imshow(cv.cvtColor(invLUT, cv.COLOR_BGR2RGB)) plt.tight_layout() plt.show()輸出
圖像反轉(for 循環實現): 1.9181 s
圖像反轉(LUT 查表實現): 0.0326 s
由此可見兩者速度差異還是比較明顯的。
色彩風格濾鏡
色彩風格濾鏡就是OpenCV提供了一些色彩搭配方案,通過函數cv.applyColorMap可以進行調用。
OpenCV 提供了 22 種色彩風格類型:
ColorMaps[] = { "Autumn", "Bone", "Jet", "Winter", "Rainbow", "Ocean", "Summer", "Spring","Cool", "HSV", "Pink", "Hot", "Parula", "Magma", "Inferno", "Plasma", "Viridis","Cividis", "Twilight", "Twilight Shifted", "Turbo", "Deep Green"};示例程序:
""" 色彩風格濾鏡 """ import cv2 as cv import matplotlib.pyplot as plt import numpy as npimg = cv.imread("../img/img.jpg", flags=1)# 偽彩色處理 pseudo1 = cv.applyColorMap(img, colormap=cv.COLORMAP_PINK) pseudo2 = cv.applyColorMap(img, colormap=cv.COLORMAP_JET) pseudo3 = cv.applyColorMap(img, colormap=cv.COLORMAP_WINTER) pseudo4 = cv.applyColorMap(img, colormap=cv.COLORMAP_RAINBOW) pseudo5 = cv.applyColorMap(img, colormap=cv.COLORMAP_HOT)plt.figure(figsize=(9, 6)) plt.subplot(231), plt.axis('off'), plt.title("Origin") plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB)) plt.subplot(232), plt.axis('off'), plt.title("cv.COLORMAP_PINK") plt.imshow(cv.cvtColor(pseudo1, cv.COLOR_BGR2RGB)) plt.subplot(233), plt.axis('off'), plt.title("cv.COLORMAP_JET") plt.imshow(cv.cvtColor(pseudo2, cv.COLOR_BGR2RGB)) plt.subplot(234), plt.axis('off'), plt.title("cv.COLORMAP_WINTER") plt.imshow(cv.cvtColor(pseudo3, cv.COLOR_BGR2RGB)) plt.subplot(235), plt.axis('off'), plt.title("cv.COLORMAP_RAINBOW") plt.imshow(cv.cvtColor(pseudo4, cv.COLOR_BGR2RGB)) plt.subplot(236), plt.axis('off'), plt.title("cv.COLORMAP_HOT") plt.imshow(cv.cvtColor(pseudo5, cv.COLOR_BGR2RGB)) plt.tight_layout() plt.show()調節色彩
通過cv.LUT可以在RGB色彩范圍內調節三通道的數值,從而調節色彩。
下面的示例程序將各通道的最大值設置為maxG,將某顏色通道的色階從 0-255 映射到 0-maxG,就可以使該顏色通道的色彩衰減。
示例程序:
""" 調節色彩 """ import cv2 as cv import matplotlib.pyplot as plt import numpy as npimg = cv.imread("../img/img.jpg", flags=1)maxG = 128 # 修改顏色通道最大值,0<=maxG<=255 lutHalf = np.array([int(i * maxG / 255) for i in range(256)]).astype("uint8") lutEqual = np.array([i for i in range(256)]).astype("uint8")lut3HalfB = np.dstack((lutHalf, lutEqual, lutEqual)) # (1,256,3), B_half/BGR lut3HalfG = np.dstack((lutEqual, lutHalf, lutEqual)) # (1,256,3), G_half/BGR lut3HalfR = np.dstack((lutEqual, lutEqual, lutHalf)) # (1,256,3), R_half/BGRblendHalfB = cv.LUT(img, lut3HalfB) # B 通道衰減 50% blendHalfG = cv.LUT(img, lut3HalfG) # G 通道衰減 50% blendHalfR = cv.LUT(img, lut3HalfR) # R 通道衰減 50%plt.figure(figsize=(9, 5)) plt.subplot(131), plt.axis('off'), plt.title("B half decayed") plt.imshow(cv.cvtColor(blendHalfB, cv.COLOR_BGR2RGB)) plt.subplot(132), plt.axis('off'), plt.title("G half decayed") plt.imshow(cv.cvtColor(blendHalfG, cv.COLOR_BGR2RGB)) plt.subplot(133), plt.axis('off'), plt.title("R half decayed") plt.imshow(cv.cvtColor(blendHalfR, cv.COLOR_BGR2RGB)) plt.tight_layout() plt.show()調節飽和度和明度
將RGB顏色空間轉換到HSV空間,可以調整圖片的飽和度和明度。
示例程序:
""" 調節飽和度和明度 """ import cv2 as cv import matplotlib.pyplot as plt import numpy as npimg = cv.imread("../img/img.jpg", flags=1) hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV) # 色彩空間轉換, BGR->HSV# 調節通道強度 lutWeaken = np.array([int(0.6 * i) for i in range(256)]).astype("uint8") lutEqual = np.array([i for i in range(256)]).astype("uint8") lutRaisen = np.array([int(102 + 0.6 * i) for i in range(256)]).astype("uint8") # 調節飽和度 lutSWeaken = np.dstack((lutEqual, lutWeaken, lutEqual)) # Saturation weaken lutSRaisen = np.dstack((lutEqual, lutRaisen, lutEqual)) # Saturation raisen # 調節明度 lutVWeaken = np.dstack((lutEqual, lutEqual, lutWeaken)) # Value weaken lutVRaisen = np.dstack((lutEqual, lutEqual, lutRaisen)) # Value raisenblendSWeaken = cv.LUT(hsv, lutSWeaken) # 飽和度降低 blendSRaisen = cv.LUT(hsv, lutSRaisen) # 飽和度增大 blendVWeaken = cv.LUT(hsv, lutVWeaken) # 明度降低 blendVRaisen = cv.LUT(hsv, lutVRaisen) # 明度升高plt.figure(figsize=(9, 6)) plt.subplot(231), plt.axis('off'), plt.title("Saturation weaken") plt.imshow(cv.cvtColor(blendSWeaken, cv.COLOR_HSV2RGB)) plt.subplot(232), plt.axis('off'), plt.title("Normal saturation") plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB)) plt.subplot(233), plt.axis('off'), plt.title("Saturation raisen") plt.imshow(cv.cvtColor(blendSRaisen, cv.COLOR_HSV2RGB)) plt.subplot(234), plt.axis('off'), plt.title("Value weaken") plt.imshow(cv.cvtColor(blendVWeaken, cv.COLOR_HSV2RGB)) plt.subplot(235), plt.axis('off'), plt.title("Normal value") plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB)) plt.subplot(236), plt.axis('off'), plt.title("Value raisen") plt.imshow(cv.cvtColor(blendVRaisen, cv.COLOR_HSV2RGB)) plt.tight_layout() plt.show()圖像繪制
繪制直線
函數cv.line()繪制圖像中點pt1與點pt2之間的線段
函數cv.arrowedLine()繪制圖像中點pt1與點pt2之間的帶箭頭線段
cv.line(img, pt1, pt2, color[, thickness=1, lineType=LINE_8, shift=0]) → img
cv.arrowedLine(img, pt1, pt2, color[, thickness=1, line_type=8, shift=0, tipLength=0.1]) → img
參數說明:
- img:輸入輸出圖像,允許單通道灰度圖像或多通道彩色圖像
- pt1:線段第一個點的坐標,(x1, y1)
- pt2:線段第二個點的坐標,(x2, y2)
- tipLength:箭頭部分長度與線段長度的比例,默認為 0.1
示例程序:
""" 繪制直線 """ import cv2 as cv import matplotlib.pyplot as plt import numpy as npheight, width, channels = 200, 120, 3 img = np.ones((height, width, channels), np.uint8) * 160 # 創建黑色圖像 RGB=0# 注意 pt1, pt2 坐標的格式是 (x,y) 而不是 (y,x) img1 = img.copy() cv.line(img1, (0, 0), (200, 150), (0, 0, 255), 1) # 紅色 R=255 cv.line(img1, (0, 0), (150, 200), (0, 255, 0), 1) # 綠色 G=255 cv.line(img1, (0, 50), (200, 50), (128, 0, 0), 2) # 深藍色 B = 128 cv.line(img1, (0, 100), (200, 100), 128, 2) # color=128 等效于 (128,0,0) cv.line(img1, (0, 150), (200, 150), 255, 2) # color=255 等效于 (255,0,0)# img2 = img.copy() # tipLength 指箭頭部分長度與整個線段長度的比例 img2 = cv.arrowedLine(img.copy(), (10, 0), (100, 30), (0, 0, 255), tipLength=0.05) # 從 pt1 指向 pt2 img2 = cv.arrowedLine(img2, (10, 50), (100, 80), (0, 0, 255), tipLength=0.1) img2 = cv.arrowedLine(img2, (10, 100), (100, 130), (0, 0, 255), tipLength=0.2) # 雙向箭頭 img2 = cv.arrowedLine(img2, (100, 130), (10, 100), (0, 0, 255), tipLength=0.2) # 雙向箭頭 img2 = cv.arrowedLine(img2, (10, 150), (200, 200), (0, 0, 255), tipLength=0.1) # 終點越界,箭頭不顯示# 繪制直線可以用于灰度圖像,參數 color 只有第一通道值有效,并被設為灰度值 gray = np.zeros((height, width), np.uint8) # 創建灰度圖像 img3 = cv.line(gray, (0, 10), (200, 10), (0, 255, 255), 2) img3 = cv.line(gray, (0, 30), (200, 30), (64, 128, 255), 2) img3 = cv.line(gray, (0, 60), (200, 60), (128, 64, 255), 2) img3 = cv.line(gray, (0, 100), (200, 100), (255, 0, 255), 2) img3 = cv.line(gray, (20, 0), (20, 200), 128, 2) img3 = cv.line(gray, (60, 0), (60, 200), (255, 0, 0), 2) img3 = cv.line(gray, (100, 0), (100, 200), (255, 255, 255), 2)plt.figure(figsize=(9, 6)) plt.subplot(131), plt.title("img1"), plt.axis('off') plt.imshow(cv.cvtColor(img1, cv.COLOR_BGR2RGB)) plt.subplot(132), plt.title("img2"), plt.axis('off') plt.imshow(cv.cvtColor(img2, cv.COLOR_BGR2RGB)) plt.subplot(133), plt.title("img3"), plt.axis('off') plt.imshow(img3, cmap="gray") plt.tight_layout() plt.show()繪制矩形
函數cv.rectangle()用來在圖像上繪制垂直于圖像邊界的矩形
cv.rectangle(img, pt1, pt2, color[, thickness=1, lineType=LINE_8, shift=0]) → img
cv.rectangle(img, rec, color[, thickness=1, lineType=LINE_8, shift=0]) → img
參數說明:
- img:輸入輸出圖像,允許單通道灰度圖像或多通道彩色圖像
- pt1:矩陣第一個點的坐標,(x1, y1) 格式的元組
- pt2:與 pt1 成對角的矩陣第二個點的坐標,(x2, y2) 格式的元組
- color:繪圖線條的顏色,(b,g,r) 格式的元組,或者表示灰度值的標量
- thickness:繪制矩形的線寬,默認值 1px,負數表示矩形內部填充
- lineType:繪制線段的線性,默認為 LINE_8
- shift:點坐標的小數位數,默認為 0
繪制傾斜矩形
cv.rectangle()只能繪制垂直的矩形,如果需要繪制傾斜矩形,需要繪制多條直線。
示例程序:
""" 繪制傾斜矩形 """ import cv2 as cv import matplotlib.pyplot as plt import numpy as npheight, width, channels = 600, 400, 3 img = np.ones((height, width, channels), np.uint8) * 192 # 創建黑色圖像 RGB=0# 圍繞矩形中心旋轉 x, y, w, h = (100, 200, 200, 100) # 左上角坐標 (x,y), 寬度 w,高度 h cx, cy = x + w // 2, y + h // 2 # 矩形中心 img1 = img.copy() cv.circle(img1, (cx, cy), 4, (0, 0, 255), -1) # 旋轉中心 angle = [15, 30, 45, 60, 75, 90] # 旋轉角度,順時針方向 for i in range(len(angle)):ang = angle[i] * np.pi / 180x1 = int(cx + (w / 2) * np.cos(ang) - (h / 2) * np.sin(ang))y1 = int(cy + (w / 2) * np.sin(ang) + (h / 2) * np.cos(ang))x2 = int(cx + (w / 2) * np.cos(ang) + (h / 2) * np.sin(ang))y2 = int(cy + (w / 2) * np.sin(ang) - (h / 2) * np.cos(ang))x3 = int(cx - (w / 2) * np.cos(ang) + (h / 2) * np.sin(ang))y3 = int(cy - (w / 2) * np.sin(ang) - (h / 2) * np.cos(ang))x4 = int(cx - (w / 2) * np.cos(ang) - (h / 2) * np.sin(ang))y4 = int(cy - (w / 2) * np.sin(ang) + (h / 2) * np.cos(ang))color = (30 * i, 0, 255 - 30 * i)cv.line(img1, (x1, y1), (x2, y2), color)cv.line(img1, (x2, y2), (x3, y3), color)cv.line(img1, (x3, y3), (x4, y4), color)cv.line(img1, (x4, y4), (x1, y1), color)# 圍繞矩形左上頂點旋轉 x, y, w, h = (200, 200, 200, 100) # 左上角坐標 (x,y), 寬度 w,高度 h img2 = img.copy() cv.circle(img2, (x, y), 4, (0, 0, 255), -1) # 旋轉中心 angle = [15, 30, 45, 60, 75, 90, 120, 150, 180, 225] # 旋轉角度,順時針方向 for i in range(len(angle)):ang = angle[i] * np.pi / 180x1, y1 = x, yx2 = int(x + w * np.cos(ang))y2 = int(y + w * np.sin(ang))x3 = int(x + w * np.cos(ang) - h * np.sin(ang))y3 = int(y + w * np.sin(ang) + h * np.cos(ang))x4 = int(x - h * np.sin(ang))y4 = int(y + h * np.cos(ang))color = (30 * i, 0, 255 - 30 * i)cv.line(img2, (x1, y1), (x2, y2), color)cv.line(img2, (x2, y2), (x3, y3), color)cv.line(img2, (x3, y3), (x4, y4), color)cv.line(img2, (x4, y4), (x1, y1), color)plt.figure(figsize=(9, 6)) plt.subplot(121), plt.title("img1"), plt.axis('off') plt.imshow(cv.cvtColor(img1, cv.COLOR_BGR2RGB)) plt.subplot(122), plt.title("img2"), plt.axis('off') plt.imshow(cv.cvtColor(img2, cv.COLOR_BGR2RGB)) plt.show()繪制圓形
函數cv.circle()用來在圖像上繪制圓形
cv.circle(img, center, radius, color[, thickness=1, lineType=LINE_8, shift=0]) → img
參數說明:
- img:輸入輸出圖像,允許單通道灰度圖像或多通道彩色圖像
- center:圓心點的坐標,(x, y) 格式的元組
- radius:圓的半徑,整數
- color:繪圖線條的顏色,(b,g,r) 格式的元組,或者表示灰度值的標量
- thickness:繪制矩形的線寬,默認值 1px,負數表示矩形內部填充
- lineType:繪制線段的線性,默認為 LINE_8
- cv.LINE_4:4 鄰接線型
- cv.LINE_8:8 鄰接線型
- cv.LINE_AA:抗鋸齒線型,圖像更平滑
- shift:點坐標的小數位數,默認為 0
示例程序:
""" 繪制圓形 """ import cv2 as cv import matplotlib.pyplot as plt import numpy as npimg = np.ones((400, 600, 3), np.uint8) * 192center = (0, 0) # 圓心坐標 cx, cy = 300, 200 # 圓心坐標 for r in range(200, 0, -20):color = (r, r, 255 - r)cv.circle(img, (cx, cy), r, color, -1)cv.circle(img, center, r, 255)cv.circle(img, (600, 400), r, color, 5)plt.figure(figsize=(6, 4)) plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB)) plt.axis('off') plt.show()繪制橢圓
函數cv.ellipse()用來在圖像上繪制橢圓輪廓、填充橢圓、橢圓弧或填充橢圓扇區
cv.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness=1, lineType=LINE_8, shift=0]) → img
cv.ellipse(img, box, color[, thickness=1, lineType=LINE_8]) → img
參數說明:
- img:輸入輸出圖像,允許單通道灰度圖像或多通道彩色圖像
- center:橢圓中心點的坐標,(x, y) 格式的元組
- axes:橢圓半軸長度,(hfirst, hsecond) 格式的元組
- angle: 橢圓沿 x軸方向的旋轉角度(角度制,順時針方向)
- startAngle:繪制的起始角度
- endAngle:繪制的終止角度
- color:繪圖線條的顏色,(b,g,r) 格式的元組,或者表示灰度值的標量
- thickness:繪制矩形的線寬,默認值 1px,負數表示矩形內部填充
- lineType:繪制線段的線性,默認為 LINE_8
- shift:點坐標的小數位數,默認為 0
示例程序:
""" 繪制橢圓 """ import cv2 as cv import matplotlib.pyplot as plt import numpy as npimg = np.ones((600, 400, 3), np.uint8) * 224 img1 = img.copy() img2 = img.copy()# (1) 半軸長度 (haf) 的影響 cx, cy = 200, 150 # 圓心坐標 angle = 30 # 旋轉角度 startAng, endAng = 0, 360 # 開始角度,結束角度 haf = [50, 100, 150, 180] # 第一軸的半軸長度 has = 100 # 第二軸的半軸長度 for i in range(len(haf)):color = (i * 50, i * 50, 255 - i * 50)cv.ellipse(img1, (cx, cy), (haf[i], has), angle, startAng, endAng, color, 2)angPi = angle * np.pi / 180 # 轉換為弧度制,便于計算坐標xe = int(cx + haf[i] * np.cos(angPi))ye = int(cy + haf[i] * np.sin(angPi))cv.circle(img1, (xe, ye), 2, color, -1)cv.arrowedLine(img1, (cx, cy), (xe, ye), color) # 從圓心指向第一軸端點text = "haF={}".format(haf[i])cv.putText(img1, text, (xe + 5, ye), cv.FONT_HERSHEY_SIMPLEX, 0.5, color) # 繪制第二軸 xe = int(cx + has * np.sin(angPi)) # 計算第二軸端點坐標 ye = int(cy - has * np.cos(angPi)) cv.arrowedLine(img1, (cx, cy), (xe, ye), color) # 從圓心指向第二軸端點 text = "haS={}".format(has) cv.putText(img1, text, (xe + 5, ye), cv.FONT_HERSHEY_SIMPLEX, 0.5, color)# (2) 旋轉角度 (angle) 的影響 cx, cy = 200, 450 # 圓心坐標 haf, has = 120, 50 # 半軸長度 startAng, endAng = 0, 360 # 開始角度,結束角度 angle = [0, 30, 60, 135] # 旋轉角度 for i in range(len(angle)):color = (i * 50, i * 50, 255 - i * 50)cv.ellipse(img1, (cx, cy), (haf, has), angle[i], startAng, endAng, color, 2)angPi = angle[i] * np.pi / 180 # 轉換為弧度制,便于計算坐標xe = int(cx + haf * np.cos(angPi))ye = int(cy + haf * np.sin(angPi))cv.circle(img1, (xe, ye), 2, color, -1)cv.arrowedLine(img1, (cx, cy), (xe, ye), color) # 從圓心指向第一軸端點text = "rotate {}".format(angle[i])cv.putText(img1, text, (xe + 5, ye), cv.FONT_HERSHEY_SIMPLEX, 0.5, color)# (3) 起始角度 (startAngle) 的影響 I cx, cy = 50, 80 # 圓心坐標 haf, has = 40, 30 # 半軸長度 angle = 0 # 旋轉角度 endAng = 360 # 結束角度 startAng = [0, 45, 90, 180] # 開始角度 for i in range(len(startAng)):color = (i * 20, i * 20, 255 - i * 20)cxi = cx + i * 100cv.ellipse(img2, (cxi, cy), (haf, has), angle, startAng[i], endAng, color, 2)angPi = angle * np.pi / 180 # 轉換為弧度制,便于計算坐標xe = int(cxi + haf * np.cos(angPi))ye = int(cy + haf * np.sin(angPi))cv.arrowedLine(img2, (cxi, cy), (xe, ye), 255) # 從圓心指向第一軸端點text = "start {}".format(startAng[i])cv.putText(img2, text, (cxi - 40, cy), cv.FONT_HERSHEY_SIMPLEX, 0.5, color) text = "end={}".format(endAng) cv.putText(img2, text, (10, cy - 40), cv.FONT_HERSHEY_SIMPLEX, 0.5, 255)# (4) 起始角度 (startAngle) 的影響 II cx, cy = 50, 200 # 圓心坐標 haf, has = 40, 30 # 半軸長度 angle = 30 # 旋轉角度 endAng = 360 # 結束角度 startAng = [0, 45, 90, 180] # 開始角度 for i in range(len(startAng)):color = (i * 20, i * 20, 255 - i * 20)cxi = cx + i * 100cv.ellipse(img2, (cxi, cy), (haf, has), angle, startAng[i], endAng, color, 2)angPi = angle * np.pi / 180 # 轉換為弧度制,便于計算坐標xe = int(cxi + haf * np.cos(angPi))ye = int(cy + haf * np.sin(angPi))cv.arrowedLine(img2, (cxi, cy), (xe, ye), 255) # 從圓心指向第一軸端點text = "start {}".format(startAng[i])cv.putText(img2, text, (cxi - 40, cy), cv.FONT_HERSHEY_SIMPLEX, 0.5, color) text = "end={}".format(endAng) cv.putText(img2, text, (10, cy - 40), cv.FONT_HERSHEY_SIMPLEX, 0.5, 255)# (5) 結束角度 (endAngle) 的影響 I cx, cy = 50, 320 # 圓心坐標 haf, has = 40, 30 # 半軸長度 angle = 0 # 旋轉角度 startAng = 0 # 開始角度 endAng = [45, 90, 180, 360] # 結束角度 for i in range(len(endAng)):color = (i * 20, i * 20, 255 - i * 20)cxi = cx + i * 100cv.ellipse(img2, (cxi, cy), (haf, has), angle, startAng, endAng[i], color, 2)angPi = angle * np.pi / 180 # 轉換為弧度制,便于計算坐標xe = int(cxi + haf * np.cos(angPi))ye = int(cy + haf * np.sin(angPi))cv.arrowedLine(img2, (cxi, cy), (xe, ye), 255) # 從圓心指向第一軸端點text = "end {}".format(endAng[i])cv.putText(img2, text, (cxi - 40, cy), cv.FONT_HERSHEY_SIMPLEX, 0.5, color) text = "start={}".format(startAng) cv.putText(img2, text, (10, cy - 40), cv.FONT_HERSHEY_SIMPLEX, 0.5, 255)# (6) 結束角度 (endAngle) 的影響 II cx, cy = 50, 420 # 圓心坐標 haf, has = 40, 30 # 半軸長度 angle = 30 # 旋轉角度 startAng = 45 # 開始角度 endAng = [30, 90, 180, 360] # 結束角度 for i in range(len(endAng)):color = (i * 20, i * 20, 255 - i * 20)cxi = cx + i * 100cv.ellipse(img2, (cxi, cy), (haf, has), angle, startAng, endAng[i], color, 2)angPi = angle * np.pi / 180 # 轉換為弧度制,便于計算坐標xe = int(cxi + haf * np.cos(angPi))ye = int(cy + haf * np.sin(angPi))cv.arrowedLine(img2, (cxi, cy), (xe, ye), 255) # 從圓心指向第一軸端點text = "end {}".format(endAng[i])cv.putText(img2, text, (cxi - 40, cy), cv.FONT_HERSHEY_SIMPLEX, 0.5, color) text = "start={}".format(startAng) cv.putText(img2, text, (10, cy - 40), cv.FONT_HERSHEY_SIMPLEX, 0.5, 255)# (7) 結束角度 (endAngle) 的影響 II cx, cy = 50, 550 # 圓心坐標 haf, has = 40, 30 # 半軸長度 angle = 30 # 旋轉角度 startAng = [0, 0, 180, 180] # 開始角度 endAng = [90, 180, 270, 360] # 結束角度 for i in range(len(endAng)):color = (i * 20, i * 20, 255 - i * 20)cxi = cx + i * 100cv.ellipse(img2, (cxi, cy), (haf, has), angle, startAng[i], endAng[i], color, 2)angPi = angle * np.pi / 180 # 轉換為弧度制,便于計算坐標xe = int(cxi + haf * np.cos(angPi))ye = int(cy + haf * np.sin(angPi))cv.arrowedLine(img2, (cxi, cy), (xe, ye), 255) # 從圓心指向第一軸端點text = "start {}".format(startAng[i])cv.putText(img2, text, (cxi - 40, cy - 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, color)text = "end {}".format(endAng[i])cv.putText(img2, text, (cxi - 40, cy), cv.FONT_HERSHEY_SIMPLEX, 0.5, color) text = "rotate={}".format(angle) cv.putText(img2, text, (10, cy - 50), cv.FONT_HERSHEY_SIMPLEX, 0.5, 255)plt.figure(figsize=(9, 6)) plt.subplot(121), plt.title("Ellipse1"), plt.axis('off') plt.imshow(cv.cvtColor(img1, cv.COLOR_BGR2RGB)) plt.subplot(122), plt.title("Ellipse2"), plt.axis('off') plt.imshow(cv.cvtColor(img2, cv.COLOR_BGR2RGB)) plt.show()繪制多段線和多邊形
函數cv.polylines()用來繪制多邊形曲線或多段線
函數cv.fillPoly()用來繪制一個或多個填充的多邊形區域
函數cv.fillConvexPoly()用來繪制一個填充的凸多邊形
cv.polylines(img, pts, isClosed, color[, thickness=1, lineType=LINE_8, shift=0]) → img
cv.fillPoly(img, pts, color[, lineType=LINE_8, shift=0, offset=Point()]) → img
cv.fillConvexPoly(img, points, color[, lineType=LINE_8, shift=0]) → img
參數說明:
- img:輸入輸出圖像,允許單通道灰度圖像或多通道彩色圖像
- pts:多邊形頂點坐標, 二維 Numpy 數組的列表
- points:多邊形頂點坐標,二維 Numpy 數組
- isClosed: 閉合標志,True 表示閉合多邊形,False 表示多邊形不閉合
示例程序:
""" 繪制多段線和多邊形 """ import cv2 as cv import matplotlib.pyplot as plt import numpy as npimg = np.ones((980, 400, 3), np.uint8) * 224 img1 = img.copy() img2 = img.copy() img3 = img.copy() img4 = img.copy()# 多邊形頂點 points1 = np.array([[200, 100], [295, 169], [259, 281], [141, 281], [105, 169]], np.int) points2 = np.array([[200, 400], [259, 581], [105, 469], [295, 469], [141, 581]]) # (5,2) points3 = np.array([[200, 700], [222, 769], [295, 769], [236, 812], [259, 881],[200, 838], [141, 881], [164, 812], [105, 769], [178, 769]])# 繪制多邊形,閉合曲線 pts1 = [points1] # pts1 是列表,列表元素是形狀為 (m,2) 的 numpy 二維數組 cv.polylines(img1, pts1, True, (0, 0, 255)) # pts1 是列表 cv.polylines(img1, [points2, points3], 1, 255, 2) # 可以繪制多個多邊形# 繪制多段線,曲線不閉合 cv.polylines(img2, [points1], False, (0, 0, 255)) cv.polylines(img2, [points2, points3], 0, 255, 2) # 可以繪制多個多段線# 繪制填充多邊形,注意交叉重疊部分處理 cv.fillPoly(img3, [points1], (0, 0, 255)) cv.fillPoly(img3, [points2, points3], 255) # 可以繪制多個填充多邊形# 繪制一個填充多邊形,注意交叉重疊部分 cv.fillConvexPoly(img4, points1, (0, 0, 255)) cv.fillConvexPoly(img4, points2, 255) # 不能繪制存在自相交的多邊形 cv.fillConvexPoly(img4, points3, 255) # 可以繪制凹多邊形,但要慎用plt.figure(figsize=(9, 6)) plt.subplot(141), plt.title("closed polygon"), plt.axis('off') plt.imshow(cv.cvtColor(img1, cv.COLOR_BGR2RGB)) plt.subplot(142), plt.title("unclosed polygo"), plt.axis('off') plt.imshow(cv.cvtColor(img2, cv.COLOR_BGR2RGB)) plt.subplot(143), plt.title("fillPoly"), plt.axis('off') plt.imshow(cv.cvtColor(img3, cv.COLOR_BGR2RGB)) plt.subplot(144), plt.title("fillConvexPoly"), plt.axis('off') plt.imshow(cv.cvtColor(img4, cv.COLOR_BGR2RGB)) plt.tight_layout() plt.show()添加水印
添加水印的思路是先在黑色背景上添加圖像或文字制作水印,再使用cv.addWeight函數,通過重疊混合把水印添加到原始圖像上。
示例程序:
""" 添加水印 """ import cv2 as cv import matplotlib.pyplot as plt import numpy as npimg = cv.imread("../img/lena.jpg", 1) # 加載原始圖片 h, w = img.shape[0], img.shape[1]# 生成水印圖案 logo = cv.imread("../img/img.jpg", 0) # 加載 Logo logoResize = cv.resize(logo, (200, 200)) # 調整圖片尺寸 grayMark = np.zeros(img.shape[:2], np.uint8) # 水印黑色背景 grayMark[10:210, 10:210] = logoResize # 生成水印圖案# 生成文字水印 mark = np.zeros(img.shape[:2], np.uint8) # 黑色背景 for i in range(h // 100):cv.putText(mark, "zstar", (50, 70 + 100 * i), cv.FONT_HERSHEY_SIMPLEX, 1.5, 255, 2) MAR = cv.getRotationMatrix2D((w // 2, h // 2), 45, 1.0) # 旋轉 45 度 grayMark2 = cv.warpAffine(mark, MAR, (w, h)) # 旋轉變換,默認為黑色填充# 添加圖片水印 markC3 = cv.merge([grayMark, grayMark, grayMark]) imgMark1 = cv.addWeighted(img, 1, markC3, 0.25, 0) # 加權加法圖像融合# 添加文字水印 markC32 = cv.merge([grayMark2, grayMark2, grayMark2]) imgMark2 = cv.addWeighted(img, 1, markC32, 0.25, 0) # 加權加法圖像融合plt.figure(figsize=(9, 6)) plt.subplot(221), plt.title("original"), plt.axis('off') plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB)) plt.subplot(222), plt.title("watermark"), plt.axis('off') plt.imshow(cv.cvtColor(markC3, cv.COLOR_BGR2RGB)) plt.subplot(223), plt.title("watermark embedded"), plt.axis('off') plt.imshow(cv.cvtColor(imgMark1, cv.COLOR_BGR2RGB)) plt.subplot(224), plt.title("watermark embedded"), plt.axis('off') plt.imshow(cv.cvtColor(imgMark2, cv.COLOR_BGR2RGB)) plt.tight_layout() plt.show()添加馬賽克
實現馬賽克的原理就是將處理區域劃分為一個個小方塊,每個小方塊內所有像素置為相同的或相似的像素值。
示例程序:
""" 添加馬賽克 """ import cv2 as cv import matplotlib.pyplot as plt import numpy as npimg = cv.imread("../img/lena.jpg", 1) # 加載原始圖片 roi = cv.selectROI(img, showCrosshair=True, fromCenter=False) x, y, wRoi, hRoi = roi # 矩形裁剪區域的位置參數 # x, y, wRoi, hRoi = 208, 176, 155, 215 # 矩形裁剪區域 imgROI = img[y:y + hRoi, x:x + wRoi].copy() # 切片獲得矩形裁剪區域plt.figure(figsize=(9, 6)) plt.subplot(231), plt.title("Original image"), plt.axis('off') plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB)) plt.subplot(232), plt.title("Region of interest"), plt.axis('off') plt.imshow(cv.cvtColor(imgROI, cv.COLOR_BGR2RGB))mosaic = np.zeros(imgROI.shape, np.uint8) # ROI 區域 ksize = [5, 10, 20] # 馬賽克塊的寬度 for i in range(3):k = ksize[i]for h in range(0, hRoi, k):for w in range(0, wRoi, k):color = imgROI[h, w]mosaic[h:h + k, w:w + k, :] = color # 用頂點顏色覆蓋馬賽克塊imgMosaic = img.copy()imgMosaic[y:y + hRoi, x:x + wRoi] = mosaicplt.subplot(2, 3, i + 4), plt.title("Coding image (size={})".format(k)), plt.axis('off')plt.imshow(cv.cvtColor(imgMosaic, cv.COLOR_BGR2RGB))plt.subplot(233), plt.title("Mosaic"), plt.axis('off') plt.imshow(cv.cvtColor(mosaic, cv.COLOR_BGR2RGB)) plt.show()趣味應用
下面這個是迷途小書童的Note編寫的,通過調整色調和色相,可以將圖片變成賽博朋克風格。
完整代碼:
""" Title:賽博朋克特效實現 Author:迷途小書童的Note Link:https://mp.weixin.qq.com/s/brZSanGvqqi6AHT3wg54Lg """import cv2 import numpy as npdef modify_color_temperature(img):# ---------------- 冷色調 ---------------- ## 1.計算三個通道的平均值,并依據平均值調整色調imgB = img[:, :, 0]imgG = img[:, :, 1]imgR = img[:, :, 2]# 調整色調 # 白平衡 -> 三個值變化相同# 冷色調(增加b分量) -> 除了b之外都增加# 暖色調(增加r分量) -> 除了r之外都增加bAve = cv2.mean(imgB)[0]gAve = cv2.mean(imgG)[0] + 10rAve = cv2.mean(imgR)[0] + 10aveGray = (int)(bAve + gAve + rAve) / 3# 2. 計算各通道增益系數,并使用此系數計算結果bCoef = aveGray / bAvegCoef = aveGray / gAverCoef = aveGray / rAveimgB = np.floor((imgB * bCoef)) # 向下取整imgG = np.floor((imgG * gCoef))imgR = np.floor((imgR * rCoef))# 3. 變換后處理imgb = imgBimgb[imgb > 255] = 255imgg = imgGimgg[imgg > 255] = 255imgr = imgRimgr[imgr > 255] = 255cold_rgb = np.dstack((imgb, imgg, imgr)).astype(np.uint8)return cold_rgbdef reverse_hue(image):# 反轉色相image_hls = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)image_hls = np.asarray(image_hls, np.float32)hue = image_hls[:, :, 0]hue[hue < 90] = 180 - hue[hue < 90] - 10image_hls[:, :, 0] = hueimage_hls = np.asarray(image_hls, np.uint8)image = cv2.cvtColor(image_hls, cv2.COLOR_HLS2BGR)return imagedef cyberpunk(image):image_lab = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)image_lab = np.asarray(image_lab, np.float32)image_lab[:,:,0] = np.clip(image_lab[:,:,0] * 1.2,0,255)# 提高像素亮度,讓亮的地方更亮light_gamma_high = np.power(image_lab[:, :, 0], 0.9)light_gamma_high = np.asarray(light_gamma_high / np.max(light_gamma_high) * 255, np.uint)# 降低像素亮度,讓暗的地方更暗light_gamma_low = np.power(image_lab[:, :, 0], 1.1)light_gamma_low = np.asarray(light_gamma_low / np.max(light_gamma_low) * 255, np.uint8)# 調色至偏紫dark_b = image_lab[:, :, 2] * (light_gamma_low / 255) * 0.4dark_a = image_lab[:, :, 2] * (1 - light_gamma_high / 255) * 0.1image_lab[:, :, 2] = np.clip(image_lab[:, :, 2] - dark_b, 0, 255)image_lab[:, :, 1] = np.clip(image_lab[:, :, 1] - dark_a, 0, 255)image_lab = np.asarray(image_lab, np.uint8)return cv2.cvtColor(image_lab, cv2.COLOR_Lab2BGR)if __name__ == "__main__":# 設置窗口可縮放cv2.namedWindow('origin', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)cv2.namedWindow('cold_style', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)cv2.namedWindow('reverser_hue', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)cv2.namedWindow('cyberpunk', cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO)image = cv2.imread("../img/img.jpg")cv2.imshow("origin", image)image = modify_color_temperature(image)cv2.imshow("cold_style", image)image = reverse_hue(image)cv2.imshow("reverser_hue", image)# cv2.waitKey()image = cyberpunk(image)cv2.imshow("cyberpunk", image)cv2.imwrite("result2.jpg", image)cv2.waitKey()總結
以上是生活随笔為你收集整理的【OpenCV】Chapter10.色彩转换与图像绘制的全部內容,希望文章能夠幫你解決所遇到的問題。