📜  在C++中使用openCV在图像上书写

📅  最后修改于: 2021-05-30 19:36:41             🧑  作者: Mango

在本文中,我们将讨论如何使用OpenCV C++覆盖图像。从OpenCV的C++库函数putText()将被用来写文本的图像上。

程序1:

下面的程序显示了如何在空白背景图像上写文本:

C++
// C++ program to demonstrate the
// above approach
#include 
#include 
  
// Library to include for
// drawing shapes
#include 
#include 
using namespace cv;
using namespace std;
  
// Driver Code
int main(int argc, char** argv)
{
    // Create a blank image of size
    // (500 x 500) with white background
    // (B, G, R) : (255, 255, 255)
    Mat image(500, 500, CV_8UC3,
              Scalar(255, 255, 255));
  
    // Check if the image is created
    // successfully.
    if (!image.data) {
        cout << "Could not open or"
             << " find the image"
             << endl;
        return 0;
    }
  
    // Writing over the Image
    Point org(30, 100);
    putText(image, "Text On Image", org,
            FONT_HERSHEY_SCRIPT_COMPLEX, 2.1,
            Scalar(0, 0, 255), 2, LINE_AA);
  
    // Show our image inside a window.
    imshow("Output", image);
    waitKey(0);
  
    return 0;
}


C++
// C++ program to demonstrate the
// above approach
#include 
#include 
  
// Library to include for
// drawing shapes
#include 
#include 
using namespace cv;
using namespace std;
  
// Driver Code
int main(int argc, char** argv)
{
    // Create a blank image of size
    // (500 x 500) with white background
    // (B, G, R) : (255, 255, 255)
    Mat image = imread("C:/Users/harsh/Downloads/geeks.png",
                       IMREAD_COLOR);
  
    // Check if the image is
    // created successfully.
    if (!image.data) {
        cout << "Could not open or"
             << " find the image" << std::endl;
        return 0;
    }
  
    // Writing over the Image
    Point org(1, 30);
    putText(image, "Geeks For Geeks", org,
            FONT_HERSHEY_SCRIPT_COMPLEX, 1.0,
            Scalar(0, 255, 0), 2, LINE_AA);
  
    // Show our image inside a window.
    imshow("Output", image);
    waitKey(0);
  
    return 0;
}


输出:

程式2:

下面的程序显示了如何在加载的图像上写入文本:

C++

// C++ program to demonstrate the
// above approach
#include 
#include 
  
// Library to include for
// drawing shapes
#include 
#include 
using namespace cv;
using namespace std;
  
// Driver Code
int main(int argc, char** argv)
{
    // Create a blank image of size
    // (500 x 500) with white background
    // (B, G, R) : (255, 255, 255)
    Mat image = imread("C:/Users/harsh/Downloads/geeks.png",
                       IMREAD_COLOR);
  
    // Check if the image is
    // created successfully.
    if (!image.data) {
        cout << "Could not open or"
             << " find the image" << std::endl;
        return 0;
    }
  
    // Writing over the Image
    Point org(1, 30);
    putText(image, "Geeks For Geeks", org,
            FONT_HERSHEY_SCRIPT_COMPLEX, 1.0,
            Scalar(0, 255, 0), 2, LINE_AA);
  
    // Show our image inside a window.
    imshow("Output", image);
    waitKey(0);
  
    return 0;
}

输出:

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”