RLL Computer Vision Code: code/image_preprocessor.cpp 源文件

RLL Computer Vision

RLL Computer Vision Code  1.0
江苏科技大学HLL战队机器视觉代码
image_preprocessor.cpp
浏览该文件的文档.
1 #include "image_preprocessor.h"
2 
3 namespace HCVC {
5 {
6  //初始化阈值列表
7  FileStorage fs("statics/params.xml", FileStorage::READ);
8  if(!fs.isOpened())
9  {
10  cout << "Open file failed" << endl;
11  }
12  FileNode node = fs["image_preprocessor_threshod"];
13 
14  for(unsigned int i = 0; i < 3; i++)
15  {
16  cout << int(node[2*i]) << endl;
17  thresholds[i].push_back(int(node[2*i]));
18  thresholds[i].push_back(int(node[2*i+1]));
19  }
20 }
21 
22 Mat ImagePreprocessor::preprocess(const Mat& srcImage)
23 {
24  Mat dstImage;
25  //均值滤波,去除局部极值
26  //blur(srcImage, dstImage, Size(3,3));
27  //将bgr格式(opencv默认将彩色图片存储为bgr格式)图像转变为hsv格式
28  cvtColor(srcImage, dstImage, CV_BGR2HSV);
29  //分离图像三通道
30  Mat hsvImages[3];
31  split(dstImage, hsvImages);
32 
33  hsvImages[0] = rangeThreshold(hsvImages[0], 0);
34  hsvImages[1] = rangeThreshold(hsvImages[1], 1);
35  hsvImages[2] = rangeThreshold(hsvImages[2], 2);
36 
37  //获取自定义核,核边长只能为奇数
38  //Mat erodeKernel = getStructuringElement(MORPH_RECT, Size(3, 3));
39  Mat dilateKernel = getStructuringElement(MORPH_RECT, Size(15, 15));
40  Mat closeKernel = getStructuringElement(MORPH_RECT, Size(3, 3));
41  //腐蚀,消除离散点
42  //erode(hsvImages[1], hsvImages[1], erodeKernel);
43  //膨胀,增大光柱区域
44  dilate(hsvImages[0], hsvImages[0], dilateKernel);
45  //中值滤波,去除椒盐噪声
46  //medianBlur(hsvImages[2], hsvImages[2], 5);
47 // //显示装甲板轮廓
48 // Laplacian(hsvImages[1], hsvImages[1], CV_8UC1, 3, 1);
49  //逻辑与操作,过滤出小车灯柱
50  bitwise_and(hsvImages[0], hsvImages[2], dstImage);
51  //闭运算,将断裂的灯柱图像连接起来
52  morphologyEx(dstImage, dstImage, MORPH_CLOSE, closeKernel);
53 
54  //显示单通道处理后图像
55  imshow("hImage", hsvImages[0]);
56  imshow("SImage", hsvImages[1]);
57  imshow("VImage", hsvImages[2]);
58 
59  //显示预处理后图像
60  imshow("result", dstImage);
61 
62  return dstImage;
63 }
64 
65 void ImagePreprocessor::setThreshod(int channel, int minOrMax, int value)
66 {
67  thresholds[channel][minOrMax] = value;
68 }
69 
70 int ImagePreprocessor::getThreshod(int channel, int minOrMax) const
71 {
72  return thresholds[channel][minOrMax];
73 }
74 
75 Mat ImagePreprocessor::rangeThreshold(const Mat& srcImage, const int& channel)
76 {
77  Mat result;
78  threshold(srcImage, result, static_cast<double>(thresholds[channel][0]), 0, THRESH_TOZERO);
79  threshold(result, result, static_cast<double>(thresholds[channel][1]), 0, THRESH_TOZERO_INV);
80 
81  return result;
82 }
83 }
84 
Mat rangeThreshold(const Mat &srcImage, const int &channel)
用threshold函数实现区间图像提取
int getThreshod(int channel, int minOrMax) const
获取指定通道最大值或者最小值
Mat preprocess(const Mat &srcImage)
对读取到的图片预处理
vector< int > thresholds[3]
存储三通道过滤时,rgb的下上限值
void setThreshod(int channel, int minOrMax, int value)
设置预处理图像阈值
HLL Computer Vision Code namepace.
制作者   doxygen 1.8.13