How to overlay text on image when working with cv::Mat type

后端 未结 7 1007
广开言路
广开言路 2020-12-29 21:57

I am using opencv 2.1. In my code I have a few images stored as Mat objects initialized like this:

Mat img1 = imread(\"img/stuff.pgm\", CV_LOAD_IMAGE_GRAYSCA         


        
相关标签:
7条回答
  • 2020-12-29 22:28

    One nasty detail that I saw into my test code: pay attention into the import stament - it is not displayed into most examples and it needs to be the right import.

    My test code used only the putText sample above and I did included the imgproc.h just as I did into some of my oldcode. The code compiled and linked fine however I was facing one weirdy behaviour with the putText (some garbage into my image).

    It was a PITA until I figured out that the import was messing up with my social life ...

    imageText.cpp

    #include "Imaging/imageText.h"
    #include "Commons/xLog.h"
    #include "opencv2/imgproc.hpp" // << Seems to work right?
    using namespace cv;
    
    namespace imaging
    {
       inline Mat image2mat( SmartImage image ) NOEXCEPTION
       {
          //TODO: hard coded to work only with CV_8UC3, see the other cases ...
          Mat mat(
             Size( image->WIDTH, image->HEIGHT ),
             CV_8UC3,
             image->buffer,
             Mat::AUTO_STEP
          );
    
          return mat;
       }
    
       inline void _writeText_( SmartImage image, const string TEXT )
       {
          Mat mat( image2mat( image ) );
    
          string text = "Funny text inside the box";
          int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
          double fontScale = 2;
          int thickness = 3;  
          Point textOrg( 10, 130 );
          putText( mat, text, textOrg, fontFace, fontScale, Scalar::all( 255 ), thickness, 8 );
       }
    
       const bool writeText( SmartImage image, const string text )  NOEXCEPTION
       {
          try
          {
             _writeText_( image, text );
             return true;
          }
          catch( cv::Exception& e )
          {
             LOG_ERROR( "writeText OpenCV ERROR: " << e.what() << "!" );
          }
          catch( ... )
          {
             LOG_ERROR( "writeText ERROR!" );
          }
          return false;
       }
    }
    

    Then I just changed the imgproc import above to

    #include <opencv2/opencv.hpp> // << It does includes ALL opencv stuff
    

    My 5 cents.

    0 讨论(0)
提交回复
热议问题