没有合适的资源?快使用搜索试试~ 我知道了~
1. 图像分割进行A4纸矫正 1. 迭代法求阈值 2.OSTU法求阈值 1. 统计霍夫空间点 2. 获取对应检测出的直线
资源推荐
资源详情
资源评论
计算机视觉实验报告
16340220 王培钰 电子政务
1. 图像分割进行A4纸矫正
(1) 将图像从rgb空间转为灰度空间
(2) 高斯滤波
(3) 求阈值
1. 迭代法求阈值
void ImageSegmentation::rgb2gray() {
grayImg.resize(Img._width, Img._height, 1, 1, 0);
cimg_forXY(grayImg, x, y) {
double R = Img(x,y,0);
double G = Img(x,y,1);
double B = Img(x,y,2);
double Gray = (R * 299 + G * 587 + B * 114 + 500) / 1000;
grayImg(x,y) = Gray;
}
}
void ImageSegmentation::Gauss_blur() {
blurImg = grayImg.get_blur(guassian_blur);
}
//迭代法求阈值
void ImageSegmentation::get_thres_iteration() {
CImg<int> image = blurImg;
CImg<int> hist = image.histogram(256, 0, 255);
int size = blurImg.size();
cimg_forX(hist, i) {
threshold += i*hist(i);
}
threshold /= size;
int threshold_new;
while (true) {
int t1 = 0, t2 = 0;
int num1 = 0, num2 = 0;
// 计算小于等于阈值threshold的灰度平均值t1以及大于阈值的t2
cimg_forX(hist, i) {
2.OSTU法求阈值
if (i <= threshold) {
t1 += i * hist(i);
num1 += hist(i);
}
else {
t2 += i * hist(i);
num2 += hist(i);
}
}
if (num1 == 0 || num2 == 0)
continue;
t1 /= num1;
t2 /= num2;
threshold_new = (t1 + t2) / 2;
// 若两个阈值相等,则返回阈值threshold,否则更新阈值继续循环
if (threshold == threshold_new) break;
else threshold = threshold_new;
}
cout << "threshold = " << threshold << endl;
}
//OSTU法求阈值
void ImageSegmentation::get_thres_ostu() {
//定义类间方差
double variance = 0.0;
CImg<int> image = blurImg;
CImg<int> hist = image.histogram(256, 0, 255);
int size = blurImg.size();
for (int i = 0; i < 256; i++) {
//定义前景图,背景图的像素点所占比例以及平均灰度
double p1 = 0.0, p2 = 0.0, g1 = 0.0, g2 = 0.0;
cimg_forX(hist, j) {
if (j <= i) {
p1 += hist(j);
g1 += j*hist(j);
}
else {
p2 += hist(j);
g2 += j*hist(j);
}
}
if (p1 == 0 || p2 == 0)
continue;
g1 /= p1;
p1 /= size;
g2 /= p2;
p2 /= size;
double temp_variance = p1 * p2 * (g1 - g2) * (g1 - g2);
if (variance < temp_variance) {
variance = temp_variance;
(4)按阈值进行图像分割
(5) 对分割后的图像进行梯度检测
PS:如果此时直接进行矫正或者膨胀后膨胀效果不好,所以我们检测一步梯度(采用sobel算子)
(6) 霍夫变换
1. 统计霍夫空间点
threshold = i;
}
}
cout << "threshold = " << threshold << endl;
}
void ImageSegmentation::Segmentation() {
segImg.resize(Img._width, Img._height, 1, 1, 0);
cimg_forXY(blurImg, x, y) {
if (blurImg(x,y) > threshold) {
segImg(x,y) = 0;
}
else
segImg(x,y) = 255;
}
//segImg.display();
}
void ImageSegmentation::gradDection() {
gradImg.resize(segImg._width, segImg._height, 1, 1, 0);
CImg_3x3(I, double);
cimg_for3x3(segImg, x, y, 0, 0, I, double) {
const double ix = (Inn + 2 * Icn + Ipn) - (Ipp + 2 * Icp + Inp);
const double iy = (Inp + 2 * Inc + Inn) - (Ipp + 2 * Ipc + Ipn);
double grad = sqrt(ix * ix + iy * iy);
if (grad > 255) grad = 255;
if (grad < 0 ) grad = 0;
gradImg(x, y) = grad;
}
gradImg.display();
}
void ImageSegmentation::Hough_Statistics() {
//double maxDistance = sqrt(Img._width*Img._width + Img._height*Img._height);
double w = Img._width;
double h = Img._height;
double center_x = w/2;
double center_y = h/2;
double hough_h = ((sqrt(2.0) * (double)(h>w?h:w)) / 2.0);
2. 获取对应检测出的直线
houghImg.resize(180, hough_h * 2, 1, 1, 0);
cimg_forXY(gradImg, x, y) {
if (gradImg(x,y) != 0) {
cimg_forX(houghImg, angle) {
double _angle = (double)PI*angle / 180.0f;
int polar = (int)((((double)x - center_x)*cos(_angle) + ((double)y -
center_y)*sin(_angle)) + hough_h);
//cout << polar << endl;
houghImg(angle, polar) += 1;
}
}
}
//houghImg.display();
}
void ImageSegmentation::GetLine() {
resultImg = Img;
//剔除掉可能出现的重合线,方法是取9x9空间内的霍夫最大值
int hough_h = houghImg._height;
//int hough_w = houghImg._width;
int img_h = Img._height;
int img_w = Img._width;
const int y_min = 0;
const int y_max = Img._height - 1;
const int x_min = 0;
const int x_max = Img._width - 1;
cimg_forXY(houghImg, angle, polar) {
if (houghImg(angle, polar) >= Min_thres) {
int max = houghImg(angle, polar);
for(int ly=-DIFF;ly<=DIFF;ly++) {
for(int lx=-DIFF;lx<=DIFF;lx++) {
if( (ly+polar>=0 && ly+polar<houghImg._height) && (lx+angle>=0 &&
lx+angle<houghImg._width) ) {
if( (int)houghImg(angle + lx, polar + ly ) > max ) {
max = houghImg(angle + lx, polar + ly );
ly = lx = DIFF + 1;
}
}
}
}
if (max > (int)houghImg(angle, polar) )
continue;
peaks.push_back(pair< pair<int, int>, int >(pair<int, int>(angle, polar),
houghImg(angle, polar)));
}
}
sort(peaks.begin(), peaks.end(), [](const pair< pair<int, int>, int > &a, const pair<
pair<int, int>, int > &b) -> int {return a.second > b.second ;});
for (int i = 0; lines.size() != 4; i++) {
int angle = peaks[i].first.first;
3. 获得四个角点
int polar = peaks[i].first.second;
//cout << angle << endl << polar << endl;
int x1, y1, x2, y2;
x1 = y1 = x2 = y2 = 0;
double _angle = (double)PI*angle / 180.0f;
if(angle >= 45 && angle <= 135) {
x1 = 0;
y1 = ((double)(polar-(hough_h/2)) - ((x1 - (img_w/2) ) * cos(_angle))) / sin(_angle)
+ (img_h / 2);
x2 = img_w;
y2 = ((double)(polar-(hough_h/2)) - ((x2 - (img_w/2) ) * cos(_angle))) / sin(_angle)
+ (img_h / 2);
}
else {
y1 = 0;
x1 = ((double)(polar-(hough_h/2)) - ((y1 - (img_h/2) ) * sin(_angle))) / cos(_angle)
+ (img_w / 2);
y2 = img_h;
x2 = ((double)(polar-(hough_h/2)) - ((y2 - (img_h/2) ) * sin(_angle))) / cos(_angle)
+ (img_w / 2);
}
//if
bool flag = true;
for (int k = 0; k < lines.size(); k++) {
if (distance(lines[k].first.first - x1, lines[k].first.second - y1) < 100 &&
distance(lines[k].second.first - x2, lines[k].second.second - y2) < 100) {
flag = false;
break;
}
}
if (flag == true) {
lines.push_back(pair< pair<int, int>, pair<int, int> >(pair<int, int>(x1, y1),
pair<int, int>(x2, y2)));
}
}
for (int i = 0; i < lines.size(); i++) {
cout << lines[i].first.first << ", " << lines[i].first.second << " .. " <<
lines[i].second.first << ", " << lines[i].second.second << endl;
resultImg.draw_line(lines[i].first.first, lines[i].first.second, lines[i].second.first,
lines[i].second.second, Red);
}
//resultImg.draw_line(200, 3458, 2500, 3459, Red);
//resultImg.display();
}
void ImageSegmentation::GetVertexs() {
for (int i = 0; i < lines.size(); i++) {
double k0, b0;
if (lines[i].first.first == lines[i].second.first) {
k0 = DBL_MAX;
剩余30页未读,继续阅读
资源评论
吉利吉利
- 粉丝: 24
- 资源: 308
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功