OpenCV 4.12.0
開源計算機視覺
載入中...
搜尋中...
無匹配項
samples/cpp/demhist.cpp

影像直方圖建立示例

#include <iostream>
using namespace cv;
using namespace std;
int _brightness = 100;
int _contrast = 100;
Mat image;
/* 亮度/對比度回撥函式 */
static void updateBrightnessContrast( int /*arg*/, void* )
{
int histSize = 64;
int brightness = _brightness - 100;
int contrast = _contrast - 100;
/*
* 該演算法由 Werner D. Streidt 提供
* (http://visca.com/ffactory/archives/5-99/msg00021.html)
*/
double a, b;
if( contrast > 0 )
{
double delta = 127.*contrast/100;
a = 255./(255. - delta*2);
b = a*(brightness - delta);
}
else
{
double delta = -128.*contrast/100;
a = (256.-delta*2)/255.;
b = a*brightness + delta;
}
Mat dst, hist;
image.convertTo(dst, CV_8U, a, b);
imshow("image", dst);
calcHist(&dst, 1, 0, Mat(), hist, 1, &histSize, 0);
Mat histImage = Mat::ones(200, 320, CV_8U)*255;
normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, CV_32F);
histImage = Scalar::all(255);
int binW = cvRound((double)histImage.cols/histSize);
for( int i = 0; i < histSize; i++ )
rectangle( histImage, Point(i*binW, histImage.rows),
Point((i+1)*binW, histImage.rows - cvRound(hist.at<float>(i))),
Scalar::all(0), -1, 8, 0 );
imshow("histogram", histImage);
}
const char* keys =
{
"{help h||}{@image|baboon.jpg|輸入影像檔案}"
};
int main( int argc, const char** argv )
{
CommandLineParser parser(argc, argv, keys);
parser.about("\n該程式演示了 calcHist() 的用法 -- 直方圖建立。\n");
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
string inputImage = parser.get<string>(0);
// 載入源影像。使用 HighGUI。
image = imread(samples::findFile(inputImage), IMREAD_GRAYSCALE);
if(image.empty())
{
std::cerr << "無法讀取影像檔案: " << inputImage << std::endl;
return -1;
}
namedWindow("image", 0);
namedWindow("histogram", 0);
createTrackbar("brightness", "image", &_brightness, 200, updateBrightnessContrast);
createTrackbar("contrast", "image", &_contrast, 200, updateBrightnessContrast);
updateBrightnessContrast(0, 0);
return 0;
}
如果陣列沒有元素,則返回 true。
int64_t int64
n 維密集陣列類
定義 mat.hpp:830
_Tp & at(int i0=0)
返回指定陣列元素的引用。
int cols
定義 mat.hpp:2165
int rows
當矩陣具有超過 2 個維度時,行數和列數或 (-1, -1)
定義 mat.hpp:2165
void convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const
使用可選縮放將陣列轉換為另一種資料型別。
CV_8U
#define CV_8U
#define CV_32F
Definition interface.h:78
int cvRound(double value)
將浮點數舍入為最接近的整數。
Definition fast_math.hpp:200
int waitKey(int delay=0)
等待按鍵按下。
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
建立視窗。
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
建立滑動條並將其附加到指定視窗。
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
從檔案載入影像。
int main(int argc, char *argv[])
定義 highgui_qt.cpp:3
定義 core.hpp:107
STL 名稱空間。