gpt4 book ai didi

c++ - 如何在 C++ 中将 Cairo Image 表面转换为 opencv Mat

转载 作者:太空宇宙 更新时间:2023-11-03 22:14:28 29 4
gpt4 key购买 nike

我在 cairomm 库中有一个 cairo Image surface

开罗::RefPtr cropSurface = 开罗::ImageSurface::create(开罗::FORMAT_ARGB32, 宽度, 高度);

如何在 C++ 中将其转换为 Opencv Mat?

最佳答案

这里是 cairo + opencv 用法的一个简短示例,希望它有用:

#include <iostream>
#include <vector>
#include <stdio.h>
#include "opencv2/opencv.hpp"

#include "cairo/cairo-win32.h"
#include "cairo/cairo-pdf.h"
#include "cairo/cairo-ps.h"
#include "cairo/cairo-svg.h"
using namespace std;
using namespace cv;
//-----------------------------------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------------------------------
void MatToCairo(Mat &MC3,cairo_surface_t *surface)
{
Mat MC4 = Mat(cairo_image_surface_get_width(surface),cairo_image_surface_get_height(surface),CV_8UC4,cairo_image_surface_get_data(surface),cairo_image_surface_get_stride(surface));
vector<Mat> Imgs1;
vector<Mat> Imgs2;
cv::split(MC4,Imgs1);
cv::split(MC3,Imgs2);
for(int i=0;i<3;i++)
{
Imgs1[i]=Imgs2[i];
}
// Alpha - прозрачность
Imgs1[3]=255;
cv::merge(Imgs1,MC4);
}
//-----------------------------------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------------------------------
void CairoToMat(cairo_surface_t *surface,Mat &MC3)
{
Mat MC4 = Mat(cairo_image_surface_get_width(surface),cairo_image_surface_get_height(surface),CV_8UC4,cairo_image_surface_get_data(surface),cairo_image_surface_get_stride(surface));
vector<Mat> Imgs1;
vector<Mat> Imgs2;
cv::split(MC4,Imgs1);
cv::split(MC3,Imgs2);
for(int i=0;i<3;i++)
{
Imgs2[i]=Imgs1[i];
}
cv::merge(Imgs2,MC3);
}
//-----------------------------------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------------------------------
int main( int argc, char** argv )
{
int m_spcount;
double m_compactness(0);
int width;
int height;
Mat Img=imread("D:\\ImagesForTest\\lena.jpg");
namedWindow("Image");
width=Img.cols;
height=Img.rows;

cairo_surface_t *surface;
cairo_t *cr;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
cr = cairo_create(surface);

MatToCairo(Img,surface);

cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, 32.0);
cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
cairo_move_to (cr, 10.0, 50.0);
cairo_show_text (cr, "Hello, world");

cairo_surface_write_to_png (surface, "hello.png");

CairoToMat(surface,Img);

imshow("Image", Img);

waitKey(0);
cairo_destroy (cr);
cairo_surface_destroy (surface);
destroyAllWindows();
return 0;
}

关于c++ - 如何在 C++ 中将 Cairo Image 表面转换为 opencv Mat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19948319/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com