将数组拆分为子块

时间:2014-12-03 13:45:18

标签: c++ arrays image-segmentation

我想要实现的目标是:

我有一个图像,我需要将其拆分为16x16的子块,我正在研究这个算法。但是出于测试目的,我使用的是一个小矩阵:

A = {1, 2, 3, 4}

现在我想要的是:2个包含以下内容的块

A[1] = {1 2};
A[2] = {3, 4}; 

我试过使用以下内容:

double matrix[4] = {1, 2, 3, 4};

for(int i = 0; (i < 4); i++)
{
        for(unsigned j=i; (j < 2); j +=2)
        {
            std::cout << j << ' ';
        }
        std::cout << std::endl;
}

我的思维过程是循环遍历整个数组(4),然后每次递增2以创建1x2块。然而,这不起作用。

我在哪里错了?

3 个答案:

答案 0 :(得分:1)

那样的东西? (输出和分配都是)

int LEN = 4;
int INNER = 2;
int OUTER_LEN = LEN/INNER_LEN;
double matrix[LEN] = {1, 2, 3, 4};
double* matrix2[OUTER_LEN];

for(int i = 0; i < OUTER_LEN; i++)
{ 
        matrix2[i] = &matrix[i*INNER_LEN];
        for(unsigned j=0; j < INNER_LEN; j++)
        {
            std::cout << matrix[i*INNER_LEN+j] << ' ';
        }
        std::cout << std::endl;
}

答案 1 :(得分:1)

只是为了输出你可以做类似的事情:

#include <iostream>

int main(){
  const size_t SIZE = 4;
  const size_t PART_SIZE = 2;
  double matrix[4] = {1, 2, 3, 4};

  for(int i = 0; (i < SIZE); i += PART_SIZE)
  {
    for(size_t j = i; (j < i + PART_SIZE) && j < SIZE; j += 1)
    {
        std::cout << matrix[j] << ' ';
    }
    std::cout << std::endl;
  }
}

添加另一个矩阵:

#include <iostream>

int main(){
    const size_t SIZE = 4;
    const size_t PART_SIZE = 2;
    size_t partsNumber = SIZE / PART_SIZE; // Beware of SIZE that is not divisible by PART_SIZE - partsNumber will be too small
    double matrix[4] = { 1, 2, 3, 4 };

    // To do it properly I should make it dynamic array with size of partsNumber instead of the 2 literals
    double parts_matrix[2][PART_SIZE]; 

    for (int i = 0; (i < SIZE); i += PART_SIZE) {
        for (size_t j = i; (j < i + PART_SIZE) && j < SIZE; j += 1) {
            std::cout << matrix[j] << ' ';
            parts_matrix[j / partsNumber][j % PART_SIZE] = matrix[j];
        }
        std::cout << std::endl;
    }
    std::cout << parts_matrix[0][0] << " " << parts_matrix[0][1] << std::endl << parts_matrix[1][0] << " " << parts_matrix[1][1]; // Check if it works
}

答案 2 :(得分:1)

以下是如何使用提升范围boost::slice功能对自定义块大小进行拆分(粗略切割,边角情况和输入验证)的演示(这里&#34;输出创建&#34;被呈现)

#include <iterator>
#include <iostream>
#include <boost/range/adaptor/sliced.hpp>
#include <boost/range/algorithm/copy.hpp>

using namespace std;
using namespace boost::adaptors;

template<typename T, size_t N>
void split(T (&input)[N], size_t block_size)
{
   for (size_t i(0); i <= N-block_size; i += block_size)
   {
       cout << "{ ";
       boost::copy(input | sliced(i, i+block_size),
           std::ostream_iterator<int>(std::cout, " "));
       cout << "}\n"; 
   }
}


int main()
{
    int A[] = {1, 2, 3, 4};
    split(A, 2); 
}

Demo

输出

  

{1 2}

     

{3 4}

如果我不想做输出怎么办

以下某些内容可能看起来更具可读性

template<typename T, size_t N>
void split(T (&input)[N], size_t block_size)
{
   for (size_t i(0); i <= N-block_size; i += block_size)
   {
       cout << "{ ";
       // do whatever with the i slice (again I'm showing output)
       for (auto k : (input | sliced(i, i+block_size))) cout << k << " "; 
       cout << "}\n"; 
   }
}