Last active
May 15, 2019 02:58
-
-
Save hewumars/9ab782a437aa5db897be0e166388f244 to your computer and use it in GitHub Desktop.
将blob转成Mat,然后保存成jpg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "opencv2/opencv.hpp" | |
#include "hi_ive.h" | |
static HI_VOID readChn(cv::Mat& dstMat, const HI_U8* pu8Data, HI_U8 u8Chn, HI_U32 u32Width, HI_U32 u32Height, HI_U32 u32Stride) | |
{ | |
HI_U32 w, h; | |
for (w = 0; w < u32Width; w++) | |
{ | |
for (h = 0; h < u32Height; h++) | |
{ | |
dstMat.at<cv::Vec3b>(h, w)[u8Chn] = pu8Data[w + h * u32Stride]; | |
} | |
} | |
} | |
static HI_VOID readSingleChn(cv::Mat& dstMat, const HI_U8* pu8Data, HI_U32 u32Width, HI_U32 u32Height, HI_U32 u32Stride) | |
{ | |
HI_U32 w, h; | |
for (w = 0; w < u32Width; w++) | |
{ | |
for (h = 0; h < u32Height; h++) | |
{ | |
dstMat.at<uchar>(h, w) = pu8Data[w + h * u32Stride]; | |
} | |
} | |
} | |
HI_VOID convertBlob2Mat(const HI_RUNTIME_BLOB_S* pstSrcBlob, HI_U16 u16Index, cv::Mat& dstMat) | |
{ | |
HI_U32 u32SrcWidth = pstSrcBlob->unShape.stWhc.u32Width; | |
HI_U32 u32SrcHeight = pstSrcBlob->unShape.stWhc.u32Height; | |
HI_U32 u32SrcStride = pstSrcBlob->u32Stride; | |
HI_U8* pu8Src = (HI_U8*)(pstSrcBlob->u64VirAddr + u16Index * u32SrcHeight * u32SrcStride * pstSrcBlob->unShape.stWhc.u32Chn); | |
HI_U8* pu8SrcB = pu8Src; | |
HI_U8* pu8SrcG = pu8SrcB + u32SrcStride * u32SrcHeight; | |
HI_U8* pu8SrcR = pu8SrcG + u32SrcStride * u32SrcHeight; | |
readChn(dstMat, pu8SrcB, 0, u32SrcWidth, u32SrcHeight, u32SrcStride); | |
readChn(dstMat, pu8SrcG, 1, u32SrcWidth, u32SrcHeight, u32SrcStride); | |
readChn(dstMat, pu8SrcR, 2, u32SrcWidth, u32SrcHeight, u32SrcStride); | |
} | |
HI_VOID convertMat2Blob(cv::Mat& srcMat, HI_RUNTIME_BLOB_S* pstBlob, HI_U16 u16Index) | |
{ | |
HI_U32 u32BlobChn = pstBlob->unShape.stWhc.u32Chn; | |
HI_U32 u32BlobWidth = pstBlob->unShape.stWhc.u32Width; | |
HI_U32 u32BlobHeight = pstBlob->unShape.stWhc.u32Height; | |
HI_U8* pu8DstAddr = (HI_U8*)(pstBlob->u64VirAddr + u16Index * u32BlobChn * u32BlobHeight * pstBlob->u32Stride); | |
for (HI_U32 c = 0; c < u32BlobChn; c++) | |
{ | |
for (HI_U32 h = 0; h < u32BlobHeight; h++) | |
{ | |
HI_U32 index = 0; | |
if (h >= (HI_U32)srcMat.rows) | |
{ | |
for (HI_U32 w = 0; w < u32BlobWidth; w++) | |
{ | |
pu8DstAddr[index++] = 0; | |
} | |
} | |
else | |
{ | |
for (HI_U32 w = 0; w < u32BlobWidth; w++) | |
{ | |
if (w >= (HI_U32)srcMat.cols) | |
{ | |
pu8DstAddr[index++] = 0; | |
} | |
else | |
{ | |
pu8DstAddr[index++] = srcMat.at<cv::Vec3b>(h, w)[c]; | |
} | |
} | |
} | |
pu8DstAddr += pstBlob->u32Stride; | |
} | |
} | |
} | |
extern "C" HI_VOID saveIveImage(HI_CHAR *pcFileName, IVE_IMAGE_S* pstImage) | |
{ | |
if (IVE_IMAGE_TYPE_U8C3_PLANAR == pstImage->enType) | |
{ | |
cv::Mat dstMat(pstImage->u32Height, pstImage->u32Width, CV_8UC3); | |
HI_U8* pu8SrcB = (HI_U8*)(HI_UL)pstImage->au64VirAddr[0]; | |
HI_U8* pu8SrcG = (HI_U8*)(HI_UL)pstImage->au64VirAddr[1]; | |
HI_U8* pu8SrcR = (HI_U8*)(HI_UL)pstImage->au64VirAddr[2]; | |
readChn(dstMat, pu8SrcB, 0, pstImage->u32Width, pstImage->u32Height, pstImage->au32Stride[0]); | |
readChn(dstMat, pu8SrcG, 1, pstImage->u32Width, pstImage->u32Height, pstImage->au32Stride[1]); | |
readChn(dstMat, pu8SrcR, 2, pstImage->u32Width, pstImage->u32Height, pstImage->au32Stride[2]); | |
imwrite(pcFileName, dstMat); | |
} | |
else if (IVE_IMAGE_TYPE_U8C1 == pstImage->enType) | |
{ | |
cv::Mat dstMat(pstImage->u32Height, pstImage->u32Width, CV_8UC1); | |
HI_U8* pu8Src = (HI_U8*)(HI_UL)pstImage->au64VirAddr[0]; | |
readSingleChn(dstMat, pu8Src, pstImage->u32Width, pstImage->u32Height, pstImage->au32Stride[0]); | |
imwrite(pcFileName, dstMat); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mat mat(cv::Size(pstConnectorSrc->pstBlobs[3].unShape.stWhc.u32Width,pstConnectorSrc->pstBlobs[3].unShape.stWhc.u32Height),CV_8UC3); | |
copyBlob2Mat(&pstConnectorSrc->pstBlobs[3],mat); | |
HI_S32 s32BboxNum=0; | |
for (int k = 0; k < u32ClassNum; ++k) { | |
s32BboxNum += ps32ClassRoiNum[k]; | |
for (int j = 0; j < ps32ClassRoiNum[k]; ++j) { | |
SAMPLE_LOG_PRINT("%d:%.5f,%d,%d,%d,%d\n",j,(HI_FLOAT)ps32DstScore[j]/4096,ps32DstRoi[4*j],ps32DstRoi[4*j+1],ps32DstRoi[4*j+2],ps32DstRoi[4*j+3]); | |
cv::rectangle(mat,cvPoint(ps32DstRoi[4*j],ps32DstRoi[4*j+1]),cvPoint(ps32DstRoi[4*j+2],ps32DstRoi[4*j+3]),cv::Scalar(255,0,0),1,1,0); | |
} | |
} | |
cv::imwrite("./nv21_result.jpg",mat); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment