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

时间:2016-03-08 08:41:05

标签: c++ opencv image-processing

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

2 个答案:

答案 0 :(得分:1)

您可以通过两种方式将Vec3b color = ... std::stringstream ss; ss << color; std::string color_string = ss.str(); 转换为字符串:

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

    putText
  2. 创建自己的字符串:

    bgr2hsv
  3. 然后,您可以使用Vec3b在图像上绘制字符串。

    enter image description 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; } 将唯一的ServerName mywebapp.com ServerAlias mywebapp.com ProxyRequests On ProxyVia On <Proxy *> AddDefaultCharset Off Order deny,allow Allow from all </Proxy> RewriteEngine On Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^mywebapp.com$ RewriteRule ^(.*)$ http://mywebapp.com/app1$1 [R=301,L] ProxyPass / ajp://localhost:8009/ ProxyPassReverse / ajp://localhost:8009/ 从BGR转换为HSV(改编自here)。

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^mywebapp.com$
    RewriteRule ^(.*)$ http://mywebapp.com/app1$1 [R=301,L]
    

答案 1 :(得分:-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;
}