【问题标题】:C++ Image processing data blocks into array / pointerC++图像处理数据块成数组/指针
【发布时间】:2016-12-18 21:34:17
【问题描述】:

我在 txt 文件中有两个灰度图像,一个是主图像的较小块。我已将图像读入两个不同的二维向量矩阵。

图像的行和列是:

主要:M = 768 N = 1024

SubImg:R = 49 C = 36

int R = 49;诠释 C = 36; //子图像行/列

int M = 768;整数 N = 1024; //主图像行/列

我已经按宽度:49 和高度:36 的块循环遍历主图像,我想将每个块放入一个数组中,所以我可以将数组与子图像(使用最近邻搜索)进行比较,看看哪个块具有最接近子图像的结果。

这是主图的循环代码:

for (double bx = 0; bx < M; bx += R)
    for (double by = 0; by < N; by += C)
    {
        for (int x = 0; ((x < R) && ((bx + x) < M)); ++x)
        {
            for (int y = 0; ((y < C) && ((by + y) < N)); ++y)
            {
                if ((bx + x) >= M) 
                {
                    std::cout << (bx + x) << (by + y) << " ";

                }
                cout << MainIMG_2DVector[bx + x][by + y] << " ";
            }
        }
        cout << "\n\n\n" << endl;
    }

此循环一次性显示所有块。我遇到的问题是我不知道如何将每个块放入一个数组中,所以我可以比较数据。 另外,使用指针而不是数组更好吗?

谢谢

【问题讨论】:

    标签: c++ arrays pointers for-loop image-processing


    【解决方案1】:

    我不确定你想如何比较块,但就存储块而言,你可以像这样制作一个简单的 Block 对象:

    const double R = 49, C = 36;
    
    // Block object (takes in your image 2D vector and x/y coordinates)
    class Block {
    public:
        Block(std::vector<std::vector<int>> *img_vector, int bx, int by);
        int compare(const Block &block) const;
    
    private:
        std::vector<std::vector<int>> *img_vector;
        int bx, by;
    };
    
    Block::Block(std::vector<std::vector<int>> *img_vector, int bx, int by) {
        this->img_vector = img_vector;
        this->bx = bx;
        this->by = by;
    }
    
    // Compare any two blocks
    int Block::compare(const Block &block) const {
        for (int x = bx; x < bx + R; x++) {
            for (int y = by; y < by + C; y++) {
                // Do some comparing
                std::cout << "Compare " << (*img_vector)[x][y]
                    << " with " << (*block.img_vector)[x][y] << std::endl;
            }
        }
        return 0; // Return some value that indicates how they compare
    }
    

    然后将图像块添加到向量中:

    // Add main image blocks to vector
    std::vector<Block> main_img_blocks;
    for (double bx = 0; bx < M; bx += R) {
        for (double by = 0; by < N; by += C)
            main_img_blocks.push_back(Block(&MainIMG_2DVector, bx, by));
    }
    
    // Do the same for sub image blocks...
    
    // Invoke the compare function between 2 blocks at a time
    int comp_value = main_img_blocks[0].compare(main_img_blocks[1]);
    

    希望有帮助:)

    【讨论】:

    • 您好,谢谢您的回答,但是我该如何声明 Block.我不断收到此错误消息(“Block”类不存在默认构造函数。对不起,我是 C++ 新手。干杯。
    • 有几种不同的方法可以做到这一点,通常类在它们自己的头文件中定义并在相应的 .cpp 文件中实现。一个通用的 C++ 对象教程是 here 让你开始。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多