-
Notifications
You must be signed in to change notification settings - Fork 0
/
Preprocess.cpp
60 lines (39 loc) · 2.23 KB
/
Preprocess.cpp
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
// Preprocess.cpp
#include "Preprocess.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
void preprocess(cv::Mat &imgOriginal, cv::Mat &imgGrayscale, cv::Mat &imgThresh) {
imgGrayscale = extractValue(imgOriginal); // extract value channel only from original image to get imgGrayscale
cv::Mat imgMaxContrastGrayscale = maximizeContrast(imgGrayscale); // maximize contrast with top hat and black hat
cv::Mat imgBlurred;
cv::GaussianBlur(imgMaxContrastGrayscale, imgBlurred, GAUSSIAN_SMOOTH_FILTER_SIZE, 0); // gaussian blur
// call adaptive threshold to get imgThresh
//cv::adaptiveThreshold(imgBlurred, imgThresh, 255.0, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY_INV, ADAPTIVE_THRESH_BLOCK_SIZE, ADAPTIVE_THRESH_WEIGHT);
cv::adaptiveThreshold(imgBlurred, imgThresh , 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY, 55, 5);
Mat element = getStructuringElement(MORPH_RECT, cv::Size(3, 3));
erode(imgThresh, imgThresh, element);
dilate(imgThresh, imgThresh, element);
medianBlur(imgThresh,imgThresh , 5);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
cv::Mat extractValue(cv::Mat &imgOriginal) {
cv::Mat imgHSV;
std::vector<cv::Mat> vectorOfHSVImages;
cv::Mat imgValue;
cv::cvtColor(imgOriginal, imgHSV, CV_BGR2HSV);
cv::split(imgHSV, vectorOfHSVImages);
imgValue = vectorOfHSVImages[2];
return(imgValue);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
cv::Mat maximizeContrast(cv::Mat &imgGrayscale) {
cv::Mat imgTopHat;
cv::Mat imgBlackHat;
cv::Mat imgGrayscalePlusTopHat;
cv::Mat imgGrayscalePlusTopHatMinusBlackHat;
cv::Mat structuringElement = cv::getStructuringElement(CV_SHAPE_RECT, cv::Size(3, 3));
cv::morphologyEx(imgGrayscale, imgTopHat, CV_MOP_TOPHAT, structuringElement);
cv::morphologyEx(imgGrayscale, imgBlackHat, CV_MOP_BLACKHAT, structuringElement);
imgGrayscalePlusTopHat = imgGrayscale + imgTopHat;
imgGrayscalePlusTopHatMinusBlackHat = imgGrayscalePlusTopHat - imgBlackHat;
return(imgGrayscalePlusTopHatMinusBlackHat);
}