openCV基础函数imread第二个参数

imread是学OpenCV 的第一个函数了,一直都用默认的方式也就是cv::imread("图像名");

但是在执行一个简单的图像锐化算法的时候输出图像总是输入图像的1/3,请教师兄后才知道是图像读入的问题。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 
             
#include
#include
#include

using namespace std ;
using namespace cv ;


void sharpen ( const Mat & image , Mat & result )
{
result . create ( image . size (), image . type ());
for ( int j = 1 ; j < image . rows - 1 ; j ++ )
{
const uchar * previous = image . ptr < const uchar > ( j - 1 );
const uchar * current = image . ptr < const uchar > ( j );
const uchar * next = image . ptr < const uchar > ( j + 1 );
uchar * output = result . ptr < uchar > ( j );
for ( int i = 1 ; i < image . cols - 1 ; i ++ )
{
* output ++ = saturate_cast < uchar > ( 5 * current [ i ] - current [ i - 1 ] - current [ i + 1 ] - previous [ i ] - next [ i ]);
}
}
result . row ( 0 ). setTo ( Scalar ( 0 ));
result . row ( result . rows - 1 ). setTo ( Scalar ( 0 ));
result . col ( 0 ). setTo ( Scalar ( 0 ));
result . col ( result . cols - 1 ). setTo ( Scalar ( 0 ));
}
int main ()
{
clock_t start_time = clock ();

Mat image = imread ( "home.tif" , CV_LOAD_IMAGE_UNCHANGED );
if ( ! image . data )
{
cout << "Cannot Open!!" << endl ;
}
imshow ( "Original" , image );
Mat result ;
sharpen ( image , result );
imshow ( "result" , result );
waitKey ( 0 );

clock_t end_time = clock ();
cout << "Running Time: " << static_cast < double > ( end_time - start_time ) / CLOCKS_PER_SEC << "s" << endl ;
system ( "PAUSE" );
return 0 ;
}
来自CODE的代码片
OpenCVCookBook2-6.cpp
在main()里的imread,先前没有加第二个参数CV_LOAD_IMAGE_UNCHANGED,但是函数默认以RGB三波段形式读入图像,可是我给的是一个单波段的tif图像,因此,图像的处理效果就只有源图像的1/3了。

在reference里imread的各个参数如下:

C++: Mat imread(const string& filename, int flags=1 )

 
  • filename – Name of file to be loaded.
  • flags –

    Flags specifying the color type of a loaded image:

    • CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
    • CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
    • CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
    • >0 Return a 3-channel color image.

      Note

       

      In the current implementation the alpha channel, if any, is stripped from the output image. Use negative value if you need the alpha channel.

    • =0 Return a grayscale image.
    • <0 Return the loaded image as is (with alpha channel).
由于接触时间短,还总结不出有什么精辟的结论,但是以后似乎用CV_LOAD_IMAGE_UNCHANGED就好了。

你可能感兴趣的:(OPENCV)