注:在阴影检测算法中经常需要将RGB格式的图像转化为HSV格式,对于阴影区域而言,它的色度和饱和度相对于原图像而言变化不大,主要是亮度信息变化较大,,将RGB格式转化为HSV格式,就可以得到H、S、V分量,从而得到色度、饱和度、亮度得值;

转换程序:

void convert_ImageRGBtoHSV(const Mat& imageRGB, Mat &imageHSV)
{
 float fR, fG, fB;
 float fH, fS, fV;
 const float FLOAT_TO_BYTE = 255.0f;
 const float BYTE_TO_FLOAT = 1.0f / FLOAT_TO_BYTE;

// Create a blank HSV image
 //if (!imageHSV || imageRGB->depth != 8 || imageRGB->nChannels != 3) {
 //printf("ERROR in convertImageRGBtoHSV()! Bad input image.\n");
 //exit(1);
 //}

int h = imageRGB.rows;  // Pixel height.
 int w = imageRGB.cols;  // Pixel width.
 //int rowSizeRGB = imageRGB->widthStep; // Size of row in bytes, including extra padding.
 //char *imRGB = imageRGB->imageData; // Pointer to the start of the image pixels.
 //int rowSizeHSV = imageHSV->widthStep; // Size of row in bytes, including extra padding.
 //char *imHSV = imageHSV->imageData; // Pointer to the start of the image pixels.
 for (int y = 0; y < h; ++y) {
  for (int x = 0; x < w; ++x) {
   // Get the RGB pixel components. NOTE that OpenCV stores RGB pixels in B,G,R order.
   //uchar *pRGB = (uchar*)(imRGB + y*rowSizeRGB + x*3);
   int bB = imageRGB.at<Vec3b>(y, x)[0]; //*(uchar*)(pRGB+0); // Blue component
   int bG = imageRGB.at<Vec3b>(y, x)[1]; //*(uchar*)(pRGB+1); // Green component
   int bR = imageRGB.at<Vec3b>(y, x)[2]; //*(uchar*)(pRGB+2); // Red component

// Convert from 8-bit integers to floats.
   fR = bR * BYTE_TO_FLOAT;
   fG = bG * BYTE_TO_FLOAT;
   fB = bB * BYTE_TO_FLOAT;

// Convert from RGB to HSV, using float ranges 0.0 to 1.0.
   float fDelta;
   float fMin, fMax;
   int iMax;
   // Get the min and max, but use integer comparisons for slight speedup.
   if (bB < bG) {
    if (bB < bR) {
     fMin = fB;
     if (bR > bG) {
      iMax = bR;
      fMax = fR;
     }
     else {
      iMax = bG;
      fMax = fG;
     }
    }
    else {
     fMin = fR;
     fMax = fG;
     iMax = bG;
    }
   }
   else {
    if (bG < bR) {
     fMin = fG;
     if (bB > bR) {
      fMax = fB;
      iMax = bB;
     }
     else {
      fMax = fR;
      iMax = bR;
     }
    }
    else {
     fMin = fR;
     fMax = fB;
     iMax = bB;
    }
   }
   fDelta = fMax - fMin;
   fV = fMax;    // Value (Brightness).
   if (iMax != 0) {   // Make sure its not pure black.
    fS = fDelta / fMax;  // Saturation.
    float ANGLE_TO_UNIT = 1.0f / (6.0f * fDelta); // Make the Hues between 0.0 to 1.0 instead of 6.0
    if (iMax == bR) {  // between yellow and magenta.
     fH = (fG - fB) * ANGLE_TO_UNIT;
    }
    else if (iMax == bG) {  // between cyan and yellow.
     fH = (2.0f / 6.0f) + (fB - fR) * ANGLE_TO_UNIT;
    }
    else {    // between magenta and cyan.
     fH = (4.0f / 6.0f) + (fR - fG) * ANGLE_TO_UNIT;
    }
    // Wrap outlier Hues around the circle.
    if (fH < 0.0f)
     fH += 1.0f;
    if (fH >= 1.0f)
     fH -= 1.0f;
   }
   else {
    // color is pure Black.
    fS = 0;
    fH = 0; // undefined hue
   }

// Convert from floats to 8-bit integers.
   int bH = (int)(0.5f + fH * 255.0f);
   int bS = (int)(0.5f + fS * 255.0f);
   int bV = (int)(0.5f + fV * 255.0f);

// Clip the values to make sure it fits within the 8bits.
   if (bH > 255)
    bH = 255;
   if (bH < 0)
    bH = 0;
   if (bS > 255)
    bS = 255;
   if (bS < 0)
    bS = 0;
   if (bV > 255)
    bV = 255;
   if (bV < 0)
    bV = 0;

// Set the HSV pixel components.
   imageHSV.at<Vec3b>(y, x)[0] = bH;  // H component
   imageHSV.at<Vec3b>(y, x)[1] = bS;  // S component
   imageHSV.at<Vec3b>(y, x)[2] = bV;  // V component
  }
 }
}

