Ask Your Question
2

Combining multiple blocks into one image

asked 2014-08-13 14:16:24 -0500

Iona gravatar image

Hello,

I had 256x256 image which I successfully split into 32x32 blocks. So in total I have 64 blocks. I did some operations on these blocks obtained. And now I want to combine these 64 32x32 blocks into 256x256 image. Here is the code I tried but in vain. Any help would be appreciated.

//combine blocks
for  ( int y= 0; y<256; y+=smallBlock.height )
{
    for  ( int x= 0 ; x<256; x+=smallBlock.width )
    {
        cv::Rect rcn=cv::Rect(x,y,smallBlock.width,smallBlock.height);
        smallBlock.copyTo(rcn);
    }
}

Here smallBlock has height 32 and width 32.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
6

answered 2014-08-13 16:59:20 -0500

Guanta gravatar image

Your solution is nearly right, you just need to slice the correct region of interest of your output matrix, i.e. sth like this:

cv::Size sb_size(32,32);
std::vector<cv::Mat> small_blocks;

// ... do your block manipulations ...
// ... and save them to small_blocks ...

// now create combined image of all blocks
cv::Mat combined(256, 256, small_blocks[0].type());

for( size_t i = 0; i < small_blocks.size(); i++ ) 
{
  for  ( int y = 0; y < 256; y += sb_size.height )
  {
    for  ( int  x= 0 ; x < 256; x += sb_size.width )
    {
        // get the correct slice
        cv::Mat roi = combined(cv::Rect(x,y,sb_size.width,sb_size.height));
        small_blocks[i].copyTo(roi);
    }
  }
}
edit flag offensive delete link more

Comments

Thanks! Just two questions:

  1. namedWindow("combined image",CV_WINDOW_AUTOSIZE); will be placed before the first for loop, right?

  2. imshow("combined image", roi); will be placed immediately after the last line (small_blocks[i].copyTo(roi);) or after the last closing flower bracket?

Iona gravatar imageIona ( 2014-08-13 19:36:11 -0500 )edit

I'd just call imshow("combined image", combined); after all the loops (and don't forget waitKey(); after that ).

Guanta gravatar imageGuanta ( 2014-08-15 03:44:53 -0500 )edit

Hi i used ur code but my combined image alwayse shows the same block all over it not each block at his place

Oussama gravatar imageOussama ( 2019-10-06 19:24:22 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-08-13 14:16:24 -0500

Seen: 4,343 times

Last updated: Aug 13 '14