1. 程式人生 > >[OpenCV3程式設計入門讀書筆記]LUT函式(5)

[OpenCV3程式設計入門讀書筆記]LUT函式(5)

LUT函式可以用於影象元素的查詢、掃描和其他操作。

LUT函式定義

/** @brief Performs a look-up table transform of an array.

The function LUT fills the output array with values from the look-up table. Indices of the entries
are taken from the input array. That is, the function processes each element of src as follows:
\f[\texttt{dst} (I)  \leftarrow \texttt{lut(src(I) + d)}\f]
where
\f[d =  \fork{0}{if \(\texttt{src}\) has depth \(\texttt{CV_8U}\)}{128}{if \(\texttt{src}\) has depth \(\texttt{CV_8S}\)}\f]
@param src input array of 8-bit elements.
@param lut look-up table of 256 elements; in case of multi-channel input array, the table should
either have a single channel (in this case the same table is used for all channels) or the same
number of channels as in the input array.
@param dst output array of the same size and number of channels as src, and the same depth as lut.
@sa  convertScaleAbs, Mat::convertTo
*/
CV_EXPORTS_W void LUT(InputArray src, InputArray lut, OutputArray dst);
  1. src表示的是輸入影象(可以是單通道也可是3通道)
  2. lut表示查詢表(查詢表也可以是單通道,也可以是3通道,如果輸入影象為單通道,那查詢表必須為單通道,若輸入影象為3通道,查詢表可以為單通道,也可以為3通道,若為單通道則表示對影象3個通道都應用這個表,若為3通道則分別應用 )
  3. dst表示輸出影象,大小和通道必須與src相同

例子:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;


int main() {
	Mat image = imread("C:\\Users\\thor\\Desktop\\1.png");
	uchar table[256];
	int divideWith = 100;
	for (int i = 0; i < 256; i++)
		table[i] = (i / divideWith)*divideWith;
	Mat lut(1, 256, CV_8UC1, table);
	Mat outputImage;
	LUT(image, lut, outputImage);
	imshow("原始影象", image);
	imshow("輸出影象", outputImage);
	waitKey(0);
}

輸出: