在屏幕上打印 BGR 和 HSV 颜色值

Posted

技术标签:

【中文标题】在屏幕上打印 BGR 和 HSV 颜色值【英文标题】:Printing BGR and HSV colour values on screen 【发布时间】:2016-03-08 08:41:04 【问题描述】:

我们如何在 C++ OpenCV 中使用 putText() 在屏幕上打印单色的 BGR 和 HSV 值?假设我有一个所有像素都是单色的 Mat 图像

【问题讨论】:

【参考方案1】:

您可以通过两种方式将Vec_<Tp> 转换为字符串:

    使用std::iostream

    Vec3b color = ...
    std::stringstream ss;
    ss << color;
    std::string color_string = ss.str();
    

    创建自己的字符串:

    Vec3b color = ...
    std::string text = "[" +    std::to_string(color[0]) + ", " + 
                                std::to_string(color[1]) + ", " + 
                                std::to_string(color[2]) + "]";
    

然后您可以使用putText 在图像上绘制字符串。

这是完整的代码。请注意函数 bgr2hsv 将唯一的单个 Vec3b 从 BGR 转换为 HSV(改编自 here)。

#include <opencv2\opencv.hpp>
#include <sstream>
using namespace cv;

Vec3b bgr2hsv(Vec3b bgr)

    Mat3b m(bgr);
    cvtColor(m, m, COLOR_BGR2HSV);
    return m(0);


int main()

    // Create a green image
    Mat3b img(200, 200, Vec3b(0,255,0));

    // Get the color of the first pixel of the image
    Vec3b colorBGR = img(0);

    // Get the HSV color
    Vec3b colorHSV = bgr2hsv(colorBGR);

    // Vec_<Tp> can be used directly with streams
    std::stringstream ss;
    ss << colorBGR;
    putText(img, ss.str(), Point(10, 50), FONT_HERSHEY_PLAIN, 1, Scalar(255,0,0));

    // Or you can build your own string
    std::string text = "[" +    std::to_string(colorHSV[0]) + ", " + 
                                std::to_string(colorHSV[1]) + ", " + 
                                std::to_string(colorHSV[2]) + "]";
    putText(img, text, Point(10, 150), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 255));

    // Show result
    imshow("img", img);
    waitKey();

    return 0;

【讨论】:

【参考方案2】:
#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

char RGB_window[30] = "RGB Window";
char HSV_window[30] = "HSV Window";
 Mat src,hsv;

 static void onMouse( int event, int x, int y, int f, void* )
 Mat image=src.clone();
 Vec3b rgb=image.at<Vec3b>(y,x);
 int B=rgb.val[0];
 int G=rgb.val[1];
 int R=rgb.val[2];

  Mat HSV;
  Mat RGB=image(Rect(x,y,1,1));//capture that pixel in its own ROI
  cvtColor(RGB, HSV,CV_BGR2HSV);

    Vec3b hsv=HSV.at<Vec3b>(0,0);
    int H=hsv.val[0];
    int S=hsv.val[1];
    int V=hsv.val[2];

    char name[30];
    sprintf(name,"B=%d",B);
    putText(image,name, Point(150,40) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    sprintf(name,"G=%d",G);
    putText(image,name, Point(150,80) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    sprintf(name,"R=%d",R);
    putText(image,name, Point(150,120) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    sprintf(name,"H=%d",H);
    putText(image,name, Point(25,40) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    sprintf(name,"S=%d",S);
    putText(image,name, Point(25,80) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    sprintf(name,"V=%d",V);
    putText(image,name, Point(25,120) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,255,0), 2,8,false );

    sprintf(name,"X=%d",x);
    putText(image,name, Point(25,300) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,0,255), 2,8,false );

    sprintf(name,"Y=%d",y);
    putText(image,name, Point(25,340) , FONT_HERSHEY_SIMPLEX, .7, Scalar(0,0,255), 2,8,false );
    //namedWindow("Ref HSV",WINDOW_NORMAL);
    Mat ref(50,50,CV_8UC3,Scalar(H,S,V));
 //imwrite("hsv.jpg",image);
 imshow( RGB_window, image );
 //imshow( "Ref HSV",ref);





int main()


//VideoCapture cap(0);
static int Bs=0,Gs=0,Rs=0;
namedWindow("colourCtrl");
 //src = imread("bgr.png",1);
for(;;)

  //cap>>src;
  createTrackbar("B","colourCtrl",&Bs,255);
  createTrackbar("G","colourCtrl",&Gs,255);
  createTrackbar("R","colourCtrl",&Rs,255);
  Mat refRGB(500,500,CV_8UC3,Scalar(Bs,Gs,Rs));
  src=refRGB;
  cvtColor(src,hsv,CV_BGR2HSV);
  imshow(RGB_window,src);
 imshow(HSV_window,hsv);
 setMouseCallback( RGB_window, onMouse, 0 );
 setMouseCallback( HSV_window, onMouse, 0 );
 char c=waitKey(10);
 if(c=='b')
    break;
 
return 0;

【讨论】:

以上是关于在屏幕上打印 BGR 和 HSV 颜色值的主要内容,如果未能解决你的问题,请参考以下文章

python使用openCV加载图像并将BGR格式转换成HSV格式定义HSV格式中需要分离颜色的掩码(掩模)区间(mask)并使用mask信息进行颜色分离BGR格式的图像转化为RGB并可视化

HSV色彩空间

颜色空间转换

如何在OpenCV中为InRange阈值选择颜色的最佳HSV值

将 RGB 颜色快速更改为 HSV

OpenCV竟然可以这样学!成神之路终将不远