Opencv imread用法

本文介绍了OpenCV中imread函数的使用方法及注意事项,特别是当处理单波段图像时如何正确设置参数以避免图像处理结果仅为输入图像的1/3的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

#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(j - 1);
		const uchar* current = image.ptr(j);
		const uchar* next = image.ptr(j + 1);
		uchar* output = result.ptr(j);
		for (int i = 1;i(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(end_time - start_time)/CLOCKS_PER_SEC << "s" << endl;
	system("PAUSE");
	return 0;
}
在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就好了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值