失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > opencv-python数字图像处理学习7:提取一副彩色图像中红色 用HIS模型处理 RGB模型对比显示

opencv-python数字图像处理学习7:提取一副彩色图像中红色 用HIS模型处理 RGB模型对比显示

时间:2021-06-16 15:54:54

相关推荐

opencv-python数字图像处理学习7:提取一副彩色图像中红色 用HIS模型处理 RGB模型对比显示

提取一副彩色图像中红色,用HIS模型处理,RGB模型对比显示

参考文献引入显示HIS模型提取的红色RGB模型 提取红色

参考文献

bgr_2_hsi、color_slicing来自:数字图像处理:彩色图片处理,HSI模型

hsv提取彩色的方法来自:OpenCV之颜色空间转换、掩膜(mask)、颜色提取——python实现

HSV提取指定其他所有颜色的方法:Python3 识别判断图片主要颜色,提取指定颜色的方法

感谢各位作者!

引入

import cv2import numpy as npimport matplotlib.pyplot as pltplt.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 senseif i_s == 0:i = 0s = 0h = 0out[x][y] = h, s, icontinues = (1 - (3 * np.min([b, g, r])) / i_s) * 255# s == 0 h is no senseif s == 0:h = 0out[x][y] = h, s, icontinuethea = np.arccos((2 * r - g - b) / (2 * np.sqrt((r - g) ** 2 + (r - b) * (g - b))))if g >= b:h1 = theaelse:h1 = np.pi * 2 - theah1 = np.rad2deg(h1)# slicingif (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 * 255out[x][y] = h, s, ireturn 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 = centera_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()

结果:

如果觉得《opencv-python数字图像处理学习7:提取一副彩色图像中红色 用HIS模型处理 RGB模型对比显示》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。