Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit110258c

Browse files
committed
new function imquery() and imread()
1 parent7de627c commit110258c

File tree

6 files changed

+169
-23
lines changed

6 files changed

+169
-23
lines changed

‎modules/imgcodecs/include/opencv2/imgcodecs.hpp‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,46 @@ enum ImwritePAMFlags {
147147
IMWRITE_PAM_FORMAT_GRAYSCALE =2,
148148
IMWRITE_PAM_FORMAT_GRAYSCALE_ALPHA =3,
149149
IMWRITE_PAM_FORMAT_RGB =4,
150-
IMWRITE_PAM_FORMAT_RGB_ALPHA =5,
150+
IMWRITE_PAM_FORMAT_RGB_ALPHA =5
151151
};
152152

153+
//! imread Return Codes.
154+
enum ImreadResults {
155+
IMREAD_SUCCESS =0,
156+
IMREAD_FILE_NOT_OPENED =1,
157+
IMREAD_UNKNOWN_FILE_TYPE =2,
158+
IMREAD_READ_HEADER_ERROR =3,
159+
IMREAD_READ_DATA_ERROR =4
160+
};
153161
//! @} imgcodecs_flags
154162

163+
structCV_EXPORTS_W_SIMPLE pageInfo
164+
{
165+
CV_PROPint type;
166+
CV_PROPint width;
167+
CV_PROPint height;
168+
CV_PROPbool scalable;
169+
};
170+
171+
structCV_EXPORTS_W_SIMPLE imInfo
172+
{
173+
CV_PROPint errorcode;
174+
CV_PROPint page_count;
175+
CV_WRAP pageInfogetPageInfo(int index);
176+
std::vector<pageInfo> pages;
177+
};
178+
179+
inline pageInfoimInfo::getPageInfo(int index)
180+
{
181+
return pages[index];
182+
}
183+
184+
/** @brief Reads the image file header and gets image properties.
185+
186+
The function fills image information into returned struct cv::imInfo
187+
*/
188+
CV_EXPORTS_W imInfoimquery(const String& filename);
189+
155190
/** @brief Loads an image from a file.
156191
157192
@anchor imread
@@ -205,6 +240,14 @@ Currently, the following file formats are supported:
205240
*/
206241
CV_EXPORTS_W Matimread(const String& filename,int flags = IMREAD_COLOR );
207242

