#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/core/cuda.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <fstream>
#include <vpi/OpenCVInterop.hpp>
#include <vpi/Image.h>
#include <vpi/Stream.h>
#include <vpi/algo/ConvertImageFormat.h>
using namespace std;
using namespace cv;
Mat readRawImage(const string& filename, int width, int height) {
// Open the raw file in binary mode
FILE* file = fopen(filename.c_str(), "rb");
if (!file) {
cerr << "Error opening file: " << filename << endl;
return Mat();
}
// Calculate the size of the image
int imageSize = width * height;
// Read the raw data into a buffer
vector<uchar> buffer(imageSize);
fread(buffer.data(), sizeof(uchar), buffer.size(), file);
if (!file) {
cerr << "Error reading file: " << filename << endl;
return cv::Mat();
}
fclose(file);
// Create a Mat object from the buffer
// CV_8UC1 indicates an 8-bit single-channel image (grayscale)
Mat image(height, width, CV_8UC1);
memcpy(image.data, buffer.data(), buffer.size());
return image;
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
cerr << "Must pass an input image to be blurred" << endl;
return 1;
}
Mat cvImage = readRawImage(argv[1], 2048, 1216);
if (cvImage.empty()) {
cerr << "Failed to load image." << endl;
return -1;
}
VPIStatus status;
cout << "Width: " << cvImage.cols << endl;
cout << "Height: " << cvImage.rows << endl;
cout << "Channels: " << cvImage.channels() << endl;
VPIStream stream;
vpiStreamCreate(0, &stream);
VPIImage inputImage, outputImage;
Mat cvOut(cvImage.rows, cvImage.cols, CV_8UC3, Scalar(255, 255, 255));
VPIImageFormat inputImageFormat = VPI_MAKE_RAW_IMAGE_FORMAT(VPI_RAW_BAYER_RGGB, VPI_MEM_LAYOUT_PITCH_LINEAR,
VPI_DATA_TYPE_UNSIGNED, VPI_SWIZZLE_X000,
1, VPI_PACKING_X8);
//inputImageFormat = vpiImageFormatSetColorSpec(inputImageFormat, VPI_COLOR_SPEC_sRGB);
status = vpiImageCreate(cvImage.cols, cvImage.rows, inputImageFormat, VPI_BACKEND_CUDA, &inputImage);
cout << "VPI InputImage : " << status << endl;
status = vpiImageCreateWrapperOpenCVMat(cvImage, 0 , &inputImage);
cout << "Opencv Wrapper : " << status << endl;
status = vpiImageCreate(cvImage.cols, cvImage.rows, VPI_IMAGE_FORMAT_RGB8, 0, &outputImage);
cout << "Create output VPIImage status: " << status << endl;
status = vpiSubmitConvertImageFormat(stream, VPI_BACKEND_CUDA, inputImage, outputImage, NULL);
cout << "Conversion status: " << status << endl;
VPIImageFormat outputFormat;
status = vpiImageGetFormat(outputImage, &outputFormat);
cout << "Ouput format status: "<< status << endl;
vpiStreamSync(stream);
VPIImageData outputData;
status = vpiImageLockData(outputImage, VPI_LOCK_READ, VPI_IMAGE_BUFFER_HOST_PITCH_LINEAR, &outputData);
cout << "Output data status: " << status << endl;
status = vpiImageDataExportOpenCVMat(outputData, &cvOut);
cout << "CV Output status: " << status << endl;
//const VPIImageBufferPitchLinear &outPitch = outputData.buffer.pitch;
//Mat cvOut3(outPitch.planes[0].height, outPitch.planes[0].width, CV_8UC3, outPitch.planes[0].data,
// outPitch.planes[0].pitchBytes);
cout << "Width: " << cvOut.cols << endl;
cout << "Height: " << cvOut.rows << endl;
cout << "Channels: " << cvOut.channels() << endl;
vpiImageUnlock(outputImage);
imwrite("out.jpg", cvOut);
vpiImageDestroy(inputImage);
vpiImageDestroy(outputImage);
vpiStreamDestroy(stream);
return 0;
}
- Not able to generate RGB image with this code, please anyone suggest what is wrong with the code