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));
173 cv::Mat mag = InplaceFFTShift(LogMagnitude(CompleteFullHermitian(cvSpectrum, cvImage.size())));
176 normalize(mag, mag, 0, 255, cv::NORM_MINMAX);
179 imwrite(
"spectrum_" + strBackend +
".png", mag);
184 catch (std::exception &e)
186 std::cerr << e.what() << std::endl;
213 cv::Mat LogMagnitude(cv::Mat cpx)
217 assert(cpx.channels() == 2);
222 magnitude(reim[0], reim[1], mag);
225 mag += cv::Scalar::all(1);
227 mag = mag(cv::Rect(0, 0, mag.cols & -2, mag.rows & -2));
232 cv::Mat CompleteFullHermitian(cv::Mat in, cv::Size fullSize)
234 assert(in.type() == CV_32FC2);
236 cv::Mat out(fullSize, CV_32FC2);
237 for (
int i = 0; i < out.rows; ++i)
239 for (
int j = 0; j < out.cols; ++j)
244 p = in.at<cv::Vec2f>(i, j);
248 p = in.at<cv::Vec2f>((out.rows - i) % out.rows, (out.cols - j) % out.cols);
251 out.at<cv::Vec2f>(i, j) = p;
258 cv::Mat InplaceFFTShift(cv::Mat mag)
264 int cx = mag.cols / 2;
265 int cy = mag.rows / 2;
266 cv::Mat qTL(mag, cv::Rect(0, 0, cx, cy));
267 cv::Mat qTR(mag, cv::Rect(cx, 0, cx, cy));
268 cv::Mat qBL(mag, cv::Rect(0, cy, cx, cy));
269 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(uint64_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, uint64_t backend, VPIPayload payload, VPIImage input, VPIImage output, uint64_t flags)
Runs the direct Fast Fourier Transform on single image.
VPIImageBuffer buffer
Stores the image contents.
VPIImagePlanePitchLinear planes[VPI_MAX_PLANE_COUNT]
Data of all image planes in pitch-linear layout.
VPIImageBufferPitchLinear pitch
Image stored in pitch-linear layout.
void * data
Pointer to the first row of this plane.
VPIImageFormat format
Image format.
VPIImageBufferType bufferType
Type of image buffer.
int32_t height
Height of this plane in pixels.
int32_t width
Width of this plane in pixels.
int32_t pitchBytes
Difference in bytes of beginning of one row and the beginning of the previous.
void vpiImageDestroy(VPIImage img)
Destroy an image instance.
struct VPIImageImpl * VPIImage
A handle to an image.
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 the image plane contents.
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.
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(uint64_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.