#include <iostream>
#include <opencv2/opencv.hpp>
#include <cmath>
#define pi 3.14159265
using namespace cv;
using namespace std;
//显示图像和轮廓
void ImgShow(Mat LSF, Mat Image)
{
Mat src = (LSF < 0); //先得到二值图
Image.convertTo(Image, CV_8UC1);//转化类型
vector <vector<Point> > contours;
vector <Vec4i> hierarchy;
findContours(src, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);
drawContours(Image, contours, -1, cvScalar(255,0,0), 2);
imshow("分割结果", Image);
waitKey(100);
}
//NeumannBound条件
void NeumannBoundCond(Mat& LSF)
{
int w = LSF.cols - 1;
int h = LSF.rows - 1;
LSF.at<float>(0, 0) = LSF.at<float>(2, 2);
LSF.at<float>(h, 0) = LSF.at<float>(h - 2, 2);
LSF.at<float>(0, w) = LSF.at<float>(2, w - 2);
LSF.at<float>(h, w) = LSF.at<float>(h - 2, w - 2);
for (int i = 0; i <= w; i++)
{
LSF.at<float>(0, i) = LSF.at<float>(2, i);
LSF.at<float>(h, i) = LSF.at<float>(h - 2, i);
}
for (int i = 0; i <= h; i++)
{
LSF.at<float>(i, 0) = LSF.at<float>(i, 2);
LSF.at<float>(i, w) = LSF.at<float>(i, w - 2);
}
}
//计算矩阵的反三角函数
Mat atan(Mat LSF)
{
Mat dst(LSF.size(), LSF.type());
for (int k = 0; k < LSF.rows; k++) //遍历
{
const float* inData = LSF.ptr<float>(k);
float* outData = dst.ptr<float>(k);
for (int i = 0; i < LSF.cols; i++)
outData[i] = atan(inData[i]);
}
return dst;
}
Mat gradient_x(Mat input)
{
Mat Ix(input.size(), input.type());
for (int ncol = 0; ncol < input.cols; ncol++)
{
for (int nrow = 0; nrow < input.rows; nrow++)
{
if (ncol == 0) {
Ix.at<float>(nrow, ncol) = input.at<float>(nrow, 1) - input.at<float>(nrow, 0);
}
else if (ncol == input.cols - 1) {
Ix.at<float>(nrow, ncol) = input.at<float>(nrow, ncol) - input.at<float>(nrow, ncol - 1);
}
else
Ix.at<float>(nrow, ncol) = (input.at<float>(nrow, ncol + 1) - input.at<float>(nrow, ncol - 1)) / 2;
}
}
return Ix;
}
Mat gradient_y(Mat input)
{
Mat Iy(input.size(), input.type());
for (int nrow = 0; nrow < input.rows; nrow++)
{
for (int ncol = 0; ncol < input.cols; ncol++)
{
if (nrow == 0) {
Iy.at<float>(nrow, ncol) = input.at<float>(1, ncol) - input.at<float>(0, ncol);
}
else if (nrow == input.rows - 1) {
Iy.at<float>(nrow, ncol) = input.at<float>(nrow, ncol) - input.at<float>(nrow - 1, ncol);
}
else
Iy.at<float>(nrow, ncol) = (input.at<float>(nrow + 1, ncol) - input.at<float>(nrow - 1, ncol)) / 2;
}
}
return Iy;
}
void CV(Mat& LSF, Mat Img, float mu, float nu, float epison, float step)
{
NeumannBoundCond(LSF); //边界条件
Mat Drc = (epison / pi) / (epison*epison+ LSF.mul(LSF)); //Dirac 函数
Mat Hea = 0.5*(1 + (2 / pi)*atan(LSF/epison)); //Heaviside 函数
//计算曲率
Mat Ix, Iy;
Ix = gradient_x(LSF);
Iy = gradient_y(LSF);
Mat s;
magnitude(Ix, Iy, s);//梯度的模
Mat Nx = Ix / s;
Mat Ny = Iy / s;
Mat Nxx, Nyy;
Nxx = gradient_x(Nx);
Nyy = gradient_y(Ny);
Mat cur = Nxx + Nyy;
//长度项
Mat Length = nu*Drc.mul(cur);
//规则项
Mat Lap;
Laplacian(LSF, Lap, CV_32FC1);
Mat Penalty = mu*(Lap - cur);
//CV项
Scalar S1;
S1 = sum(Hea.mul(Img));
Scalar S2;
S2 = sum(Hea);
float C1 = S1.val[0] / S2.val[0];
Scalar S3;
S3 = sum((1 - Hea).mul(Img));
Scalar S4;
S4 = sum((1 - Hea));
float C2 = S3.val[0] / S4.val[0];
Mat CVterm = Drc.mul((-1 * (Img - C1).mul(Img - C1) + 1 * (Img - C2).mul(Img - C2)));
//三项相加
LSF = LSF + step*(Length + Penalty + CVterm);
uint i;
}
//主程序
int main()
{
Mat Img = imread("1.bmp", 0); //读入图像
Img.convertTo(Img, CV_32FC1);//转化类型
//初始轮廓
Mat LSF = Mat::ones(Img.size(), CV_32FC1);;
Rect roi(30, 30, 50, 50);
LSF(roi) = -1;
LSF = -LSF;
ImgShow(LSF, Img);
waitKey(1000);
//参数设置
float mu = 1;
float nu = 0.003 * 255 * 255;
int num = 50;
float epison = 1;
float step = 0.1;
for (int n = 0; n < num; n++)
{
CV(LSF, Img, mu, nu, epison,step);//迭代
if (n % 10 == 0)
ImgShow(LSF, Img);
}
waitKey();
//_CrtDumpMemoryLeaks();
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
CV.rar (51个子文件)
CV
CV.sdf 18.63MB
CV
CV.cpp 4KB
CV.vcxproj.user 298B
2.bmp 119KB
1.bmp 139KB
Debug
CV.tlog
4.bmp 147KB
0.bmp 271KB
CV.vcxproj.filters 943B
x64
Release
CV.tlog
CL.write.1.tlog 292B
link.command.1.tlog 1KB
CL.read.1.tlog 41KB
CV.lastbuildstate 215B
CL.command.1.tlog 614B
link.write.1.tlog 298B
CV.write.1u.tlog 570B
link.read.1.tlog 4KB
vc141.pdb 1.8MB
CV.log 2KB
CV.obj 3.33MB
Debug
CV.tlog
CL.write.1.tlog 966B
link.command.1.tlog 3KB
CL.read.1.tlog 83KB
link.delete.1.tlog 636B
CV.lastbuildstate 207B
CL.command.1.tlog 2KB
link.write.1.tlog 738B
link.read.1.tlog 7KB
vc141.pdb 2.79MB
vc140.pdb 2.34MB
CV.log 699B
vc141.idb 1.87MB
cv.obj.enc 712KB
CV.obj 691KB
vc140.idb 1.48MB
CV.vcxproj 8KB
3.bmp 389KB
.vs
CV
v15
ipch
AutoPCH
e90f2d0f8a0dda8
CV.ipch 85.31MB
CV-d6708092
CV-d4860849
CV.ipch 81.81MB
.suo 38KB
Browse.VC.db 18.89MB
v14
.suo 30KB
CV.VC.db 16.3MB
x64
Release
CV.pdb 1.99MB
CV.ipdb 224KB
CV.iobj 546KB
CV.exe 42KB
Debug
CV.pdb 804KB
lnk{AA52ABA0-D353-4155-B0BE-2D3120EC9A3F}.tmp 3.48MB
CV.exe 178KB
CV.ilk 764KB
CV.sln 1KB
共 51 条
- 1
资源评论
- zyy_learning2019-08-28不错,学习一下
dingkeyanlail
- 粉丝: 48
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功