0

How do we print the BGR and HSV values of a single colour on screen using putText() in C++ OpenCV?, assuming that I have a Mat image with all pixels of single colour

2 Answers2

1

You can convert a Vec_<Tp> to string in two ways:

  1. Using std::iostream:

    Vec3b color = ...
    std::stringstream ss;
    ss << color;
    std::string color_string = ss.str();
    
  2. Creating your own string:

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

You can then use putText to draw the string on the image.

enter image description here

Here is the full code. Note the function bgr2hsv that converts a only single Vec3b from BGR to HSV (adapted from 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;
}
Community
  • 1
  • 1
Miki
  • 40,887
  • 13
  • 123
  • 202
-1
#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;
}
Saransh Kejriwal
  • 2,467
  • 5
  • 15
  • 39