| 75 | } |
| 76 | |
| 77 | Matrix OpOutputToCvMat::formatToCvMat(const Array<float>& outputData) |
| 78 | { |
| 79 | try |
| 80 | { |
| 81 | // Sanity check |
| 82 | if (outputData.empty()) |
| 83 | error("Wrong input element (empty outputData).", __LINE__, __FUNCTION__, __FILE__); |
| 84 | // Final result |
| 85 | cv::Mat cvMat; |
| 86 | // CPU version |
| 87 | if (!mGpuResize) |
| 88 | { |
| 89 | // outputData to cvMat |
| 90 | // Equivalent: outputData.getConstCvMat().convertTo(cvMat, CV_8UC3); |
| 91 | const cv::Mat constCvMat = OP_OP2CVCONSTMAT(outputData.getConstCvMat()); |
| 92 | constCvMat.convertTo(cvMat, CV_8UC3); |
| 93 | } |
| 94 | // CUDA version |
| 95 | else |
| 96 | { |
| 97 | #ifdef USE_CUDA |
| 98 | // (Free and re-)Allocate temporary memory |
| 99 | if (mOutputMaxSizeUChar < *spOutputMaxSize) |
| 100 | { |
| 101 | mOutputMaxSizeUChar = *spOutputMaxSize; |
| 102 | cudaFree(pOutputImageUCharCuda); |
| 103 | cudaMalloc((void**)&pOutputImageUCharCuda, sizeof(unsigned char) * mOutputMaxSizeUChar); |
| 104 | } |
| 105 | // Float ptr --> unsigned char ptr |
| 106 | const auto volume = (int)outputData.getVolume(); |
| 107 | uCharImageCast(pOutputImageUCharCuda, *spOutputImageFloatCuda, volume); |
| 108 | // Allocate cvMat |
| 109 | cvMat = cv::Mat(outputData.getSize(0), outputData.getSize(1), CV_8UC3); |
| 110 | // CUDA --> CPU: Copy output image back to CPU |
| 111 | cudaMemcpy( |
| 112 | cvMat.data, pOutputImageUCharCuda, sizeof(unsigned char) * volume, |
| 113 | cudaMemcpyDeviceToHost); |
| 114 | // Indicate memory was copied out |
| 115 | *spGpuMemoryAllocated = false; |
| 116 | #else |
| 117 | error("You need to compile OpenPose with CUDA support in order to use GPU resize.", |
| 118 | __LINE__, __FUNCTION__, __FILE__); |
| 119 | #endif |
| 120 | } |
| 121 | // Return cvMat |
| 122 | const Matrix opMat = OP_CV2OPMAT(cvMat); |
| 123 | return opMat; |
| 124 | } |
| 125 | catch (const std::exception& e) |
| 126 | { |
| 127 | error(e.what(), __LINE__, __FUNCTION__, __FILE__); |
| 128 | return Matrix(); |
| 129 | } |
| 130 | } |
| 131 | } |