RGB格式图像转化为HSV格式的更多相关文章

  1. Typora原生态的图片格式快速转化为HTML格式

    Typora更改图片样式 前言 ​ 在Typora中插入的图片,默认是居中且显示原图大小的,如果想要缩小显示,可以右击图片选择缩放图片. ​ 但是,当我上传到博客园中时,并没有保留 居中.缩放 的样式 ...

  2. 将搜狗词库(.scel格式)转化为txt格式

    参考:http://blog.csdn.net/zhangzhenhu/article/details/7014271 #!/usr/bin/python # -*- coding: utf-8 -* ...

  3. 如何将微信上传AMR格式语音转化为MP3格式

    1. 服务器安装ffmpeg 2. 执行命令 ffmpeg -i {amr_file_path} -f mp3 -acodec libmp3lame -y {mp3_file_path} public ...

  4. 将搜狗词库.scel格式转化为.txt格式

    由于项目中要用到词库,而下载的搜狗词库是.scel格式,所以就用python脚本将搜狗词库.scel格式文件转化为.txt格式文件. #!/bin/python # -*- coding: utf-8 ...

  5. bmp格式图像的读写函数(对一个开源代码的封装)

    在网上看到一段读写bmp格式图像的代码,本文对这段代码分成两个函数封装起来方便使用,一个函数是读取bmp格式的图像,一个是向指定文件写入bmp格式的图像. 前提 我们不需要知道这段代码是如何读取bmp ...

  6. DICOM图像转出为bmp格式图像方法(matlab程序实现)

    在matlab中用dicomread读取dicom文件后,生成一个MxN矩阵(对应图像像素个数),每个像素灰度数据是int16格式 但是bmp图像灰度是int8格式的(灰度范围0~255),所以若想把 ...

  7. TensorFlow 自定义模型导出:将 .ckpt 格式转化为 .pb 格式

    本文承接上文 TensorFlow-slim 训练 CNN 分类模型(续),阐述通过 tf.contrib.slim 的函数 slim.learning.train 训练的模型,怎么通过人为的加入数据 ...

  8. 图像RGB2YUV与YUV2RGB格式互转介绍

    1 YUV格式与RGB格式说明 由于不同国家的电视信号系统支持的图像格式不同,有YUV格式成像,也有RGB格式成像,因此为了保证兼容性,需要进行RGB与YUV格式的互转. 另外YUV格式具有亮度信息和 ...

  9. RGB和YUV、YCbCr 以及格式的转换总结

    比较好的文章收集链接: https://www.douban.com/note/76361504/ http://blog.sina.com.cn/s/blog_a85e142101010h8n.ht ...

随机推荐

  1. python实现http get请求

    接口请求方式为get请求,如下图抓包查看 Python实现脚本请求接口并以中文打印接口返回的数据 import urllib.parse import urllib.request url = &qu ...

  2. 4、NFS

    一.NFS简介 4.1.1:什么是NFS NFS(Network File System,网络文件系统)是由SUN公司开发,并于1984年推出的技术,通过使用NF,用户和程序可以向访问本地文件一样访问 ...

  3. dilated convolutions:扩张卷积

    最近在阅读<Context Encoding for Semantic Segmentation>中看到应用了dilated convolutions. 扩张卷积与普通的卷积相比,除了卷积 ...

  4. javascript中的词法分析

    词法分析 JavaScript中在调用函数的那一瞬间,会先进行词法分析. 词法分析的过程: 当函数调用的前一瞬间,会先形成一个激活对象:Avtive Object(AO),并会分析以下3个方面: 1: ...

  5. MapRedcue的demo(协同过滤)

    MapRedcue的演示(协同过滤) 做一个关于电影推荐.你于你好友之间的浏览电影以及电影评分的推荐的协同过滤. 百度百科: 协同过滤简单来说是利用某兴趣相投.拥有共同经验之群体的喜好来推荐用户感兴趣 ...

  6. STL 小白学习(3) vector

    #include <iostream> using namespace std; #include <vector> void printVector(vector<in ...

  7. Linux常用命令汇总集

    cd ./ 当前目录 ../ 上级目录 / 代表根目录 or 代表目录和文件之间的分隔符 .. pwd 查看当前路径 LS 查看当前目录下的文件 ls ./a/ 查看目标路径下的文件 tab 自动补全 ...

  8. PDF 补丁丁 0.6.0.3282 版发布(修复内存漏洞)

    补丁丁的新测试版修复了旧版在导出图片.分析文件结构时的内存漏洞. 对于希望表达对本软件感情的用户,可点击“帮助”菜单的“关于本程序及作者”命令,用微信扫描里面的二维码表达您的谢意. 新的测试版正在制作 ...

  9. 微服务-注册与发现-zookeeper bydasn

    目录 一.微服务注册的概述 二.zookeeper2.1 zookeeper安装启动2.2 zookeeper集群搭建2.3 zkcli操作 一.微服务注册概述 在微服务中,有这么一个东西叫服务注册表 ...

  10. Linux下实现ssh免密认证

    添加域名映射 配置ssh免密登陆 拷贝master服务器公钥至本机 验证master服务器ssh免密登录其余服务器 添加域名映射 打开hosts文件 Vim /etc/hosts 添加域名对象 配置s ...