部分代码来自:otsu自适应阈值分割
收获:
1了解了大津阈值法原理
2Matlab不自带霍夫圆变换;Opencv具有优化版霍夫圆变换,对大分辨率图像处理的速度也极快
#include <opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int OtsuThreshold(Mat* src)
{
int threshold;
try
{
int height = src->rows;
int width = src->cols;
//histogram
float histogram[256] = { 0 };
for (int i = 0; i < height; i++) {
unsigned char* p = (unsigned char*)src->data + src->step*i;
for (int j = 0; j < width; j++) {
histogram[*p++]++;
}
}
//normalize histogram
int size = height*width;
for (int i = 0; i < 256; i++) {
histogram[i] = histogram[i] / size;
}
//average pixel value