将背景图像添加到此代码而不是黑色?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将背景图像添加到此代码而不是黑色?相关的知识,希望对你有一定的参考价值。
我看到这段代码,需要添加背景图片
像这样enter image description here
我怎么能这样做添加背景而不是黑色背景
请帮我
我从另一个问题得到这个代码,但我不知道如何改变背景
Create video from array of pixel values in C++
#include <iostream>
#include "CImg.h"
using namespace std;
using namespace cimg_library;
int main()
{
const unsigned int width=1024;
const unsigned int height=768;
// Basic frame we will draw in
CImg<unsigned char> image(width,height,1,3);
unsigned char magenta[] = {255,0,255};
// We are going to output 300 frames of 1024x768 RGB raw video
// ... making a 10s long video at 30fps
int radius=100;
int cx=100;
int cy=100;
for(int frame=0;frame<300;frame++){
// Start with black - it shows fewer stains ;-)
image.fill(0);
image.draw_circle(cx,cy,radius,magenta);
// Move and re-colour circle
cx+=2; cy++; if(magenta[1]!=255){magenta[1]++;}
// Output to ffmpeg to make video, in planar GBR format
// i.e. run program like this
// ./main | ffmpeg -y -f rawvideo -pixel_format gbrp -video_size 1024x768 -i - -c:v h264 -pix_fmt yuv420p video.mov
char* s=reinterpret_cast<char*>(image.data()+(width*height)); // Get start of G plane
std::cout.write(s,width*height); // Output it
s=reinterpret_cast<char*>(image.data()+2*(width*height)); // Get start of B plane
std::cout.write(s,width*height); // Output it
s=reinterpret_cast<char*>(image.data()); // Get start of R plane
std::cout.write(s,width*height); // Output it
}
}
我用这个编译
./main | ffmpeg -y -f rawvideo -pixel_format gbrp -video_size 1024x768 -i - -c:v h264 -pix_fmt yuv420p video.mov
答案
您需要做的就是在开始时加载背景图像,然后在每次迭代时复制它以进行绘制:
#include <iostream>
#include "CImg.h"
using namespace std;
using namespace cimg_library;
int main()
{
// Background frame we will draw
CImg<unsigned char> bg("background.jpg");
bg.resize(1024,768);
int width=bg.width();
int height=bg.height();
unsigned char magenta[] = {255,0,255};
// We are going to output 300 frames of RGB raw video
// ... making a 10s long video at 30fps
int radius=100;
int cx=100;
int cy=100;
for(int frame=0;frame<300;frame++){
// Take copy of background image
CImg<unsigned char> image(bg,false);
image.draw_circle(cx,cy,radius,magenta);
// Move and re-colour circle
cx+=2; cy++; if(magenta[1]!=255){magenta[1]++;}
// Output to ffmpeg to make video, in planar GBR format
// i.e. run program like this
// ./main | ffmpeg -y -f rawvideo -pixel_format gbrp -video_size 1024x768 -i - -c:v h264 -pix_fmt yuv420p video.mov
char* s=reinterpret_cast<char*>(image.data()+(width*height)); // Get start of G plane
std::cout.write(s,width*height); // Output it
s=reinterpret_cast<char*>(image.data()+2*(width*height)); // Get start of B plane
std::cout.write(s,width*height); // Output it
s=reinterpret_cast<char*>(image.data()); // Get start of R plane
std::cout.write(s,width*height); // Output it
}
}
以上是关于将背景图像添加到此代码而不是黑色?的主要内容,如果未能解决你的问题,请参考以下文章