243+
/** @overload
244+
@param filename Name of file to be loaded.
245+
@param image OutputArray where the image data will be loaded.
246+
@param flags Flag that can take values of cv::ImreadModes
247+
@param index page index to be loaded for multipage image files.
248+
*/
249+
CV_EXPORTS_Wintimread(const String& filename, OutputArray image,int flags = IMREAD_COLOR,int index =0);
250+
208251
/** @brief Loads a multi-page image from a file.
209252
210253
The function imreadmulti loads a multi-page image from the specified file into a vector of Mat objects.

‎modules/imgcodecs/src/grfmt_base.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ BaseImageDecoder::BaseImageDecoder()
5353
m_type = -1;
5454
m_buf_supported =false;
5555
m_scale_denom =1;
56+
m_page_count =1;
57+
m_error_code =0;
5658
}
5759

5860

5961
ExifEntry_tBaseImageDecoder::getExifTag(const ExifTagName tag)const
6062
{
6163
return m_exif.getTag(tag);
6264
}
65+
6366
boolBaseImageDecoder::setSource(const String& filename )
6467
{
6568
m_filename = filename;

‎modules/imgcodecs/src/grfmt_base.hpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ class BaseImageDecoder
6464

6565
intwidth()const {return m_width; }
6666
intheight()const {return m_height; }
67+
intgetErrorCode()const{return m_error_code; }
6768
virtualinttype()const {return m_type; }
6869

6970
ExifEntry_tgetExifTag(const ExifTagName tag)const;
71+
virtualintgetPageCount()const {return m_page_count; }
7072
virtualboolsetSource(const String& filename );
7173
virtualboolsetSource(const Mat& buf );
7274
virtualintsetScale(constint& scale_denom );
@@ -85,6 +87,8 @@ class BaseImageDecoder
8587
int m_height;// height of the image ( filled by readHeader )
8688
int m_type;
8789
int m_scale_denom;
90+
int m_page_count;
91+
int m_error_code;
8892
String m_filename;
8993
String m_signature;
9094
Mat m_buf;

‎modules/imgcodecs/src/grfmt_jpeg.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ JpegDecoder::JpegDecoder()
181181
m_state =0;
182182
m_f =0;
183183
m_buf_supported =true;
184+
m_scale_denom = -1;
184185
}
185186

186187

@@ -249,7 +250,7 @@ bool JpegDecoder::readHeader()
249250

250251
state->cinfo.scale_num=1;
251252
state->cinfo.scale_denom = m_scale_denom;
252-
m_scale_denom=1;// trick! to know which decoder used scale_denom see imread_
253+
m_scale_denom = -1;// trick! to know which decoder used scale_denom see imread_
253254
jpeg_calc_output_dimensions(&state->cinfo);
254255
m_width = state->cinfo.output_width;
255256
m_height = state->cinfo.output_height;
@@ -505,6 +506,7 @@ bool JpegDecoder::readData( Mat& img )
505506
}
506507
}
507508

509+
m_error_code = cinfo->err->msg_code ==117 ?117 :0;
508510
result =true;
509511
jpeg_finish_decompress( cinfo );
510512
}

‎modules/imgcodecs/src/grfmt_tiff.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ bool TiffDecoder::readHeader()
266266
uint32 wdth =0, hght =0;
267267
uint16 photometric =0;
268268

269+
m_page_count =TIFFNumberOfDirectories(tif);
269270
CV_TIFF_CHECK_CALL(TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &wdth));
270271
CV_TIFF_CHECK_CALL(TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &hght));
271272
CV_TIFF_CHECK_CALL(TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric));

‎modules/imgcodecs/src/loadsave.cpp‎

Lines changed: 114 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ ImageCodecInitializer& getCodecs()
219219
*
220220
* @return Image decoder to parse image file.
221221
*/
222-
static ImageDecoderfindDecoder(const String& filename ) {
222+
static ImageDecoderfindDecoder(const String& filename,int& exception_code ) {
223223

224224
size_t i, maxlen =0;
225225

@@ -236,7 +236,10 @@ static ImageDecoder findDecoder( const String& filename ) {
236236

237237
/// in the event of a failure, return an empty image decoder
238238
if( !f )
239+
{
240+
exception_code = IMREAD_FILE_NOT_OPENED;
239241
returnImageDecoder();
242+
}
240243

241244
// read the file signature
242245
Stringsignature(maxlen,'');
@@ -252,6 +255,7 @@ static ImageDecoder findDecoder( const String& filename ) {
252255
}
253256

254257
/// If no decoder was found, return base type
258+
exception_code = IMREAD_UNKNOWN_FILE_TYPE;
255259
returnImageDecoder();
256260
}
257261

@@ -377,32 +381,34 @@ static void ApplyExifOrientation(ExifEntry_t orientationTag, Mat& img)
377381
*
378382
* @param[in] filename File to load
379383
* @param[in] flags Flags
380-
* @param[in] hdrtype { LOAD_CVMAT=0,
381-
* LOAD_IMAGE=1,
382-
* LOAD_MAT=2
383-
* }
384-
* @param[in] mat Reference to C++ Mat object (If LOAD_MAT)
384+
* @param[in] mat Reference to C++ Mat object
385+
* @param index a variable used when loading multipage images
385386
*
386387
*/
387-
staticbool
388-
imread_(const String& filename,int flags, Mat& mat )
388+
staticint
389+
imread_(const String& filename,int flags, Mat& mat,int index )
389390
{
390391
/// Search for the relevant decoder to handle the imagery
391392
ImageDecoder decoder;
393+
int exception_code;
392394

393395
#ifdef HAVE_GDAL
394396
if(flags != IMREAD_UNCHANGED && (flags & IMREAD_LOAD_GDAL) == IMREAD_LOAD_GDAL ){
395397
decoder =GdalDecoder().newDecoder();
396398
}else{
397399
#endif
398-
decoder =findDecoder( filename );
400+
decoder =findDecoder( filename, exception_code );
399401
#ifdef HAVE_GDAL
400402
}
401403
#endif
402404

403405
/// if no decoder was found, return nothing.
404406
if( !decoder ){
405-
return0;
407+
if (!mat.empty())
408+
{
409+
mat.at<int>(0,0) =0;
410+
}
411+
return exception_code;
406412
}
407413

408414
int scale_denom =1;
@@ -417,7 +423,7 @@ imread_( const String& filename, int flags, Mat& mat )
417423
}
418424

419425
/// set the scale_denom in the driver
420-
decoder->setScale( scale_denom );
426+
int initial_scale_denom =decoder->setScale( scale_denom );
421427

422428
/// set the filename in the driver
423429
decoder->setSource( filename );
@@ -426,23 +432,45 @@ imread_( const String& filename, int flags, Mat& mat )
426432
{
427433
// read the header to make sure it succeeds
428434
if( !decoder->readHeader() )
429-
return0;
435+
returnIMREAD_READ_HEADER_ERROR;
430436
}
431437
catch (const cv::Exception& e)
432438
{
433439
std::cerr <<"imread_('" << filename <<"'): can't read header:" << e.what() << std::endl << std::flush;
434-
return0;
440+
returnIMREAD_READ_HEADER_ERROR;
435441
}
436442
catch (...)
437443
{
438444
std::cerr <<"imread_('" << filename <<"'): can't read header: unknown exception" << std::endl << std::flush;
439-
return0;
445+
returnIMREAD_READ_HEADER_ERROR;
440446
}
441447

442448

443449
// established the required input image size
444450
Size size =validateInputImageSize(Size(decoder->width(), decoder->height()));
445451

452+
if (index <0)// if index is -1 that means the function called only for reading header
453+
{
454+
int pageCount = decoder->getPageCount();
455+
mat.at<int>(0,0) = pageCount;
456+
457+
for(int i =0; i < pageCount; i++)
458+
{
459+
mat.push_back(decoder->type());
460+
mat.push_back(decoder->width());
461+
mat.push_back(decoder->height());
462+
mat.push_back(initial_scale_denom <0 ?1 :0);
463+
decoder->nextPage();
464+
}
465+
return IMREAD_SUCCESS;
466+
}
467+
468+
int counter =0;
469+
470+
for (;;)
471+
{
472+
if ((index <1) || (counter == index))
473+
{
446474
// grab the decoded type
447475
int type = decoder->type();
448476
if( (flags & IMREAD_LOAD_GDAL) != IMREAD_LOAD_GDAL && flags != IMREAD_UNCHANGED )
@@ -477,7 +505,7 @@ imread_( const String& filename, int flags, Mat& mat )
477505
if (!success)
478506
{
479507
mat.release();
480-
returnfalse;
508+
returnIMREAD_READ_DATA_ERROR + decoder->getErrorCode();
481509
}
482510

483511
if( decoder->setScale( scale_denom ) >1 )// if decoder is JpegDecoder then decoder->setScale always returns 1
@@ -491,10 +519,43 @@ imread_( const String& filename, int flags, Mat& mat )
491519
ApplyExifOrientation(decoder->getExifTag(ORIENTATION), mat);
492520
}
493521

494-
returntrue;
522+
return IMREAD_SUCCESS + decoder->getErrorCode();
523+
}
524+
525+
if (!decoder->nextPage())
526+
{
527+
break;
528+
}
529+
else
530+
counter++;
531+
}
532+
533+
return IMREAD_SUCCESS + decoder->getErrorCode();
495534
}
496535

