如何在 c++ 中混合/合并 pgm 格式的两个图像程序

发布于 2024-11-03 22:27:09 字数 138 浏览 1 评论 0原文

所以我对此很陌生,对 C++ 也很陌生,但我一直致力于创建此类来混合两个输入图像。我知道我必须创建一个空白矩阵,输入要混合的两个图像,对于空矩阵的每个像素将其设置为等于其他两个图像中相同像素位置的平均值,困难的部分是编写代码为此......任何意见都将不胜感激!

So I am new to this and also new to c++ but I am stuck in creating this class to blend two input images. I know that I have to create a blank matrix, input the two images to be blended, for each pixel of the empty matrix set it equal to the average of that same pixel location in the other two images, the hard part is writing the code for it....any input is greatly appreciated!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘人皮目录ツ 2024-11-10 22:27:09

PGM 格式非常简单: http://en.wikipedia.org/wiki/Portable_graymap

所以使用该信息将图像读入数组,然后按照您所说的操作,迭代数组并将平均像素值粘贴到另一个数组中。然后将其写入另一个文件并使用可以查看 pgm 图像的图像程序来查看结果。

下面是一些写出 3 像素 x 3 像素 pgm 图像的代码:

std::cout << "P2\n";
std::cout << "3 3\n";
std::cout << "10\n";
std::cout << "0 10 0\n";
std::cout << "10 0 10\n";
std::cout << "0 10 0\n";

下面是一些使用 valarrays 进行混合的代码:

std::valarray<int> imageA,imageB;
//add code to load image data into valarrays here
//the next line does the blending, adding corresponding pixel values together and dividing by two to get the average
std::valarray<int> blend = (imageA + imageB) / 2; 

The PGM format is really simple: http://en.wikipedia.org/wiki/Portable_graymap

So use that info to read the images into arrays, then do just as you said, iterate over the arrays and stick the average pixel values into another array. Then write that out into another file and use an image program that can view pgm images to see your result.

Here's some code that writes out a 3 pixel by 3 pixel pgm image:

std::cout << "P2\n";
std::cout << "3 3\n";
std::cout << "10\n";
std::cout << "0 10 0\n";
std::cout << "10 0 10\n";
std::cout << "0 10 0\n";

Here's some code that uses valarrays to do the blending:

std::valarray<int> imageA,imageB;
//add code to load image data into valarrays here
//the next line does the blending, adding corresponding pixel values together and dividing by two to get the average
std::valarray<int> blend = (imageA + imageB) / 2; 
聽兲甴掵 2024-11-10 22:27:09

首先,您需要将图像从磁盘读取到内存中。最好找到一个 .png 库,否则您将不得不自己构建。这是一个起点(http://www.libpng.org/pub/png/pngaptk.html)。

接下来,您需要从 png 阅读器获取输出并将其转换为您想要的格式。输出可能已经是一个二维数组,因此您将被设置在那里。然后,您只需读取每个文件(如果它们很大,即无法放入内存),那么您需要通过平铺或扫描线来执行此操作,对数组进行平均,然后将您创建的新数组发送回png 编写器 API。

如果这必须在 C++ 中完成,那么这就是您可以做到的一般方法。如果你能掌握 IDL 或 ENVI,这可以用大约 5 行代码完成。

Well first you'll need to read the image from the disk and into memory. It's probably best to find a .png library otherwise you'll have to roll your own. Here's a starting point for that (http://www.libpng.org/pub/png/pngaptk.html).

Next you'll need to take the output from the png reader and convert it to the format you want. Likely the output will already be a 2D array, so you'll be set there. Then you just read each file (if they're large, i.e., can't fit in memory) then you'll need to do it by tiles or scanlines, average the arrays, and then send the new array you created back to the png writer API.

If this HAS to be done in c++, that's the general way you can do it. If you can get hold of IDL or ENVI this could be done in about 5 lines of code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文