c++ - 从 float array 到 mat ,连接图像 block

标签 c++ opencv

我有一个 800x800 的图像,它被分解为 16 个 200x200 的 block 。

(可以看之前的帖子here)

这些 block 是:vector<Mat> subImages;

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

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

1) 现在,我希望能够再次获得相同的图像/ block ,从 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 Error: Assertion failed

在 imshow 中。

2) 如何重新组合所有 block 以获得原始 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) ); .

最佳答案

首先,您需要知道您的子图像在大图像中的位置。为此,您可以保存 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;
}

关于c++ - 从 float array 到 mat ,连接图像 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34134252/

相关文章:

c++ - 将多波段 tiff 图像加载到 OpenCV C++ 中

c++ - 通过从每一行中选择1个元素来查找2d数组中的最低和

c++ - async_connect 在 boost::asio 中阻塞 io_service::run_one()

c++ - 将带有 DST 的时间字符串转换为 UTC 时间戳

python - 如何使用python在opencv中对噪声图像执行中值滤波

c++ - 从二进制图像 OpenCV C++ 中裁剪文本

c++ - 将 cv::Mat 存储在字节数组中,以便将数据传输到服务器

c++ - std::next with n > std::distance(it, c.end())

c++ - 软件规划好用的软件?

linux - Gst-plugin-base 从源失败构建