497536

537+
imInfoimquery(const String& filename)
538+
{
539+
CV_TRACE_FUNCTION();
540+
541+
imInfo iminfo;
542+
Matinfo(1,1, CV_32S,Scalar(0));
543+
int ret =imread_(filename, IMREAD_UNCHANGED, info, -1);
544+
iminfo.errorcode = ret;
545+
iminfo.page_count = info.at<int>(0,0);
546+
547+
for (int i =0; i < iminfo.page_count; i ++)
548+
{
549+
pageInfo pageinfo;
550+
pageinfo.type = info.at<int>(i *4 +1,0);
551+
pageinfo.width = info.at<int>(i *4 +2,0);
552+
pageinfo.height = info.at<int>(i *4 +3,0);
553+
pageinfo.scalable = info.at<int>(i *4 +4,0);
554+
iminfo.pages.push_back(pageinfo);
555+
}
556+
return iminfo;
557+
}
558+
498559
staticbool
499560
imreadmulti_(const String& filename,int flags, std::vector<Mat>& mats,int start,int count)
500561
{
@@ -509,7 +570,8 @@ imreadmulti_(const String& filename, int flags, std::vector<Mat>& mats, int star
509570
}
510571
else {
511572
#endif
512-
decoder =findDecoder(filename);
573+
int exception_code;
574+
decoder =cv::findDecoder(filename, exception_code);
513575
#ifdef HAVE_GDAL
514576
}
515577
#endif
@@ -626,12 +688,30 @@ Mat imread( const String& filename, int flags )
626688
Mat img;
627689

628690
/// load the data
629-
imread_( filename, flags, img );
691+
imread_( filename, flags, img,0 );
630692

631693
/// return a reference to the data
632694
return img;
633695
}
634696

