1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 07:13:02 +04:00

Added IMREAD_IGNORE_ORIENTATION flag

This commit is contained in:
Arkadiusz Raj
2016-11-09 18:09:22 +01:00
parent 17ffb28807
commit f893ee5cec
8 changed files with 93 additions and 88 deletions
+70
View File
@@ -45,6 +45,8 @@
#include "precomp.hpp"
#include "grfmts.hpp"
#include "utils.hpp"
#include "exif.hpp"
#undef min
#undef max
#include <iostream>
@@ -227,8 +229,11 @@ static ImageEncoder findEncoder( const String& _ext )
return ImageEncoder();
}
enum { LOAD_CVMAT=0, LOAD_IMAGE=1, LOAD_MAT=2 };
void RotateImage(const String& filename, Mat& img);
/**
* Read an image into memory and return the information
*
@@ -403,6 +408,12 @@ imreadmulti_(const String& filename, int flags, std::vector<Mat>& mats)
Mat mat(decoder->height(), decoder->width(), type);
if (!decoder->readData(mat))
{
// optionally rotate the data if EXIF' orientation flag says so
if( (flags & IMREAD_IGNORE_ORIENTATION) == 0 )
{
RotateImage(filename, mat);
}
break;
}
@@ -416,6 +427,59 @@ imreadmulti_(const String& filename, int flags, std::vector<Mat>& mats)
return !mats.empty();
}
void RotateImage(const String& filename, Mat& img)
{
int orientation = IMAGE_ORIENTATION_TL;
if (filename.size() > 0)
{
ExifReader reader( filename );
if( reader.parse() )
{
ExifEntry_t entry = reader.getTag( ORIENTATION );
if (entry.tag != INVALID_TAG)
{
orientation = entry.field_u16; //orientation is unsigned short, so check field_u16
}
}
}
switch( orientation )
{
case IMAGE_ORIENTATION_TL: //0th row == visual top, 0th column == visual left-hand side
//do nothing, the image already has proper orientation
break;
case IMAGE_ORIENTATION_TR: //0th row == visual top, 0th column == visual right-hand side
flip(img, img, 1); //flip horizontally
break;
case IMAGE_ORIENTATION_BR: //0th row == visual bottom, 0th column == visual right-hand side
flip(img, img, -1);//flip both horizontally and vertically
break;
case IMAGE_ORIENTATION_BL: //0th row == visual bottom, 0th column == visual left-hand side
flip(img, img, 0); //flip vertically
break;
case IMAGE_ORIENTATION_LT: //0th row == visual left-hand side, 0th column == visual top
transpose(img, img);
break;
case IMAGE_ORIENTATION_RT: //0th row == visual right-hand side, 0th column == visual top
transpose(img, img);
flip(img, img, 1); //flip horizontally
break;
case IMAGE_ORIENTATION_RB: //0th row == visual right-hand side, 0th column == visual bottom
transpose(img, img);
flip(img, img, -1); //flip both horizontally and vertically
break;
case IMAGE_ORIENTATION_LB: //0th row == visual left-hand side, 0th column == visual bottom
transpose(img, img);
flip(img, img, 0); //flip vertically
break;
default:
//by default the image read has normal (JPEG_ORIENTATION_TL) orientation
break;
}
}
/**
* Read an image
*
@@ -432,6 +496,12 @@ Mat imread( const String& filename, int flags )
/// load the data
imread_( filename, flags, LOAD_MAT, &img );
/// optionally rotate the data if EXIF' orientation flag says so
if( (flags & IMREAD_IGNORE_ORIENTATION) == 0 )
{
RotateImage(filename, img);
}
/// return a reference to the data
return img;
}