将bmp图像拼接在一起的应用程序。需要帮助

时间:2019-04-13 14:39:07

标签: c++ visual-c++ bitmap

我应该创建一些代码来将文件夹中找到的N个bmp图像拼接在一起。目前,我只是想将图像并排添加在一起,而不关心它们中的共同区域(我指的是全景图像的制作方式)。

我尝试在线使用一些示例来满足我所需的不同功能,这些示例我已经部分理解。我目前陷入困境,因为我无法真正弄清楚出什么问题了。

bmp.h文件的基础是此页面: https://solarianprogrammer.com/2018/11/19/cpp-reading-writing-bmp-images/ 我要附上我的代码和VS抛出异常的屏幕截图。

主要:

#include "bmp.h"
#include <fstream>
#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main() {    

    int totalImages = 0;
    int width = 0;
    int height;
    int count = 0;
    std::string path = "imagini";
    //Here i count the total number of images in the directory. 
    //I need this to know the width of the composed image that i have to produce.
    for (const auto & entry : fs::directory_iterator(path))
        totalImages++;
    //Here i thought about going through the directory and finding out the width of the images inside it.
    //I haven't managed to think of a better way to do this (which is probably why it doesn't work, i guess).
    //Ideally, i would have taken one image from the directory and multiply it's width 
    //by the total number of images in the said directory, thus getting the width of the resulting image i need.
    for (auto& p : fs::directory_iterator(path))
    {
        std::string s = p.path().string();
        const char* imageName = s.c_str();  
        BMP image(imageName);
        width = width + image.bmp_info_header.width;
        height = image.bmp_info_header.height;      
    }

    BMP finalImage(width, height);
    //Finally, I was going to pass the directory again, and for each image inside of it, i would call
    //the create_data function that i wrote in bmp.h.
    for (auto& p : fs::directory_iterator(path))
    {   
        count++;
        std::string s = p.path().string();
        const char* imageName = s.c_str();
        BMP image(imageName);
        //I use count to point out which image the iterator is currently at.
        finalImage.create_data(count, image, totalImages);
    }   
    //Here i would write the finalImage to a bmp image.
    finalImage.write("textura.bmp");
}

bmp.h(我只添加了我写的部分,其余代码可在我上面提供的链接中找到):

// This is where I try to copy the pixel RGBA values from the image passed as parameter (from it's data vector) to my 
    // resulting image (it's data vector) .
    // The math should be right, I've gone through it with pen&paper, but right now I can't test it because the code doesn't work for other reasons.
    void create_data(int count, BMP image, int totalImages)
    {
        int w = image.bmp_info_header.width * 4;
        int h = image.bmp_info_header.height;
        int q = 0;
        int channels = image.bmp_info_header.bit_count / 8;
        int startJ = w * channels * (count - 1);
        int finalI = w * channels * totalImages* (h - 1);
        int incrementI = w * channels * totalImages;
        for(int i = 0; i <= finalI; i+incrementI)
            for (int j = i + startJ; j < i + startJ + w * channels; j+4) 
            {
                data[j]  =image.data[q];
                data[j+1]=image.data[q+1];
                data[j+2]=image.data[q+2];
                data[j+3]=image.data[q+3];
                q = q + 4;
            }
    }   

我收到错误:https://imgur.com/7fq9BH4

这是我第一次发布问题,我只在这里查找答案。如果我没有为我的问题提供足够的信息,或者我做的事情不正确,我深表歉意。

此外,英语是我的第二语言,所以我希望我的观点很清楚。

编辑:由于我忘了提及,所以我想在不使用OpenCV或ImageMagick等外部库的情况下执行此代码。

0 个答案:

没有答案