697+
/**
698+
* Read an image into specified OutputArray
699+
*
700+
* @param[in] filename Name of file to be loaded.
701+
* @param[in] image OutputArray where the image data will be loaded.
702+
* @param[in] flags Flag that can take values of cv::ImreadModes
703+
* @param index page index to be loaded for multipage image files.
704+
*/
705+
intimread(const String& filename, OutputArray image,int flags,int index)
706+
{
707+
CV_TRACE_FUNCTION();
708+
709+
Mat img = image.getMat();
710+
int ret =imread_(filename, flags, img, index);
711+
image.assign(img);
712+
return ret;
713+
}
714+
635715
/**
636716
* Read a multi-page image
637717
*
@@ -646,7 +726,18 @@ bool imreadmulti(const String& filename, std::vector<Mat>& mats, int flags)
646726
{
647727
CV_TRACE_FUNCTION();
648728

649-
returnimreadmulti_(filename, flags, mats,0, -1);
729+
imInfo info =imquery(filename);
730+
731+
for (int i =0; i < info.page_count; i++)
732+
{
733+
Mat page;
734+
imread_(filename, flags, page, i);
735+
736+
if (page.empty())
737+
break;
738+
mats.push_back(page);
739+
}
740+
return !mats.empty();
650741
}
651742

652743

@@ -671,7 +762,8 @@ size_t imcount_(const String& filename, int flags)
671762
#else
672763
CV_UNUSED(flags);
673764
#endif
674-
decoder =findDecoder(filename);
765+
int exception_code;
766+
decoder =cv::findDecoder(filename, exception_code);
675767
#ifdef HAVE_GDAL
676768
}
677769
#endif
@@ -1024,7 +1116,8 @@ bool imencode( const String& ext, InputArray _image,
10241116

10251117
boolhaveImageReader(const String& filename )
10261118
{
1027-
ImageDecoder decoder =cv::findDecoder(filename);
1119+
int exception_code;
1120+
ImageDecoder decoder =cv::findDecoder(filename, exception_code);
10281121
return !decoder.empty();
10291122
}
10301123

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp