Created
April 22, 2016 02:43
-
-
Save brianhsu/68806bb1a7739c5127b07050292a345b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <opencv2/opencv.hpp> | |
#include <stdio.h> | |
using namespace cv; | |
using namespace std; | |
class Detector { | |
public: | |
Detector(Mat & sourceImage); | |
int getCapacityTop1(int threshold); | |
int getCapacityTop2(int thresholdForBlackPoints, int thresholdForContinues); | |
int getCapacityBottom(int threshold); | |
int getCapacityBottom2(int thresholdForCapacity, int thresholdForContinues); | |
vector<int> getBlackPointsForEachRow(); | |
private: | |
Mat grayed; | |
int getBlackPoints(int row); | |
void setupBlackPointsForEachRow(); | |
vector<int> blackPointsForEachRow; | |
}; | |
Detector::Detector(Mat & sourceImage) { | |
cvtColor(sourceImage, grayed, CV_8U); | |
setupBlackPointsForEachRow(); | |
} | |
int Detector::getCapacityBottom(int threshold) { | |
for (int i = blackPointsForEachRow.size() - 1; i >= 0; i--) { | |
if (blackPointsForEachRow[i] >= threshold) { | |
return i; | |
} | |
} | |
} | |
int Detector::getCapacityBottom2(int thresholdForCapacity, int thresholdForContinues) { | |
int topRow2 = this->getCapacityTop2(10, 5); | |
int blackPointThreshold = this->blackPointsForEachRow[topRow2] - (this->blackPointsForEachRow[topRow2] * 0.1); | |
int last = -1; | |
int sameCount = 0; | |
for (int i = blackPointsForEachRow.size(); i >= 0; i--) { | |
int blackPoints = blackPointsForEachRow[i]; | |
if (last == blackPoints && blackPoints <= blackPointThreshold && blackPoints >= thresholdForCapacity) { | |
sameCount++; | |
if (sameCount >= thresholdForContinues - 1) { | |
return i + thresholdForContinues - 1; | |
} | |
} else { | |
last = blackPoints; | |
sameCount = 0; | |
} | |
} | |
return 0; | |
} | |
int Detector::getCapacityTop1(int threshold) { | |
for (int i = 0; i < blackPointsForEachRow.size(); i++) { | |
if (blackPointsForEachRow[i] >= threshold) { | |
return i; | |
} | |
} | |
} | |
int Detector::getCapacityTop2(int thresholdForBlackPoints, int thresholdForContinues) { | |
int last = -1; | |
int sameCount = 0; | |
for (int i = 0; i < blackPointsForEachRow.size(); i++) { | |
int blackPoints = blackPointsForEachRow[i]; | |
if (last == blackPoints && blackPoints >= thresholdForBlackPoints) { | |
sameCount++; | |
if (sameCount >= thresholdForContinues - 1) { | |
return i - thresholdForContinues + 1; | |
} | |
} else { | |
last = blackPoints; | |
sameCount = 0; | |
} | |
} | |
} | |
vector<int> Detector::getBlackPointsForEachRow() { | |
return blackPointsForEachRow; | |
} | |
int Detector::getBlackPoints(int row) { | |
int blackPixels = 0; | |
Mat rowMatrix = grayed.row(row); | |
MatIterator_<uchar> it = rowMatrix.begin<uchar>(); | |
MatIterator_<uchar> end = rowMatrix.end<uchar>(); | |
int counter = 0; | |
int start = 0; | |
bool flag = false; | |
while (it != end) { | |
if (*it == 0) { | |
blackPixels++; | |
} | |
it++; | |
counter++; | |
} | |
return blackPixels; | |
} | |
void Detector::setupBlackPointsForEachRow() { | |
for (int i = 0; i < grayed.rows; i++) { | |
int blackPoints = getBlackPoints(i); | |
blackPointsForEachRow.push_back(blackPoints); | |
} | |
} | |
int main(int argc, char** argv ) | |
{ | |
Mat src = imread("test.png", 1 ); | |
int width = src.cols; | |
int height = src.rows; | |
Detector * detector = new Detector(src); | |
vector<int> rowIntergal = detector->getBlackPointsForEachRow(); | |
int topRow1 = detector->getCapacityTop1(10); | |
int topRow2 = detector->getCapacityTop2(10, 5); | |
int capacityBottomRow = detector->getCapacityBottom(100); | |
int capacityBottom2 = detector->getCapacityBottom2(100, 2); | |
printf("topRow1: %d\n", topRow1); | |
printf("topRow2: %d\n", topRow2); | |
printf("capacityBottomRow: %d\n", capacityBottomRow); | |
printf("capacityBottom2: %d\n", capacityBottom2); | |
printf("L1 = %d\n", capacityBottomRow - topRow1); | |
printf("L0 = %d\n", capacityBottomRow - topRow2); | |
printf("D1 = %d\n", rowIntergal[topRow2]); | |
printf("B = %d\n", capacityBottomRow - capacityBottom2); | |
printf("A = %d\n", rowIntergal[capacityBottom2]); | |
printf("E = %d\n", rowIntergal[topRow2] - rowIntergal[capacityBottom2]); | |
line(src, Point(0, topRow1), Point(width, topRow1), Scalar(0, 0, 255)); | |
line(src, Point(0, topRow2), Point(width, topRow2), Scalar(0, 0, 255)); | |
line(src, Point(0, capacityBottomRow), Point(width, capacityBottomRow), Scalar(255, 0, 255)); | |
line(src, Point(0, capacityBottom2), Point(width, capacityBottom2), Scalar(255, 0, 255)); | |
namedWindow( "Source", CV_WINDOW_AUTOSIZE ); | |
imshow( "Source", src ); | |
waitKey(0); | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment