阅读 81

Android OpenCV(六):颜色模型及转换

颜色模型

RGB

采用三种颜色的英文首字母组成,分别是红色(Red)、绿色(Green)和蓝色(Blue)。虽然该颜色模型的命名方式是红色在前,但是在OpenCV中却是相反的顺序,第一个通道时蓝色(B)分量,第二个通道时绿色(G)分量,第三个通道时红色(R)分量。根据存储顺序的不同,OpenCV 4中提供了这种顺序的反序格式,用于存储第一个通道是红色分量的图像,但是这两种格式的图像的颜色空间是相同的,颜色空间如图3-1所示。三个通道对于颜色描述的范围是相同的,因此RGB颜色模型的空间构成是一个立方体。在RGB颜色模型中,所有的颜色都是由这三种颜色通过不同比例的混合得到,如果三种颜色分量都为0,则表示为黑色,如果三种颜色的分量相同且都为最大值,则表示为白色。每个通道都表示某一种颜色由0到1的过程,不同位数的图像表示将这个颜色变化过程细分成不同的层级,例如8U3C格式的图像每个通道将这个过程量化成256个等级,分别由0到255表示。在这个模型的基础上增加第四个通道即为RGBA模型,第四个通道表示颜色的透明度,当没有透明度需求的时候,RGBA模型就会退化成RGB模型。

RGB模型
RGB模型

YUV

YUV模型是电视信号系统所采用的颜色编码方式。这三个变量分别表示是像素的亮度(Y)以及红色分量与亮度的信号差值(U)和蓝色与亮度的差值(V)。这种颜色模型主要用于视频和图像的传输,该模型的产生与电视机的发展历程密切相关。由于彩色电视机在黑白电视机发明之后才产生,因此用于彩色电视机的视频信号需要能够兼容黑白电视机。彩色电视机需要三个通道的数据才能显示彩色,而黑白电视机只需要一个通道的数据即可,因此为了使视频信号能够兼容彩色电视与黑白电视,将RGB编码方式转变成YUV的编码方式,其Y通道是图像的亮度,黑白电视只需要使用该通道就可以显示黑白视频图像,而彩色相机通过将YUV编码转成RGB编码方式,便可以在彩色电视种显示彩色图像,较好的解决了同一个视频信号兼容不同类型电视的问题。RGB模型与YUV模型之间的转换关系如式所示,其中RGB取值范围均为0-255。

YUV换算公式
YUV换算公式

HSV

HSV是色度(Hue)、饱和度(Saturation)和亮度(Value)的简写,通过名字也可以看出来该模型通过这三个特性对颜色进行描述。色度是色彩的基本属性,就是平时常说的颜色,例如红色,蓝色等;饱和度是指颜色的纯度,饱和度越高色彩越纯越艳,饱和度越低色彩则逐渐地变灰变暗,饱和度的取值范围是由0到100%;亮度是颜色的明亮程度,其取值范围由0到计算机中允许的最大值。由于色度、饱和度和亮度的取值范围不同,因此其颜色空间模型用锥形表示。相比于RGB模型三个颜色分量与最终颜色联系不直观的缺点,HSV模型更加符合人类感知颜色的方式:颜色、深浅以及亮暗。

HSV模型
HSV模型

Lab

Lab颜色模型弥补了RGB模型的不足,是一种设备无关的颜色模型,是一种基于生理特征的颜色模型。在模型中L表示亮度(Luminosity),a和b是两个颜色通道,两者的取值区间都是由-128到+127,其中a通道数值由小到大对应的颜色是从绿色变成红色,b通道数值由小到大对应的颜色是由蓝色变成黄色。其构成的颜色空间是一个球形。

Lab模型
Lab模型

Gray

