28 #include <opencv2/core/version.hpp>
29 #if CV_MAJOR_VERSION >= 3
30 # include <opencv2/imgcodecs.hpp>
32 # include <opencv2/contrib/contrib.hpp>
33 # include <opencv2/highgui/highgui.hpp>
36 #include <opencv2/imgproc/imgproc.hpp>
54 #define CHECK_STATUS(STMT) \
57 VPIStatus status = (STMT); \
58 if (status != VPI_SUCCESS) \
60 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
61 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
62 std::ostringstream ss; \
63 ss << "line " << __LINE__ << " " << vpiStatusGetName(status) << ": " << buffer; \
64 throw std::runtime_error(ss.str()); \
68 int main(
int argc,
char *argv[])
72 cv::Mat cvImageLeft, cvImageRight;
78 if (argc != 5 && argc != 6)
80 throw std::runtime_error(std::string(
"Usage: ") + argv[0] +
" " +
81 "<left_image> <right_image> <num_streams> <iterations> [save_outputs:true|false]");
84 std::string strLeftFileName = argv[1];
85 std::string strRightFileName = argv[2];
86 int numStreams = std::stoi(argv[3]);
87 int itersPerStream = std::stoi(argv[4]);
88 bool saveOutput =
false;
92 std::string arg5(argv[5]);
93 std::transform(arg5.begin(), arg5.end(), arg5.begin(), ::tolower);
94 saveOutput = (arg5 ==
"true");
97 if (numStreams <= 0 || itersPerStream <= 0)
99 throw std::runtime_error(
"Number of streams and iterations must be positive.");
104 cvImageLeft = cv::imread(strLeftFileName);
105 if (cvImageLeft.empty())
107 throw std::runtime_error(
"Can't open '" + strLeftFileName +
"'");
109 cvImageRight = cv::imread(strRightFileName);
110 if (cvImageRight.empty())
112 throw std::runtime_error(
"Can't open '" + strRightFileName +
"'");
117 constexpr
int MAX_DISPARITY_VALUE = 128;
118 constexpr
int STEREO_WINDOW_SIZE = 5;
122 int totalIterations = itersPerStream * numStreams;
123 std::vector<VPIImage> leftInputs(numStreams), rightInputs(numStreams), leftTmps(numStreams),
124 rightTmps(numStreams);
125 std::vector<VPIImage> leftOuts(numStreams), rightOuts(numStreams);
126 std::vector<VPIImage> disparities, confidences;
127 std::vector<VPIPayload> stereoPayloads(numStreams);
128 std::vector<VPIStream> streamsLeft(numStreams), streamsRight(numStreams);
129 std::vector<VPIEvent> events(numStreams);
130 int width = cvImageLeft.cols;
131 int height = cvImageLeft.rows;
144 for (
int i = 0; i < numStreams; i++)
155 &stereoPayloadParams, &stereoPayloads[i]));
158 int outCount = saveOutput ? (numStreams * itersPerStream) : numStreams;
159 disparities.resize(outCount);
160 confidences.resize(outCount);
161 for (
int i = 0; i < outCount; i++)
169 for (
int i = 0; i < numStreams; i++)
181 for (
int i = 0; i < numStreams; i++)
189 auto benchmarkStart = std::chrono::high_resolution_clock::now();
190 for (
int iter = 0; iter < itersPerStream; iter++)
192 for (
int i = 0; i < numStreams; i++)
194 int dispIdx = saveOutput ? (i * itersPerStream + iter) : i;
196 leftOuts[i], rightOuts[i], disparities[dispIdx],
197 confidences[dispIdx], &stereoParams));
202 for (
int i = 0; i < numStreams; i++)
206 auto benchmarkEnd = std::chrono::high_resolution_clock::now();
208 std::chrono::duration_cast<std::chrono::microseconds>(benchmarkEnd - benchmarkStart).count();
214 for (
int i = 0; i < numStreams * itersPerStream; i++)
217 cv::Mat cvDisparity, cvDisparityColor, cvConfidence, cvMask;
221 cvDisparity.convertTo(cvDisparity, CV_8UC1, 255.0 / (32 * stereoParams.
maxDisparity), 0);
222 applyColorMap(cvDisparity, cvDisparityColor, cv::COLORMAP_JET);
224 std::ostringstream fpStream;
225 fpStream <<
"stream_" << i / itersPerStream <<
"_iter_" << i % itersPerStream <<
"_disparity.png";
226 imwrite(fpStream.str(), cvDisparityColor);
232 cvConfidence.convertTo(cvConfidence, CV_8UC1, 255.0 / 65535.0, 0);
234 std::ostringstream fpStreamConf;
235 fpStreamConf <<
"stream_" << i / itersPerStream <<
"_iter_" << i % itersPerStream <<
"_confidence.png";
236 imwrite(fpStreamConf.str(), cvConfidence);
245 for (
int i = 0; i < numStreams; i++)
261 for (
int i = 0; i < (int)disparities.size(); i++)
265 for (
int i = 0; i < (int)confidences.size(); i++)
272 double totalTimeSeconds = totalTime / 1000000.0;
273 double avgTimePerFrame = totalTimeSeconds / totalIterations;
274 double throughputFPS = totalIterations / totalTimeSeconds;
276 std::cout <<
"\n" << std::string(70,
'=') << std::endl;
277 std::cout <<
"SIMPLE MULTI-STREAM RESULTS" << std::endl;
278 std::cout << std::string(70,
'=') << std::endl;
279 std::cout <<
"Total time: " << totalTimeSeconds <<
" seconds" << std::endl;
280 std::cout <<
"Avg time per frame: " << (avgTimePerFrame * 1000) <<
" ms" << std::endl;
281 std::cout <<
"THROUGHPUT: " << throughputFPS <<
" FPS" << std::endl;
282 std::cout << std::string(70,
'=') << std::endl;
286 catch (std::exception &e)
288 std::cerr << e.what() << std::endl;
Functions and structures for dealing with VPI events.
Functions and structures for dealing with VPI images.
Functions for handling OpenCV interoperability with VPI.
Declares functions that implement the Rescale algorithm.
Declaration of VPI status codes handling functions.
Declares functions that implement stereo disparity estimation algorithms.
Declares functions dealing with VPI streams.
VPIStatus vpiEventRecord(VPIEvent event, VPIStream stream)
Captures in the event the contents of the stream command queue at the time of this call.
VPIStatus vpiEventCreate(uint64_t flags, VPIEvent *event)
Create an event instance.
void vpiEventDestroy(VPIEvent event)
Destroy an event instance as well as all resources it owns.
void vpiImageDestroy(VPIImage img)
Destroy an image instance.
VPIStatus vpiImageLockData(VPIImage img, VPILockMode mode, VPIImageBufferType bufType, VPIImageData *data)
Acquires the lock on an image object and returns the image contents.
VPIStatus vpiImageCreate(int32_t width, int32_t height, VPIImageFormat fmt, uint64_t flags, VPIImage *img)
Create an empty image instance with the specified flags.
VPIStatus vpiImageUnlock(VPIImage img)
Releases the lock on an image object.
@ VPI_IMAGE_BUFFER_HOST_PITCH_LINEAR
Host-accessible with planes in pitch-linear memory layout.
Stores information about image characteristics and content.
VPIStatus vpiImageCreateWrapperOpenCVMat(const cv::Mat &mat, VPIImageFormat fmt, uint64_t flags, VPIImage *img)
Wraps a cv::Mat in an VPIImage with the given image format.
VPIStatus vpiImageDataExportOpenCVMat(const VPIImageData &imgData, cv::Mat *mat)
Fills an existing cv::Mat with data from VPIImageData coming from a locked VPIImage.
void vpiPayloadDestroy(VPIPayload payload)
Deallocates the payload object and all associated resources.
int32_t maxDisparity
Maximum disparity for matching search.
VPIStereoDisparityConfidenceType confidenceType
Computation type to produce the confidence output.
int32_t windowSize
Represents the median filter size on OFA+PVA+VIC backend or census transform window size (other backe...
int32_t maxDisparity
Maximum disparity for matching search.
VPIStatus vpiInitStereoDisparityEstimatorCreationParams(VPIStereoDisparityEstimatorCreationParams *params)
Initializes VPIStereoDisparityEstimatorCreationParams with default values.
VPIStatus vpiCreateStereoDisparityEstimator(uint64_t backends, int32_t imageWidth, int32_t imageHeight, VPIImageFormat inputFormat, const VPIStereoDisparityEstimatorCreationParams *params, VPIPayload *payload)
Creates payload for vpiSubmitStereoDisparityEstimator.
VPIStatus vpiInitStereoDisparityEstimatorParams(VPIStereoDisparityEstimatorParams *params)
Initializes VPIStereoDisparityEstimatorParams with default values.
VPIStatus vpiSubmitStereoDisparityEstimator(VPIStream stream, uint64_t backend, VPIPayload payload, VPIImage left, VPIImage right, VPIImage disparity, VPIImage confidenceMap, const VPIStereoDisparityEstimatorParams *params)
Runs stereo processing on a pair of images and outputs a disparity map.
@ VPI_STEREO_CONFIDENCE_RELATIVE
The U16 confidence value of a pixel is given by: [ 1 - abs(D_lr - D_rl) / D_lr ] * 0xFFFF.
Structure that defines the parameters for vpiCreateStereoDisparityEstimator.
Structure that defines the parameters for vpiSubmitStereoDisparityEstimator.
VPIStatus vpiStreamWaitEvent(VPIStream stream, VPIEvent event)
Pushes a command that blocks the processing of all future commands submitted to the stream until the ...
VPIStatus vpiStreamSync(VPIStream stream)
Blocks the calling thread until all submitted commands in this stream queue are done (queue is empty)...
void vpiStreamDestroy(VPIStream stream)
Destroy a stream instance and deallocate all HW resources.
VPIStatus vpiStreamCreate(uint64_t flags, VPIStream *stream)
Create a stream instance.
@ VPI_BACKEND_PVA
PVA backend.
@ VPI_BACKEND_OFA
OFA backend.
@ VPI_BACKEND_VIC
VIC backend.
@ VPI_BACKEND_CPU
CPU backend.
@ VPI_LOCK_READ
Lock memory only for reading.