C ++ / OpenCV Bicubic调整RGB图像的大小

时间:2018-04-13 13:15:37

标签: c++ opencv resize bicubic

我想用双立方插值调整RGB图像的大小。我使用了以下代码:

// Include standard headers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <ctime>

#include <iostream>
using namespace std;

//#include <opencv.hpp>
#include <opencv/cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv/highgui.h>
using namespace cv;

int main() 
{
    string fileName = "myImage.jpg";

    Mat imageSrc = cv::imread(fileName, CV_LOAD_IMAGE_UNCHANGED);   // Read the file
    if (!imageSrc.data) // Check for invalid input
    {
        cout << "Could not open or find the image\n";
        return 1;
    }
    cout << "Loaded " << fileName << " (" << imageSrc.channels() << " channels)\n";

    imageSrc.convertTo(imageSrc, CV_32F, 1 / 255.0, 0.0);

    int SliceSizeWidth = imageSrc.cols / 2;
    int sliceShiftWidth = imageSrc.cols / 4;
    int sliceWidthNumber = (imageSrc.cols / sliceShiftWidth) - 1;

    int SliceSizeHeight = imageSrc.rows / 2;
    int sliceShiftHeight = imageSrc.rows / 4;
    int sliceHeightNumber = (imageSrc.rows / sliceShiftHeight) - 1;


    for (int sliceIndexHeight = 0; sliceIndexHeight < sliceHeightNumber; sliceIndexHeight++)
    {
        for (int sliceIndexWidth = 0; sliceIndexWidth < sliceWidthNumber; sliceIndexWidth++)
        {
            Mat patchImage = imageSrc(Rect(sliceIndexWidth*sliceShiftWidth, sliceIndexHeight*sliceShiftHeight, SliceSizeWidth, SliceSizeHeight));

            Mat patchImageCopy;
            patchImage.copyTo(patchImageCopy); // Deep copy => data are contiguous in patchImageCopy

            Mat imageBicubic;

            resize(patchImageCopy, imageBicubic, Size(2 * patchImage.cols, 2 * patchImage.rows), INTER_CUBIC);
        }
     }
     return 0; 
}

我的问题是即使imageBicubic.channels() = 3,当我想要访问第二和第三个频道时,我也不能。

我使用了这一行:cout << imageBicubic.at<float>(25,25,1) << endl;以查看第二个频道是否已填满,而且似乎不是(我的图像的暗淡是756 * 1200 * 3)。 .exe刚停止工作。没有cout,.exe工作正常。

我是否需要在resize功能上添加一些东西才能在最后获得3D Mat?

感谢您的时间!

0 个答案:

没有答案