数字图像与机器视觉基础补充(2)--颜色转换+分割车牌

文章所用编程软件为Anaconda Jupyter Notebook

一、彩色图像文件转灰度文件

1.1 使用opencv

  • 所用图片

请添加图片描述

1.1.1 通过cvtColor库将其转为灰度

  • 代码
import cv2 as cv
img = cv.imread('D:/**/1/b/24wei.bmp',1)
img_1 = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
cv.imshow('gray',img_1)
cv.imshow('colour',img)
cv.waitKey(0)
  • 运行结果

在这里插入图片描述

1.1.2 通过分离RGB三个通道得到三个通道的灰度图

  • 代码
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('D:/**/1/b/24wei.bmp',1)
#cv2.imread读取图片格式是BGR
b,g,r = cv.split(img)  #这个地方将图像拆分,把彩色图像分为3个颜色
plt.figure(figsize=(10,8))
color = [b,g,r]
img_2 = cv.merge([r,g,b])  #这个地方我把bgr格式的图片转成了rgb,然后显示的时候会变成正常的彩色
for i in range(3):
    plt.subplot(2,2,i+1)
    plt.imshow(color[i],'gray')
    plt.subplot(2,2,4)
    plt.imshow(img_2)
plt.savefig('./三通道灰度.png')
plt.show()
  • 运行结果

在这里插入图片描述

1.2 不使用opencv

  • 代码
from PIL import Image
I = Image.open('D:/**/1/b/24wei.bmp')
L = I.convert('L')
L.show()
  • 运行结果

在这里插入图片描述

二、将彩色图像转化为HSV、HSI 格式

2.1 彩色图像转化为HSV格式

2.1.1 HSV介绍

  • HSV 格式: H 代表色彩,S 代表颜色的深浅,V 代表着颜色的明暗程度。
  • HSV 颜色空间可以很好地把颜色信息和亮度信息分开,将它们放在不同的通道中,减小了光线对于特定颜色识别的影响。
  • HSV (色相hue, 饱和度saturation, 明度value), 也称HSB(B指brightness) 是艺术家们常用的,因为与加法减法混色的术语相比,使用色相,饱和度等概念描述色彩更自然直观。HSV是RGB色彩空间的一种变形,它的内容与色彩尺度与其出处——RGB色彩空间有密切联系。对应的媒介是人眼。
  • 在OpenCV 视觉库中,HSV 的数值被做了一些小的修改, H 的范围调整为 0~180,S 和 V 的范围为 0~255。

在这里插入图片描述

  • 当我们采用 HSV 的图像阈值得到某一种颜色时,可以参考颜色分布表,先将 H 通道对应的颜色找到。表格中,每种颜色都对应了一个区间。
角度 颜色
0-60 红色
60-120 黄色
120-180 绿色
180-240 青色
240-300 蓝色
300-360 品红

2.1.2 代码

# open-cv library is installed as cv2 in python
# import cv2 library into this program
import cv2 as cv
 
# read an image using imread() function of cv2
# we have to  pass only the path of the image
img = cv.imread('D:/**/1/b/24wei.bmp',1)
 
# displaying the image using imshow() function of cv2
# In this : 1st argument is name of the frame
# 2nd argument is the image matrix

cv.imshow('original image',img)
 
# converting the colourfull image into HSV format image
# using cv2.COLOR_BGR2HSV argument of
# the cvtColor() function of cv2
# in this :
# ist argument is the image matrix
# 2nd argument is the attribute
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)

 
# displaying the Hsv format image
cv.imshow('HSV format image',hsv)

cv.waitKey(0)

2.1.3 运行结果

在这里插入图片描述

2.2 彩色图像转化为HSI格式

2.2.1 HSI介绍

  • HSL (色相hue, 饱和度saturation, 亮度lightness/luminance),也称HLS 或 HSI (I指intensity),与 HSV非常相似,仅用亮度(lightness)替代了明度(brightness)。
  • 人的视觉对亮度的敏感程度远强于对颜色浓淡的敏感程度,为了便于颜色处理和识别,人的市局系统经常采用HSI彩色空间,它比RGB空间更符合人的视觉特性。此外,由于HSI空间中亮度和色度具有可分离性,使得图像处理和机器视觉中大量灰度处理算法都可在HSI空间方便进行。
  • HSI颜色空间

在这里插入图片描述

2.2.2 代码

import cv2
import numpy as np

def rgbtohsi(rgb_lwpImg):
  rows = int(rgb_lwpImg.shape[0])
  cols = int(rgb_lwpImg.shape[1])
  b, g, r = cv2.split(rgb_lwpImg)
  # 归一化到[0,1]
  b = b / 255.0
  g = g / 255.0
  r = r / 255.0
  hsi_lwpImg = rgb_lwpImg.copy()
  H, S, I = cv2.split(hsi_lwpImg
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值