当前位置:网站首页>Imdecode source code interpretation
Imdecode source code interpretation
2022-04-22 05:47:00 【xkxsxkx】
Connected to a imencode Source code interpretation , Simply write a code comment
static bool
imdecode_( const Mat& buf, int flags, Mat& mat )
{
// Whether the picture is empty
CV_Assert(!buf.empty());
// Whether the incoming pictures are continuous , Here's a concept , The continuity of the image
/* isContinue You can check whether the picture is continuous in memory , Such as through rect tailoring , Although the returned image seems to have been cropped in form , But like a deep copy , The problem of shallow copy , The clipping in the above way is only a shallow copy , Not continuous in memory */
CV_Assert(buf.isContinuous());
/* Check this Mat Is it Vector, Used to confirm whether the incoming data format is correct . */
CV_Assert(buf.checkVector(1, CV_8U) > 0);
/* Convert to single line ,reshape Logically change the number of channels and rows of the matrix , But there is actually no data replication , And data increase and decrease */
Mat buf_row = buf.reshape(1, 1); // decoders expects single row, avoid issues with vector columns
String filename;
// It's also a factory model , Find the decoder
ImageDecoder decoder = findDecoder(buf_row);
if( !decoder )
// No suitable decoder ,
return 0;
// Check whether the decoder supports in memory decoding
// If not , You need to create a temporary file to store the decoded file
if( !decoder->setSource(buf_row) )
{
// Decoding in memory is not supported
// Create temporary file
filename = tempfile();
// Write binary
FILE* f = fopen( filename.c_str(), "wb" );
// Whether the file was successfully opened
if( !f )
return 0;
// Returns the total number of elements of the matrix * The data size of each element in the matrix
size_t bufSize = buf_row.total()*buf.elemSize();
// Write picture cache
if (fwrite(buf_row.ptr(), 1, bufSize, f) != bufSize)
{
// Write failure , Close file
fclose( f );
CV_Error( Error::StsError, "failed to write image data to temporary file" );
}
// After writing , Close file
if( fclose(f) != 0 )
{
// Failed to close file
CV_Error( Error::StsError, "failed to write image data to temporary file" );
}
// Set decoding source file
decoder->setSource(filename);
}
// Whether decoding is successful flag bit
bool success = false;
try
{
// Read file header
if (decoder->readHeader())
success = true;
}
catch (const cv::Exception& e)
{
std::cerr << "imdecode_('" << filename << "'): can't read header: " << e.what() << std::endl << std::flush;
}
catch (...)
{
std::cerr << "imdecode_('" << filename << "'): can't read header: unknown exception" << std::endl << std::flush;
}
// Decoding failed ,
if (!success)
{
// Release decoder
decoder.release();
// Check if the file is empty
if (!filename.empty())
{
// Delete file
if (0 != remove(filename.c_str()))
{
std::cerr << "unable to remove temporary file:" << filename << std::endl << std::flush;
}
}
return 0;
}
// established the required input image size
// Check if it is a valid picture size
Size size = validateInputImageSize(Size(decoder->width(), decoder->height()));
// Get picture type
int type = decoder->type();
if( (flags & IMREAD_LOAD_GDAL) != IMREAD_LOAD_GDAL && flags != IMREAD_UNCHANGED )
{
if( (flags & IMREAD_ANYDEPTH) == 0 )
type = CV_MAKETYPE(CV_8U, CV_MAT_CN(type));
if( (flags & IMREAD_COLOR) != 0 ||
((flags & IMREAD_ANYCOLOR) != 0 && CV_MAT_CN(type) > 1) )
type = CV_MAKETYPE(CV_MAT_DEPTH(type), 3);
else
type = CV_MAKETYPE(CV_MAT_DEPTH(type), 1);
}
// Create an empty picture
mat.create( size.height, size.width, type );
success = false;
try
{
// Write the decoded file into the blank picture
if (decoder->readData(mat))
success = true;
}
catch (const cv::Exception& e)
{
std::cerr << "imdecode_('" << filename << "'): can't read data: " << e.what() << std::endl << std::flush;
}
catch (...)
{
std::cerr << "imdecode_('" << filename << "'): can't read data: unknown exception" << std::endl << std::flush;
}
// Release decoder
decoder.release();
// Does the file exist
if (!filename.empty())
{
// Delete file
if (0 != remove(filename.c_str()))
{
std::cerr << "unable to remove temporary file:" << filename << std::endl << std::flush;
}
}
if (!success)
{
// Decoding failed , Release picture
mat.release();
return false;
}
return true;
}
版权声明
本文为[xkxsxkx]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220535349308.html
边栏推荐
- 1.计算a+b
- torch 循环神经网络torch.nn.RNN()和 torch.nn.RNNCell()
- LeetCode 2049. 统计最高分的节点数目--树的遍历
- Error Putty X11 proxy: Authorisation not recognised
- Goodbye 20202021 I'm coming
- excel的相对引用和绝对引用
- 數據挖掘——聚類
- Redis setting and obtaining expiration time
- 记录一次项目经历和项目中遇到的技术
- Random string tool class randomstringutils detailed explanation
猜你喜欢

LeetCode 2044. 统计按位或能得到最大值的子集数目 --深度遍历

raspberry keras-ocr can‘t allocate memory in static TLS block

《PyTorch深度学习实践》Lecture_10 卷积神经网络基础 CNN

数据挖掘——逻辑回归

LeetCode 589. N 叉树的前序遍历

raspberry keras-ocr can‘t allocate memory in static TLS block

LeetCode 514. 自由之路--动态规划

AcWing 836. 合并集合(并查集)

提高工作效率的利器

Goodbye 20202021 I'm coming
随机推荐
矩阵乘法实现
JVM探究
LeetCode 1591. 奇怪的打印机 II --判断排序
为什么要引入协程
Error Putty X11 proxy: Authorisation not recognised
二分类任务为什么常见用softmax而不是sigmoid
雷达设备(贪心)
数据挖掘——认识数据
Force buckle - 322 Change
The difference between set method and add method in list
Data mining - logistic regression
数位dp(模板)
How to use on duplicate key update in MySQL
什么是JSON?初识JSON
List分割最佳实践
cookie 和 session 的区别
SQL优化最佳实践
JVM exploration
2022 Niuke winter vacation supplementary question record 2
工作中如何成长