-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathPreprocess.cpp
More file actions
52 lines (34 loc) · 2.02 KB
/
Preprocess.cpp
File metadata and controls
52 lines (34 loc) · 2.02 KB
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
// 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::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);
}