从浮点数组到mat,连接图像块 [英] from float array to mat , concatenate blocks of image

查看:428
本文介绍了从浮点数组到mat,连接图像块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张800x800的图片,分割为200x200的16个区块。

I have an image 800x800 which is broken down to 16 blocks of 200x200.

(您可以查看上一篇文章此处

(you can see previous post here)

这些块是: vector< Mat> subImages;

我想使用float指针,所以我在做:

I want to use float pointers on them , so I am doing :

float *pdata = (float*)( subImages[ idxSubImage ].data );

1)现在,我想要能够再次获得相同的图像/数组到Mat数据。

1) Now, I want to be able to get again the same images/blocks, going from float array to Mat data.

int Idx = 0;
pdata = (float*)( subImages[ Idx ].data );
namedWindow( "Display window", WINDOW_AUTOSIZE );

for( int i = 0; i < OriginalImgSize.height - 4; i+= 200 )
{
    for( int j = 0; j < OriginalImgSize.width - 4; j+= 200, Idx++ )
    {

        Mat mf( i,j, CV_32F, pdata + 200 );
        imshow( "Display window", mf );          
        waitKey(0);

    }
}

我收到了


OpenCV错误:声明失败

OpenCV Error: Assertion failed

in imshow。

in imshow.

2)如何重新组合所有的块以获得原始的800x800图像?
我试过像:

2) How can I recombine all the blocks to obtain the original 800x800 image? I tried something like:

int Idx = 0;
pdata = (float*)( subImages[ Idx ].data );

Mat big( 800,800,CV_32F );    
for( int i = 0; i < OriginalImgSize.height - 4; i+= 200 )
{
   for( int j = 0; j < OriginalImgSize.width - 4; j+= 200, Idx++ )
   {

       Mat mf( i,j, CV_32F, pdata + 200 );
       Rect roi(j,i,200,200);
       mf.copyTo( big(roi) );


   }
}

imwrite( "testing" , big );

这给我:

OpenCV Error: Assertion failed (!fixedSize()) in release

mf.copyTo(big(roi));

推荐答案

,你需要知道你的子图像到哪里大的形象。为此,您可以将每个子图像的 rect 保存到向量< Rect> smallImageRois;

First, you need to know where are your subimages into the big image. To do this, you can save the rect of each subimage into the vector<Rect> smallImageRois;

然后你可以使用指针(记住子图像不连续),或者使用 copyTo 到正确的地方:

Then you can use pointers (keep in mind that subimages are not continuous), or simply use copyTo to the correct place:

查看:

#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;

int main()
{
    Mat3b img = imread("path_to_image");
    resize(img, img, Size(800, 800));

    Mat grayImg;
    cvtColor(img, grayImg, COLOR_BGR2GRAY);
    grayImg.convertTo(grayImg, CV_32F);

    int N = 4;

    if (((grayImg.rows % N) != 0) || ((grayImg.cols % N) != 0))
    {
        // Error
        return -1;
    }

    Size graySize = grayImg.size();
    Size smallSize(grayImg.cols / N, grayImg.rows / N);

    vector<Mat> smallImages;
    vector<Rect> smallImageRois;

    for (int i = 0; i < graySize.height; i += smallSize.height)
    {
        for (int j = 0; j < graySize.width; j += smallSize.width)
        {
            Rect rect = Rect(j, i, smallSize.width, smallSize.height);
            smallImages.push_back(grayImg(rect));
            smallImageRois.push_back(rect);
        }
    }

    // Option 1. Using pointer to subimage data.

    Mat big1(800, 800, CV_32F); 
    int big1step = big1.step1();

    float* pbig1 = big1.ptr<float>(0);

    for (int idx = 0; idx < smallImages.size(); ++idx)
    {
        float* pdata = (float*)smallImages[idx].data;
        int step = smallImages[idx].step1();
        Rect roi = smallImageRois[idx];

        for (int i = 0; i < smallSize.height; ++i)
        {
            for (int j = 0; j < smallSize.width; ++j)
            {
                pbig1[(roi.y + i) * big1step + (roi.x + j)] = pdata[i * step + j];
            }
        }
    }

    // Option 2. USing copyTo
    Mat big2(800, 800, CV_32F); 
    for (int idx = 0; idx < smallImages.size(); ++idx)
    {
        smallImages[idx].copyTo(big2(smallImageRois[idx]));
    }

    return 0;
}

这篇关于从浮点数组到mat,连接图像块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