从float数组到mat,连接图像块

时间:2015-12-07 13:03:01

标签: c++ opencv

我的图像是800x800,可以分解为16个200x200的块。

(您可以看到上一篇文章here

这些块是:vector<Mat> subImages;

我想在它们上使用浮点指针,所以我在做:

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

1)现在,我希望能够再次获得相同的图像/块,从float数组到Mat数据。

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错误:断言失败

in imshow。

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

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) );中的

3 个答案:

答案 0 :(得分:1)

要将子图像连接成单个平方图像,可以使用以下函数:

// Important: all patches should have exactly the same size
Mat concatPatches(vector<Mat> &patches) {
    assert(patches.size() > 0);

    // make it square
    const int patch_width   = patches[0].cols;
    const int patch_height  = patches[0].rows;
    const int patch_stride  = ceil(sqrt(patches.size()));
    Mat image               = Mat::zeros(patch_stride * patch_height, patch_stride * patch_width, patches[0].type());

    for (size_t i = 0, iend = patches.size(); i < iend; i++) {
        Mat &patch = patches[i];
        const int offset_x = (i % patch_stride) * patch_width;
        const int offset_y = (i / patch_stride) * patch_height;

        // copy the patch to the output image
        patch.copyTo(image(Rect(offset_x, offset_y, patch_width, patch_height)));
    }

    return image;
}

它需要一个子图像矢量(或者我引用它们的补丁)并将它们连接成一个平方图像。用法示例:

vector<Mat> patches;
vector<Scalar> colours = {Scalar(255, 0, 0), Scalar(0, 255, 0), Scalar(0, 0, 255)};
// fill vector with circles of different colours
for(int i = 0; i < 16; i++) {
    Mat patch = Mat::zeros(100,100, CV_32FC3);
    circle(patch, Point(50,50), 40, colours[i % 3], -1);

    patches.push_back(patch);
}

Mat img = concatPatches(patches);
imshow("img", img);
waitKey();

将生成以下图像

Concatenating image blocks

答案 1 :(得分:1)

首先,您需要知道您的子图像在大图像中的位置。为此,您可以将每个子图像的rect保存到vector<Rect> smallImageRois;

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

看看:

#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;
}

答案 2 :(得分:0)

在创建i之前打印jMat mf的值,相信您很快就能找到错误。

提示1:ij第一次为0

提示2:使用copyTo()和ROI,如:

cv::Rect roi(0,0,200,200);
src.copyTo(dst(roi))

修改

提示3:尽量不要做这样的指针摆弄,你会遇到麻烦。特别是如果你忽略了这一步(就像你似乎那样)。