29 #include <opencv2/core/version.hpp>
30 #include <opencv2/imgproc/imgproc.hpp>
31 #if CV_MAJOR_VERSION >= 3
32 # include <opencv2/imgcodecs.hpp>
34 # include <opencv2/highgui/highgui.hpp>
49 #define CHECK_STATUS(STMT) \
52 VPIStatus status = (STMT); \
53 if (status != VPI_SUCCESS) \
55 char buffer[VPI_MAX_STATUS_MESSAGE_LENGTH]; \
56 vpiGetLastStatusMessage(buffer, sizeof(buffer)); \
57 std::ostringstream ss; \
58 ss << vpiStatusGetName(status) << ": " << buffer; \
59 throw std::runtime_error(ss.str()); \
64 cv::Mat LogMagnitude(cv::Mat cpx);
65 cv::Mat CompleteFullHermitian(cv::Mat in, cv::Size fullSize);
66 cv::Mat InplaceFFTShift(cv::Mat mag);
68 int main(
int argc,
char *argv[])
90 throw std::runtime_error(std::string(
"Usage: ") + argv[0] +
" <cpu|cuda> <input image>");
93 std::string strBackend = argv[1];
94 std::string strInputFileName = argv[2];
99 if (strBackend ==
"cpu")
103 else if (strBackend ==
"cuda")
109 throw std::runtime_error(
"Backend '" + strBackend +
"' not recognized, it must be either cpu or cuda.");
115 cvImage = cv::imread(strInputFileName);
118 throw std::runtime_error(
"Can't open '" + strInputFileName +
"'");
151 CHECK_STATUS(
vpiSubmitFFT(stream, backend, fft, imageF32, spectrum, 0));
170 cv::Mat mag = InplaceFFTShift(LogMagnitude(CompleteFullHermitian(cvSpectrum, cvImage.size())));
173 normalize(mag, mag, 0, 255, cv::NORM_MINMAX);
176 imwrite(
"spectrum_" + strBackend +
".png", mag);
181 catch (std::exception &e)
183 std::cerr << e.what() << std::endl;
210 cv::Mat LogMagnitude(cv::Mat cpx)
214 assert(cpx.channels() == 2);
219 magnitude(reim[0], reim[1], mag);
222 mag += cv::Scalar::all(1);
224 mag = mag(cv::Rect(0, 0, mag.cols & -2, mag.rows & -2));
229 cv::Mat CompleteFullHermitian(cv::Mat in, cv::Size fullSize)
231 assert(in.type() == CV_32FC2);
233 cv::Mat out(fullSize, CV_32FC2);
234 for (
int i = 0; i < out.rows; ++i)
236 for (
int j = 0; j < out.cols; ++j)
241 p = in.at<cv::Vec2f>(i, j);
245 p = in.at<cv::Vec2f>((out.rows - i) % out.rows, (out.cols - j) % out.cols);
248 out.at<cv::Vec2f>(i, j) = p;
255 cv::Mat InplaceFFTShift(cv::Mat mag)
261 int cx = mag.cols / 2;
262 int cy = mag.rows / 2;
263 cv::Mat qTL(mag, cv::Rect(0, 0, cx, cy));
264 cv::Mat qTR(mag, cv::Rect(cx, 0, cx, cy));
265 cv::Mat qBL(mag, cv::Rect(0, cy, cx, cy));
266 cv::Mat qBR(mag, cv::Rect(cx, cy, cx, cy));
Declares functions that implement the Fast Fourier Transform algorithm and its inverse.
Functions and structures for dealing with VPI images.
Functions for handling OpenCV interoperability with VPI.
Declaration of VPI status codes handling functions.
Declares functions dealing with VPI streams.
VPIStatus vpiCreateFFT(uint32_t backends, int32_t inputWidth, int32_t inputHeight, const VPIImageFormat inFormat, const VPIImageFormat outFormat, VPIPayload *payload)
Creates payload for direct Fast Fourier Transform algorithm.
VPIStatus vpiSubmitFFT(VPIStream stream, uint32_t backend, VPIPayload payload, VPIImage input, VPIImage output, uint32_t flags)
Runs the direct Fast Fourier Transform on single image.
int32_t height
Height of this plane in pixels.
int32_t width
Width of this plane in pixels.
void * data
Pointer to the first row of this plane.
int32_t pitchBytes
Difference in bytes of beginning of one row and the beginning of the previous.
VPIImagePlane planes[VPI_MAX_PLANE_COUNT]
Data of all image planes.
VPIImageFormat format
Image format.
VPIStatus vpiImageLock(VPIImage img, VPILockMode mode, VPIImageData *hostData)
Acquires the lock on an image object and returns a pointer to the image planes.
void vpiImageDestroy(VPIImage img)
Destroy an image instance.
struct VPIImageImpl * VPIImage
A handle to an image.
VPIStatus vpiImageCreate(int32_t width, int32_t height, VPIImageFormat fmt, uint32_t flags, VPIImage *img)
Create an empty image instance with the specified flags.
VPIStatus vpiImageUnlock(VPIImage img)
Releases the lock on an image object.
Stores information about image characteristics and content.
VPIStatus vpiImageCreateOpenCVMatWrapper(const cv::Mat &mat, VPIImageFormat fmt, uint32_t flags, VPIImage *img)
Wraps a cv::Mat in an VPIImage with the given image format.
struct VPIPayloadImpl * VPIPayload
A handle to an algorithm payload.
struct VPIStreamImpl * VPIStream
A handle to a stream.
VPIStatus vpiStreamSync(VPIStream stream)
Blocks the calling thread until all submitted commands in this stream queue are done (queue is empty)...
VPIBackend
VPI Backend types.
void vpiStreamDestroy(VPIStream stream)
Destroy a stream instance and deallocate all HW resources.
VPIStatus vpiStreamCreate(uint32_t flags, VPIStream *stream)
Create a stream instance.
@ VPI_BACKEND_CUDA
CUDA backend.
@ VPI_BACKEND_CPU
CPU backend.
@ VPI_LOCK_READ
Lock memory only for reading.