GRAY模型并不是一个彩色模型,他是一个灰度图像的模型,其命名使用的是英文单词gray的全字母大写。灰度图像只有单通道,灰度值根据图像位数不同由0到最大依次表示由黑到白,例如8UC1格式中,由黑到白被量化成了256个等级,通过0-255表示,其中255表示白色。彩色图像具有颜色丰富、信息含量大的特性,但是灰度图在图像处理中依然具有一定的优势。例如,灰度图像具有相同尺寸相同压缩格式所占容量小,易于采集,便于传输等优点。常用的RGB模型转成灰度图的方式如式中所示。

Gray计算公式
Gray计算公式

API

public static void cvtColor(Mat src, Mat dst, int code, int dstCn)
复制代码
  • 参数一:src,待转换颜色模型的原始图像
  • 参数二:dst,转换颜色模型后的目标图像
  • 参数三:code,颜色空间转换的标志
  • 参数四(可选):dstCn,目标图像中的通道数,如果参数为0,则从src和代码中自动导出通道数

标志位

public static final int
COLOR_BGR2BGRA = 0,
COLOR_RGB2RGBA = COLOR_BGR2BGRA,
COLOR_BGRA2BGR = 1,
COLOR_RGBA2RGB = COLOR_BGRA2BGR,
COLOR_BGR2RGBA = 2,
COLOR_RGB2BGRA = COLOR_BGR2RGBA,
COLOR_RGBA2BGR = 3,
COLOR_BGRA2RGB = COLOR_RGBA2BGR,
COLOR_BGR2RGB = 4,
COLOR_RGB2BGR = COLOR_BGR2RGB,
COLOR_BGRA2RGBA = 5,
COLOR_RGBA2BGRA = COLOR_BGRA2RGBA,
COLOR_BGR2GRAY = 6,
COLOR_RGB2GRAY = 7,
COLOR_GRAY2BGR = 8,
COLOR_GRAY2RGB = COLOR_GRAY2BGR,
COLOR_GRAY2BGRA = 9,
COLOR_GRAY2RGBA = COLOR_GRAY2BGRA,
COLOR_BGRA2GRAY = 10,
COLOR_RGBA2GRAY = 11,
COLOR_BGR2BGR565 = 12,
COLOR_RGB2BGR565 = 13,
COLOR_BGR5652BGR = 14,
COLOR_BGR5652RGB = 15,
COLOR_BGRA2BGR565 = 16,
COLOR_RGBA2BGR565 = 17,
COLOR_BGR5652BGRA = 18,
COLOR_BGR5652RGBA = 19,
COLOR_GRAY2BGR565 = 20,
COLOR_BGR5652GRAY = 21,
COLOR_BGR2BGR555 = 22,
COLOR_RGB2BGR555 = 23,
COLOR_BGR5552BGR = 24,
COLOR_BGR5552RGB = 25,
COLOR_BGRA2BGR555 = 26,
COLOR_RGBA2BGR555 = 27,
COLOR_BGR5552BGRA = 28,
COLOR_BGR5552RGBA = 29,
COLOR_GRAY2BGR555 = 30,
COLOR_BGR5552GRAY = 31,
COLOR_BGR2XYZ = 32,
COLOR_RGB2XYZ = 33,
COLOR_XYZ2BGR = 34,
COLOR_XYZ2RGB = 35,
COLOR_BGR2YCrCb = 36,
COLOR_RGB2YCrCb = 37,
COLOR_YCrCb2BGR = 38,
COLOR_YCrCb2RGB = 39,
COLOR_BGR2HSV = 40,
COLOR_RGB2HSV = 41,
COLOR_BGR2Lab = 44,
COLOR_RGB2Lab = 45,
COLOR_BGR2Luv = 50,
COLOR_RGB2Luv = 51,
COLOR_BGR2HLS = 52,
COLOR_RGB2HLS = 53,
COLOR_HSV2BGR = 54,
COLOR_HSV2RGB = 55,
COLOR_Lab2BGR = 56,
COLOR_Lab2RGB = 57,
COLOR_Luv2BGR = 58,
COLOR_Luv2RGB = 59,
COLOR_HLS2BGR = 60,
COLOR_HLS2RGB = 61,
COLOR_BGR2HSV_FULL = 66,
COLOR_RGB2HSV_FULL = 67,
COLOR_BGR2HLS_FULL = 68,
COLOR_RGB2HLS_FULL = 69,
COLOR_HSV2BGR_FULL = 70,
COLOR_HSV2RGB_FULL = 71,
COLOR_HLS2BGR_FULL = 72,
COLOR_HLS2RGB_FULL = 73,
COLOR_LBGR2Lab = 74,
COLOR_LRGB2Lab = 75,
COLOR_LBGR2Luv = 76,
COLOR_LRGB2Luv = 77,
COLOR_Lab2LBGR = 78,
COLOR_Lab2LRGB = 79,
COLOR_Luv2LBGR = 80,
COLOR_Luv2LRGB = 81,
COLOR_BGR2YUV = 82,
COLOR_RGB2YUV = 83,
COLOR_YUV2BGR = 84,
COLOR_YUV2RGB = 85,
COLOR_YUV2RGB_NV12 = 90,
COLOR_YUV2BGR_NV12 = 91,
COLOR_YUV2RGB_NV21 = 92,
COLOR_YUV2BGR_NV21 = 93,
COLOR_YUV420sp2RGB = COLOR_YUV2RGB_NV21,
COLOR_YUV420sp2BGR = COLOR_YUV2BGR_NV21,
COLOR_YUV2RGBA_NV12 = 94,
COLOR_YUV2BGRA_NV12 = 95,
COLOR_YUV2RGBA_NV21 = 96,
COLOR_YUV2BGRA_NV21 = 97,
COLOR_YUV420sp2RGBA = COLOR_YUV2RGBA_NV21,
COLOR_YUV420sp2BGRA = COLOR_YUV2BGRA_NV21,
COLOR_YUV2RGB_YV12 = 98,
COLOR_YUV2BGR_YV12 = 99,
COLOR_YUV2RGB_IYUV = 100,
COLOR_YUV2BGR_IYUV = 101,
COLOR_YUV2RGB_I420 = COLOR_YUV2RGB_IYUV,
COLOR_YUV2BGR_I420 = COLOR_YUV2BGR_IYUV,
COLOR_YUV420p2RGB = COLOR_YUV2RGB_YV12,
COLOR_YUV420p2BGR = COLOR_YUV2BGR_YV12,
COLOR_YUV2RGBA_YV12 = 102,
COLOR_YUV2BGRA_YV12 = 103,
COLOR_YUV2RGBA_IYUV = 104,
COLOR_YUV2BGRA_IYUV = 105,
COLOR_YUV2RGBA_I420 = COLOR_YUV2RGBA_IYUV,
COLOR_YUV2BGRA_I420 = COLOR_YUV2BGRA_IYUV,
COLOR_YUV420p2RGBA = COLOR_YUV2RGBA_YV12,
COLOR_YUV420p2BGRA = COLOR_YUV2BGRA_YV12,
COLOR_YUV2GRAY_420 = 106,
COLOR_YUV2GRAY_NV21 = COLOR_YUV2GRAY_420,
COLOR_YUV2GRAY_NV12 = COLOR_YUV2GRAY_420,
COLOR_YUV2GRAY_YV12 = COLOR_YUV2GRAY_420,
COLOR_YUV2GRAY_IYUV = COLOR_YUV2GRAY_420,
COLOR_YUV2GRAY_I420 = COLOR_YUV2GRAY_420,
COLOR_YUV420sp2GRAY = COLOR_YUV2GRAY_420,
COLOR_YUV420p2GRAY = COLOR_YUV2GRAY_420,
COLOR_YUV2RGB_UYVY = 107,
COLOR_YUV2BGR_UYVY = 108,
COLOR_YUV2RGB_Y422 = COLOR_YUV2RGB_UYVY,
COLOR_YUV2BGR_Y422 = COLOR_YUV2BGR_UYVY,
COLOR_YUV2RGB_UYNV = COLOR_YUV2RGB_UYVY,
COLOR_YUV2BGR_UYNV = COLOR_YUV2BGR_UYVY,
COLOR_YUV2RGBA_UYVY = 111,
COLOR_YUV2BGRA_UYVY = 112,
COLOR_YUV2RGBA_Y422 = COLOR_YUV2RGBA_UYVY,
COLOR_YUV2BGRA_Y422 = COLOR_YUV2BGRA_UYVY,
COLOR_YUV2RGBA_UYNV = COLOR_YUV2RGBA_UYVY,
COLOR_YUV2BGRA_UYNV = COLOR_YUV2BGRA_UYVY,
COLOR_YUV2RGB_YUY2 = 115,
COLOR_YUV2BGR_YUY2 = 116,
COLOR_YUV2RGB_YVYU = 117,
COLOR_YUV2BGR_YVYU = 118,
COLOR_YUV2RGB_YUYV = COLOR_YUV2RGB_YUY2,
COLOR_YUV2BGR_YUYV = COLOR_YUV2BGR_YUY2,
COLOR_YUV2RGB_YUNV = COLOR_YUV2RGB_YUY2,
COLOR_YUV2BGR_YUNV = COLOR_YUV2BGR_YUY2,
COLOR_YUV2RGBA_YUY2 = 119,
COLOR_YUV2BGRA_YUY2 = 120,
COLOR_YUV2RGBA_YVYU = 121,
COLOR_YUV2BGRA_YVYU = 122,
COLOR_YUV2RGBA_YUYV = COLOR_YUV2RGBA_YUY2,
COLOR_YUV2BGRA_YUYV = COLOR_YUV2BGRA_YUY2,
COLOR_YUV2RGBA_YUNV = COLOR_YUV2RGBA_YUY2,
COLOR_YUV2BGRA_YUNV = COLOR_YUV2BGRA_YUY2,
COLOR_YUV2GRAY_UYVY = 123,
COLOR_YUV2GRAY_YUY2 = 124,
COLOR_YUV2GRAY_Y422 = COLOR_YUV2GRAY_UYVY,
COLOR_YUV2GRAY_UYNV = COLOR_YUV2GRAY_UYVY,
COLOR_YUV2GRAY_YVYU = COLOR_YUV2GRAY_YUY2,
COLOR_YUV2GRAY_YUYV = COLOR_YUV2GRAY_YUY2,
COLOR_YUV2GRAY_YUNV = COLOR_YUV2GRAY_YUY2,
COLOR_RGBA2mRGBA = 125,
COLOR_mRGBA2RGBA = 126,
COLOR_RGB2YUV_I420 = 127,
COLOR_BGR2YUV_I420 = 128,
COLOR_RGB2YUV_IYUV = COLOR_RGB2YUV_I420,
COLOR_BGR2YUV_IYUV = COLOR_BGR2YUV_I420,
COLOR_RGBA2YUV_I420 = 129,
COLOR_BGRA2YUV_I420 = 130,
COLOR_RGBA2YUV_IYUV = COLOR_RGBA2YUV_I420,
COLOR_BGRA2YUV_IYUV = COLOR_BGRA2YUV_I420,
COLOR_RGB2YUV_YV12 = 131,
COLOR_BGR2YUV_YV12 = 132,
COLOR_RGBA2YUV_YV12 = 133,
COLOR_BGRA2YUV_YV12 = 134,
COLOR_BayerBG2BGR = 46,
COLOR_BayerGB2BGR = 47,
COLOR_BayerRG2BGR = 48,
COLOR_BayerGR2BGR = 49,
COLOR_BayerBG2RGB = COLOR_BayerRG2BGR,
COLOR_BayerGB2RGB = COLOR_BayerGR2BGR,
COLOR_BayerRG2RGB = COLOR_BayerBG2BGR,
COLOR_BayerGR2RGB = COLOR_BayerGB2BGR,
COLOR_BayerBG2GRAY = 86,
COLOR_BayerGB2GRAY = 87,
COLOR_BayerRG2GRAY = 88,
COLOR_BayerGR2GRAY = 89,
COLOR_BayerBG2BGR_VNG = 62,
COLOR_BayerGB2BGR_VNG = 63,
COLOR_BayerRG2BGR_VNG = 64,
COLOR_BayerGR2BGR_VNG = 65,
COLOR_BayerBG2RGB_VNG = COLOR_BayerRG2BGR_VNG,
COLOR_BayerGB2RGB_VNG = COLOR_BayerGR2BGR_VNG,
COLOR_BayerRG2RGB_VNG = COLOR_BayerBG2BGR_VNG,
COLOR_BayerGR2RGB_VNG = COLOR_BayerGB2BGR_VNG,
COLOR_BayerBG2BGR_EA = 135,
COLOR_BayerGB2BGR_EA = 136,
COLOR_BayerRG2BGR_EA = 137,
COLOR_BayerGR2BGR_EA = 138,
COLOR_BayerBG2RGB_EA = COLOR_BayerRG2BGR_EA,
COLOR_BayerGB2RGB_EA = COLOR_BayerGR2BGR_EA,
COLOR_BayerRG2RGB_EA = COLOR_BayerBG2BGR_EA,
COLOR_BayerGR2RGB_EA = COLOR_BayerGB2BGR_EA,
COLOR_BayerBG2BGRA = 139,
COLOR_BayerGB2BGRA = 140,
COLOR_BayerRG2BGRA = 141,
COLOR_BayerGR2BGRA = 142,
COLOR_BayerBG2RGBA = COLOR_BayerRG2BGRA,
COLOR_BayerGB2RGBA = COLOR_BayerGR2BGRA,
COLOR_BayerRG2RGBA = COLOR_BayerBG2BGRA,
COLOR_BayerGR2RGBA = COLOR_BayerGB2BGRA,
COLOR_COLORCVT_MAX = 143;
复制代码

