Opencv imread用法

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就好了。
發佈了11 篇原創文章 · 獲贊 9 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章