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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/opencv.hpp> using namespace std; using namespace cv;
Mat g_srcImage, g_dstImage; int g_nElementShape = MORPH_RECT;
int g_nMaxIterationNum = 10; int g_nOpenCloseNum = 0; int g_nErodeDilateNum = 0; int g_nTopBlackHatNum = 0;
static void on_OpenClose(int, void *); static void on_ErodeDilate(int, void *); static void on_TopBlackHat(int, void *); static void ShowHelpText();
int main() { ShowHelpText();
g_srcImage = imread("1.jpg"); if (!g_srcImage.data) { printf("Oh,no,读取srcImage错误~! \n"); return false; }
namedWindow("【原始图】"); imshow("【原始图】", g_srcImage);
namedWindow("【开运算/闭运算】", 1); namedWindow("【腐蚀/膨胀】", 1); namedWindow("【顶帽/黑帽】", 1);
g_nOpenCloseNum = 9; g_nErodeDilateNum = 9; g_nTopBlackHatNum = 2;
createTrackbar("迭代值", "【开运算/闭运算】", &g_nOpenCloseNum, g_nMaxIterationNum * 2 + 1, on_OpenClose); createTrackbar("迭代值", "【腐蚀/膨胀】", &g_nErodeDilateNum, g_nMaxIterationNum * 2 + 1, on_ErodeDilate); createTrackbar("迭代值", "【顶帽/黑帽】", &g_nTopBlackHatNum, g_nMaxIterationNum * 2 + 1, on_TopBlackHat);
while (1) { int c;
on_OpenClose(g_nOpenCloseNum, 0); on_ErodeDilate(g_nErodeDilateNum, 0); on_TopBlackHat(g_nTopBlackHatNum, 0);
c = waitKey(0);
if ((char)c == 'q' || (char)c == 27) break; if ((char)c == 49) g_nElementShape = MORPH_ELLIPSE; else if ((char)c == 50) g_nElementShape = MORPH_RECT; else if ((char)c == 51) g_nElementShape = MORPH_CROSS; else if ((char)c == ' ') g_nElementShape = (g_nElementShape + 1) % 3; }
return 0; }
static void on_OpenClose(int, void *) { int offset = g_nOpenCloseNum - g_nMaxIterationNum; int Absolute_offset = offset > 0 ? offset : -offset; Mat element = getStructuringElement(g_nElementShape, Size(Absolute_offset * 2 + 1, Absolute_offset * 2 + 1), Point(Absolute_offset, Absolute_offset)); if (offset < 0) morphologyEx(g_srcImage, g_dstImage, MORPH_OPEN, element); else morphologyEx(g_srcImage, g_dstImage, MORPH_CLOSE, element);
imshow("【开运算/闭运算】", g_dstImage); }
static void on_ErodeDilate(int, void *) { int offset = g_nErodeDilateNum - g_nMaxIterationNum; int Absolute_offset = offset > 0 ? offset : -offset; Mat element = getStructuringElement(g_nElementShape, Size(Absolute_offset * 2 + 1, Absolute_offset * 2 + 1), Point(Absolute_offset, Absolute_offset)); if (offset < 0) erode(g_srcImage, g_dstImage, element); else dilate(g_srcImage, g_dstImage, element); imshow("【腐蚀/膨胀】", g_dstImage); }
static void on_TopBlackHat(int, void *) { int offset = g_nTopBlackHatNum - g_nMaxIterationNum; int Absolute_offset = offset > 0 ? offset : -offset; Mat element = getStructuringElement(g_nElementShape, Size(Absolute_offset * 2 + 1, Absolute_offset * 2 + 1), Point(Absolute_offset, Absolute_offset)); if (offset < 0) morphologyEx(g_srcImage, g_dstImage, MORPH_TOPHAT, element); else morphologyEx(g_srcImage, g_dstImage, MORPH_BLACKHAT, element); imshow("【顶帽/黑帽】", g_dstImage); }
static void ShowHelpText() { printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n"); printf("\n\n\t\t\t此为本书OpenCV3版的第48个配套示例程序\n"); printf("\n\n\t\t\t 当前使用的OpenCV版本为:" CV_VERSION); printf("\n\n ----------------------------------------------------------------------------\n");
printf("\n\t请调整滚动条观察图像效果\n\n"); printf("\n\t按键操作说明: \n\n" "\t\t键盘按键【ESC】或者【Q】- 退出程序\n" "\t\t键盘按键【1】- 使用椭圆(Elliptic)结构元素\n" "\t\t键盘按键【2】- 使用矩形(Rectangle )结构元素\n" "\t\t键盘按键【3】- 使用十字型(Cross-shaped)结构元素\n" "\t\t键盘按键【空格SPACE】- 在矩形、椭圆、十字形结构元素中循环\n"); }
|