I used GDAL to read the original image and then converted it into a one-dimensional array, but there was an error when I used OpencV to convert the one-dimensional array into an image, which was always displayed as a 3-channel gray image (the original image became nine small images pieced together), as shown in the following picture.
Sorry for my late reply.
Thank you for your point, the correct picture is indeed the second picture as you said.
I solved this problem because the three-band data read by GDAL cannot be directly converted to Mat, but it needs to be read one band by one band. The complete code is as follows:
cv::Mat gdalToMat(GDALDataset* poRasterDS, int patchSizeX, int patchSizeY, int patchULX, int patchULY)
{
std::vector<cv::Mat> imgMat;
GByte* pafscan = new GByte[patchSizeX * patchSizeY];
int nBand = poRasterDS->GetRasterCount();
for (int band = 0; band < nBand; band++)
{
GDALRasterBand* pBand = poRasterDS->GetRasterBand(band + 1);
pBand->RasterIO(GF_Read, patchULX, patchULY, patchSizeX, patchSizeY, pafscan, patchSizeX, patchSizeY, GDT_Byte, 0, 0);
cv::Mat img = cv::Mat(patchSizeY, patchSizeX, CV_8UC1, pafscan);
imgMat.push_back(img.clone());
//delete[] pBand;
img.release();
}
delete[] pafscan;
pafscan = NULL;
cv::Mat imgPatch;
imgPatch.create(patchSizeY, patchSizeX, CV_8UC(nBand));
merge(imgMat, imgPatch);
imgMat.clear();
cv::Mat imgPatchRGB;
cv::cvtColor(imgPatch, imgPatchRGB, cv::COLOR_BGR2RGB);
return imgPatchRGB;
}