提取一副彩色影像中紅色,用HIS模型處理,RGB模型對比顯示
- 參考文獻
- 引入
- 顯示HIS模型提取的紅色
- RGB模型 提取紅色
參考文獻
bgr_2_hsi、color_slicing來自:數字影像處理:彩色圖片處理,HSI模型
hsv提取彩色的方法來自:OpenCV之顏色空間轉換、掩膜(mask)、顏色提取——python實作
HSV提取指定其他所有顏色的方法:Python3 識別判斷圖片主要顏色,提取指定顏色的方法
感謝各位作者!
引入
import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用來正常顯示中文標簽
plt.rcParams['axes.unicode_minus'] = False # 用來正常顯示負號
顯示HIS模型提取的紅色
函式bgr_2_hsi:
def bgr_2_hsi(image):
out = np.copy(image)
out_slicing = np.zeros(image.shape, np.uint8)
for x in range(image.shape[0]):
# print(str(int(x / image.shape[0] * 100)) + "%")
for y in range(image.shape[1]):
b, g, r = image[x][y]
b, g, r = int(b), int(g), int(r)
i_s = np.sum([b, g, r])
i = i_s / 3
# i == 0, s and h is no sense
if i_s == 0:
i = 0
s = 0
h = 0
out[x][y] = h, s, i
continue
s = (1 - (3 * np.min([b, g, r])) / i_s) * 255
# s == 0 h is no sense
if s == 0:
h = 0
out[x][y] = h, s, i
continue
thea = np.arccos((2 * r - g - b) / (2 * np.sqrt((r - g) ** 2 + (r - b) * (g - b))))
if g >= b:
h1 = thea
else:
h1 = np.pi * 2 - thea
h1 = np.rad2deg(h1)
# slicing
if (int(h1) in range(0, 11) or int(h1) in range(350, 361)) and s / 255 > 0.1:
print(int(h1))
out_slicing[x][y] = image[x][y]
h = h1 / 360 * 255
out[x][y] = h, s, i
return out
實作:
# 讀入的影像是BGR空間影像
frame = cv2.imread("img/color2.png")
# frame_to_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# 部分1:將BGR空間的圖片轉換到HSI空間
frame_to_hsi = bgr_2_hsi(frame)
# 部分2:
# 每個分量設定兩個閾值
# H_min < H < H_max AND
# S_min < S < S_max AND
# I_min < I < I_max
# 定義紅色
lower_red = np.array([0, 100, 100])
upper_red = np.array([10, 255, 255])
# 部分3:
# 從HSI影像中截取出藍色、綠色、紅色,即獲得相應的掩膜
# cv2.inRange()函式是設定閾值去除背景部分,得到想要的區域
red_mask = cv2.inRange(frame_to_hsi, lower_red, upper_red)
# 部分4:
# 將原影像和mask(掩膜)進行按位與
red_res = cv2.bitwise_and(frame, frame, mask=red_mask)
# 最后得到要分離出的顏色影像
# 部分5:將BGR空間下的圖片轉換成RGB空間下的圖片
frame = frame[:, :, ::-1]
red_res = red_res[:, :, ::-1]
# 部分6:顯示影像
plt.subplot(131), plt.imshow(frame) ,plt.title("原圖")
plt.subplot(132), plt.imshow(red_mask), plt.title("紅色遮罩")
plt.subplot(133), plt.imshow(red_res), plt.title("紅色")
plt.show()
結果:

RGB模型 提取紅色
函式:
def color_slicing(image, center, w):
"""
:param image:
:param center: b, g, r ib range 0 ~ 255
:param w: width
:return:
"""
out = np.zeros(image.shape, np.uint8)
for x in range(image.shape[0]):
for y in range(image.shape[1]):
r_b, r_g, r_r = center
a_b, a_g, a_r = image[x][y]
if abs(r_b - a_b) < w / 2 and abs(r_g - a_g) < w / 2 and abs(r_r - a_r) < w / 2:
out[x][y] = image[x][y]
return out
實作:
img = cv2.imread("img/color2.png")
img_to_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_slice_red = color_slicing(img_to_rgb, (241, 68, 58), 0.2549 * 255)
plt.subplot(121), plt.imshow(img_to_rgb), plt.title("原圖RGB")
plt.subplot(122), plt.imshow(img_slice_red), plt.title("紅色")
plt.show()
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/348525.html
標籤:其他
上一篇:openCV編譯匯入
