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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace cv; using namespace std;
#define WINDOW_NAME "threshold"
int g_nThresholdValue = 100; int g_nThresholdType = 3; Mat g_srcImage, g_grayImage, g_dstImage;
static void ShowHelpText(); void on_Threshold(int, void *);
int main() { ShowHelpText();
g_srcImage = imread("1.jpg"); if (!g_srcImage.data) { printf("读取图片错误,请确定目录下是否有imread函数指定的图片存在~! \n"); return false; } imshow("Original", g_srcImage);
cvtColor(g_srcImage, g_grayImage, COLOR_RGB2GRAY);
namedWindow(WINDOW_NAME, WINDOW_AUTOSIZE);
createTrackbar("模式", WINDOW_NAME, &g_nThresholdType, 4, on_Threshold);
createTrackbar("参数值", WINDOW_NAME, &g_nThresholdValue, 255, on_Threshold);
on_Threshold(0, 0);
while (1) { int key; key = waitKey(20); if ((char)key == 27) { break; } } }
void on_Threshold(int, void *) { threshold(g_grayImage, g_dstImage, g_nThresholdValue, 255, g_nThresholdType);
imshow(WINDOW_NAME, g_dstImage); }
static void ShowHelpText() { printf("\n\t欢迎来到【基本阈值操作】示例程序~\n\n"); printf("\n\t按键操作说明: \n\n" "\t\t键盘按键【ESC】- 退出程序\n" "\t\t滚动条模式0- 二进制阈值\n" "\t\t滚动条模式1- 反二进制阈值\n" "\t\t滚动条模式2- 截断阈值\n" "\t\t滚动条模式3- 反阈值化为0\n" "\t\t滚动条模式4- 阈值化为0\n"); }
|