Opencv merge gives errors

Hi

I am using opencv merge function to merge 3 bands and got following error :
Exception thrown at 0x00007FFD6309441C in project2.exe: Microsoft C++ exception: osgeo::proj::io::FactoryException at memory location 0x00000075F16FC190.

Stackt trace is as follows :slight_smile:
| |opencv_core460d.dll!icv_l9_owniCopy_8u_P3C3R()|Unknown|
| |opencv_core460d.dll!icv_l9_ippiCopy_8u_P3C3R()|Unknown|
| |opencv_core460d.dll!llwiCopyMerge(const void * const * pSrc, int srcStep, void * pDst, int dstStep, IppiSize size, int typeSize, int channels, int partial) Line 160|C|
| |opencv_core460d.dll!cv::ipp_merge(const cv::Mat * mv, cv::Mat & dst, int channels) Line 86|C++|
|>|opencv_core460d.dll!cv::merge(const cv::Mat * mv, unsigned __int64 n, const cv::debug_build_guard::_OutputArray & _dst) Line 144|C++|
| |opencv_core460d.dll!cv::merge(const cv::debug_build_guard::_InputArray & _mv, const cv::debug_build_guard::_OutputArray & _dst) Line 269|C++|
| |project2.exe!main() Line 137|C++|

Eception comes from this line executing :
case 3: return ippiCopy_8u_P3C3R((const Ipp8u**)pSrc, srcStep, (Ipp8u*)pDst, dstStep, size);

What I have to do ?

Regards
Raivo

please show, how you call this (code)

I use Geotiff and read it to Opencv with GDAL,

// Read the image data using GDAL
std::vectorcv::Mat bands(num_bands);

for (int i = 0; i < num_bands; ++i)
{
    GDALRasterBand* band = dataset->GetRasterBand(i + 1);
    int width = band->GetXSize();
    int height = band->GetYSize();

    GDALDataType data_type = band->GetRasterDataType(); // Get data type of input image

    std::cout << "width = " << width << ", height = " << height << std::endl;
    std::cout << "Input data type: " << GDALGetDataTypeName(data_type) << std::endl;

    std::vector<uint16_t> buffer(width * height);
    band->RasterIO(GF_Read, 0, 0, width, height, &buffer[0], width, height, GDT_UInt16, 0, 0);
    // Create a cv::Mat object from the buffer
    if (data_type == GDT_Byte) {
        bands[i] = Mat(height, width, CV_8UC1, &buffer[0]);
    }
    else if (data_type == GDT_UInt16) {
        bands[i] = Mat(height, width, CV_16UC1, &buffer[0]);
    }
    else if (data_type == GDT_Float32) {
        bands[i] = Mat(height, width, CV_32FC1, &buffer[0]);
    }
    else if (data_type == GDT_Float64) {
        bands[i] = Mat(height, width, CV_64FC1, &buffer[0]);
    }
    else {
        std::cerr << "Unsupported data type: " << GDALGetDataTypeName(data_type) << std::endl;
        return 1;
    }

    std::cout << "Rows: " << bands[i].rows << std::endl;
    std::cout << "Cols: " << bands[i].cols << std::endl;
    std::cout << "Channels: " << bands[i].channels() << std::endl;

    // Print the data type of the image
    std::cout << "Data Type: " << bands[i].type() << std::endl;

    std::cout << "Press any key to continue..." << std::endl;
    getch(); // wait for key press
    std::cout << "Key pressed!" << std::endl;
    
    for (int x = 0; x < 10; x++) {
        for (int y = 0; y < 10; y++) {
            uchar pixel_value = bands[i].at<uchar>(y, x);
            cout << " - (" << x << ", " << y << "): " << (int)pixel_value;
        }
        cout << endl;
    }

    std::cout << "Press any key to continue..." << std::endl;
    getch(); // wait for key press
    std::cout << "Key pressed!" << std::endl;
}

Mat color_image;
vector<Mat> channels{ bands[2],  bands[1],  bands[0] };
merge(channels, color_image);

imshow("Color Image", color_image);
waitKey(0);

this Mat has only valid pixels, as long as the buffer object is valid.
(as soon as you leave the โ€˜forโ€™ loop, those bands will point to invalid (temporary) memory !)

solution: you need to clone() those Matโ€™s (so it allocates & copies actual pixels):

bands[i] = Mat(height, width, CV_8UC1, &buffer[0]).clone();

also, please make sure, this is a valid access (i+1 ???):

dataset->GetRasterBand(i + 1);

I made cahanges into program source, but now is problems with GDAL.
I receive the following error :slight_smile:

Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol public: virtual class OGRGeometry * __cdecl OGRGeometry::MakeValid(void)const (?MakeValid@OGRGeometry@@UEBAPEAV1@XZ) project2 C:\Users\Raivo\source\Repos\cv46srcDD\project2\Source.obj 1
Openai chat said to me use gdal_i library, but it does not exist in my compure also after I reinstalled GDAL.
Can You help me ?