forked from ganler/SPTraining-VisionGroup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproportion_threshold.cpp
More file actions
48 lines (44 loc) · 1.2 KB
/
proportion_threshold.cpp
File metadata and controls
48 lines (44 loc) · 1.2 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
//
// Created by lisihang on 2019/10/9.
//
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
using namespace std;
using namespace cv;
namespace sp
{
void proportion_thresh(cv::Mat& gray_in, cv::Mat& out, double max_val, double proportion)
{
int row = gray_in.rows;
int col = gray_in.cols;
int grosse = row * col,num(0),z(0),pixel[256]={0};
for(int i = 0; i < row; i++){
const uchar* data = gray_in.ptr<uchar>(i);
for(int j =0;j < col;j++)
pixel[data[j]] = pixel[data[j]] + 1;
}
for(;num<=grosse * proportion;z++)
num = num + pixel[z];
std::cout<<z;
for(int i = 0; i < row; i++){
const uchar* data = gray_in.ptr<uchar>(i);
for(int j =0;j < col;j++){
if(int(data[j]) > z)
gray_in.at<uchar>(i,j) = max_val;
else
gray_in.at<uchar>(i,j) = 0;
}
}
out = gray_in;
}
}
int main()
{
Mat frame = imread("../1.png",0) ;
Mat frame_out;
sp::proportion_thresh(frame,frame_out,255,0.8);
imshow("after",frame_out);
waitKey(0);
return 0;
}