Skip to content

Instantly share code, notes, and snippets.

@Svtter
Created July 10, 2018 03:24
Show Gist options
  • Save Svtter/11ee34554c4496eb81c1a0b026dd326d to your computer and use it in GitHub Desktop.
Save Svtter/11ee34554c4496eb81c1a0b026dd326d to your computer and use it in GitHub Desktop.
opencv threshold cal
#include <opencv/cv.h>
#include <iostream>
using namespace cv;
// sum up given row
long int sumup(Mat row, int width)
{
std::cout << "Sumup" << std::endl;
long int total = 0;
for (int i = 0; i < width; i++)
{
total += row.at<uchar>(0, i);
}
std::cout << "total: " << total << std::endl;
return total;
}
// find the bottom threshold value.
int find_bottom(Mat img, int threshold)
{
int rows = img.rows;
int cols = img.cols;
int total_rows;
int bottom;
bottom = -1;
for (int i = rows - 1; i >= 0; i --)
{
total_rows = sumup(img.row(i), cols);
if (total_rows > threshold)
{
bottom = i;
break;
}
}
return bottom;
}
// test img function
void test()
{
Mat img = Mat::ones(3, 3, CV_8UC1);
img.at<uchar>(0, 0) = 2;
int bottom = find_bottom(img, 3);
std::cout << "Bottom: "<< bottom << std::endl;
}
int main()
{
test();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment