43 lines
840 B
C++
43 lines
840 B
C++
#include "histo.h"
|
|
#include "opencv2/imgcodecs.hpp"
|
|
#include "opencv2/videoio.hpp"
|
|
#include <opencv2/core.hpp>
|
|
#include <opencv2/highgui.hpp>
|
|
#include <opencv2/imgproc.hpp>
|
|
#include <iostream>
|
|
|
|
using namespace cv;
|
|
|
|
int calc_th(Mat img, float fg_ratio = 0.1f){
|
|
int nfg = img.cols * img.rows * fg_ratio;
|
|
Mat histo;
|
|
Histo::calcHistoC1(img, histo);
|
|
|
|
double s = 0;
|
|
int th = 0;
|
|
|
|
for(; th < 256; th++){
|
|
s += histo.at<int>(th);
|
|
if(s >= nfg){
|
|
return th;
|
|
}
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
|
|
int main(){
|
|
Mat img = imread("../scanned3.png", IMREAD_GRAYSCALE);
|
|
int th = calc_th(img, 0.1f);
|
|
imshow("t", img);
|
|
Mat mask;
|
|
if(th != -1){
|
|
threshold(img, mask, th, 255, THRESH_BINARY);
|
|
imshow("img2", mask);
|
|
}
|
|
|
|
while(waitKey(3) != 'q');
|
|
return 0;
|
|
}
|