操作

class ColorTransferActivity : AppCompatActivity() {

private lateinit var mBinding: ActivityColorTransferBinding
private lateinit var mBgr: Mat

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_color_transfer)
mBgr = Utils.loadResource(this, R.drawable.lena)
showBgr()
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_color_transfer, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.bgr -> showBgr()
R.id.rgb -> showRgb()
R.id.yuv -> showYuv()
R.id.hsv -> showHsv()
R.id.lab -> showLab()
R.id.gray -> showGray()
}
return true
}

private fun showBgr() {
showMat(mBgr)
}

private fun showRgb() {
val rgb = Mat()
Imgproc.cvtColor(mBgr, rgb, Imgproc.COLOR_BGR2RGB)
showMat(rgb)
rgb.release()
}

private fun showYuv() {
val yuv = Mat()
Imgproc.cvtColor(mBgr, yuv, Imgproc.COLOR_BGR2YUV)
showMat(yuv)
yuv.release()
}

private fun showHsv() {
val hsv = Mat()
Imgproc.cvtColor(mBgr, hsv, Imgproc.COLOR_BGR2HSV)
showMat(hsv)
hsv.release()
}

private fun showLab() {
val lab = Mat()
Imgproc.cvtColor(mBgr, lab, Imgproc.COLOR_BGR2Lab)
showMat(lab)
lab.release()
}

private fun showGray() {
val gray = Mat()
Imgproc.cvtColor(mBgr, gray, Imgproc.COLOR_BGR2GRAY)
showMat(gray)
gray.release()
}

private fun showMat(source: Mat) {
val bitmap = Bitmap.createBitmap(source.width(), source.height(), Bitmap.Config.ARGB_8888)
Utils.matToBitmap(source, bitmap)
mBinding.ivLena.setImageBitmap(bitmap)
}

override fun onDestroy() {
super.onDestroy()
mBgr.release()
}
}
复制代码

结果

运行结果
运行结果

源码

https://github.com/onlyloveyd/LearningAndroidOpenCV

本文使用 mdnice 排版