- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这跟在我的帖子后面:OpenCV - Remove "white" artifacts from image and fit a curve
我通过在任何给定位置寻找穿过对角线的点来移除图像中无关的白点。我的结果现在看起来像这样:
现在,我想为图像中的其余点拟合一条曲线。我一直在浏览 OpenCV 中以前的帖子,其中有些人建议使用 approxPolyDP
。有没有更简单的方法来拟合曲线并在 OpenCV 中显示它?
这里使用 Hough 曲线的想法对我来说有点过于复杂,因为我的图像本质上是二进制的:http://homepages.inf.ed.ac.uk/rbf/BOOKS/BANDB/LIB/bandb4_3.pdf
最终图像是 w256 x h1024
的 (0-255) 灰度图像。
编辑
我只是使用线段连接图像中的点。但是,我想通过为所有这些点拟合一条平滑曲线来连接这些点。我已经在 SO 中搜索过这样的方法,但找不到方法。
最佳答案
我认为 catmul-rom spline 将是您完成任务的不错选择。
你可以在这里找到实现: http://www.codeproject.com/Articles/30838/Overhauser-Catmull-Rom-Splines-for-Camera-Animatio
对 AHF 评论的回答(根据 Qix 的建议放在此处):
此代码草图允许您使用鼠标编辑样条曲线,它使用来自 this link 的文件(附加到您的项目:overhauser.cpp overhauser.hpp 和 vec3.hpp):
鼠标左键添加/移动一个点,右键删除。
代码不是很难,所以我做了1 channel 的修改(为了简洁)。
我猜你会明白这个想法。
#include <iostream>
#include <vector>
#include <stdio.h>
#include <functional>
#include <algorithm>
#include <numeric>
#include <cstddef>
#include "opencv2/opencv.hpp"
#include <iostream>
#include <fstream>
#include "overhauser.hpp"
using namespace std;
using namespace cv;
Mat result;
Mat Img;
vector<cv::Point2f> pts;
Mat curvesImg;
int selectedPt=-1;
CRSpline* spline = 0;
unsigned char LUT_RED[256];
// a case-insensitive comparison function:
bool mycomp (Point2f p1, Point2f p2)
{
return p1.x<p2.x;
}
float dist(Point2f p1,Point2f p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
int findNEarestPt(Point2f pt, float maxDist)
{
float minDist=FLT_MAX;
int ind=-1;
for(int i=0;i<pts.size();++i)
{
float d=dist(pt,pts[i]);
if(minDist>d)
{
ind=i;
minDist=d;
}
}
if(minDist>maxDist)
{
ind=-1;
}
return ind;
}
float F(float t,float x)
{
vec3 rv = spline->GetInterpolatedSplinePoint(t);
return x-rv.x;
}
float solveForX(float x)
{
float a=0,b=1.0,c,e=1e-5;
c=(a+b)/2;
while( (fabs(b-a)>e) && (F(c,x)!=0) )
{
if (F(a,x)*F(c,x)<0)
{
b=c;
}
else
{
a=c;
}
c=(a+b)/2;
}
return c;
}
int ind=-1;
void mouseHandler(int event, int x, int y, int flags, void* param)
{
Point2f m;
m.x=x;
m.y=y;
curvesImg=Scalar(0,0,0);
switch (event)
{
case cv::EVENT_RBUTTONDOWN:
ind=findNEarestPt(m,5);
if (ind==-1)
{
}else
{
pts.erase(pts.begin()+ind);
ind=-1;
}
break;
case cv::EVENT_LBUTTONDOWN:
ind=findNEarestPt(m,5);
if (ind==-1)
{
pts.push_back(m);
selectedPt=pts.size()-1;
}else
{
selectedPt=ind;
}
break;
case cv::EVENT_MOUSEMOVE:
if(ind!=-1)
{
pts[selectedPt].x=m.x;
pts[selectedPt].y=m.y;
}
break;
case cv::EVENT_LBUTTONUP:
ind=-1;
break;
}
std::sort(pts.begin(),pts.end(),mycomp);
if(pts.size()>0)
{
pts[pts.size()-1].x=curvesImg.cols;
pts[0].x=0;
}
for(int i=0;i<pts.size();++i)
{
circle(curvesImg,pts[i],5,Scalar(0,255,255),-1,CV_AA);
}
if (spline) {delete spline;}
spline = new CRSpline();
for (int i=0;i<pts.size();++i)
{
vec3 v(pts[i].x,pts[i].y,0);
spline->AddSplinePoint(v);
}
vec3 rv_last(0,0,0);
if(pts.size()>2)
{
for(int i=0;i<256;++i)
{
float t=solveForX(i);
vec3 rv = spline->GetInterpolatedSplinePoint(t);
unsigned char I=(unsigned char)(rv.y);
LUT_RED[i]=255-I;
if(i>1)
{
line(curvesImg,Point(rv.x,rv.y),Point(rv_last.x,rv_last.y),Scalar(0,0,255),1);
}
rv_last=rv;
}
}
line(curvesImg,Point(0,m.y),Point(curvesImg.cols,m.y),Scalar(0,255,0),1);
line(curvesImg,Point(m.x,0),Point(m.x,curvesImg.rows),Scalar(0,255,0),1);
imshow("Result",curvesImg);
vector<Mat> ch;
cv::split(Img,ch);
LUT(ch[2],Mat(256,1,CV_8UC1,LUT_RED),ch[2]);
cv::merge(ch,result);
imshow("Transformed",result);
}
// ---------------------------------
//
// ---------------------------------
//==============================================================================
int main( int argc, char** argv )
{
for (int i=0;i<256;++i)
{
LUT_RED[i]=i;
}
namedWindow("Image",cv::WINDOW_NORMAL);
namedWindow("Result");
namedWindow("Transformed");
Img=imread("D:\\ImagesForTest\\lena.jpg",1);
imshow("Image",Img);
curvesImg=Mat::zeros(256,256,CV_8UC3);
setMouseCallback("Result", mouseHandler, NULL);
waitKey(0);
getchar();
}
关于c++ - OpenCV - 将曲线拟合到一组点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22898881/
gnuplot 中拟合函数的正确方法是什么 f(x)有下一个表格吗? f(x) = A*exp(x - B*f(x)) 我尝试使用以下方法将其拟合为任何其他函数: fit f(x) "data.txt
(1)首先要建立数据集 ? 1
测量显示一个信号,其形式类似于具有偏移量和因子的平方根函数。如何找到系数并在一个图中绘制原始数据和拟合曲线? require(ggplot2) require(nlmrt) # may be thi
我想将以下函数拟合到我的数据中: f(x) = Offset+Amplitudesin(FrequencyT+Phase), 或根据 Wikipedia : f(x) = C+alphasin(ome
我正在尝试使用与此工具相同的方法在 C# 中拟合 Akima 样条曲线:https://www.mycurvefit.com/share/4ab90a5f-af5e-435e-9ce4-652c95c
问题:开放层适合 map ,只有在添加特征之后(视觉),我该如何避免这种情况? 我在做这个 第 1 步 - 创建特征 var feature = new ol.Feature({...}); 第 2
我有一个数据变量,其中包含以下内容: [Object { score="2.8", word="Blue"}, Object { score="2.8", word="Red"}, Objec
我正在尝试用中等大小的 numpy float 组来填充森林 In [3]: data.shape Out[3]: (401125, 5) [...] forest = forest.fit(data
我想用洛伦兹函数拟合一些数据,但我发现当我使用不同数量级的参数时拟合会出现问题。 这是我的洛伦兹函数: function [ value ] = lorentz( x,x0,gamma,amp )
我有一些数据,我希望对其进行建模,以便能够在与数据相同的范围内获得相对准确的值。 为此,我使用 polyfit 来拟合 6 阶多项式,由于我的 x 轴值,它建议我将其居中并缩放以获得更准确的拟合。 但
我一直在寻找一种方法来使数据符合 beta 二项分布并估计 alpha 和 beta,类似于 VGAM 库中的 vglm 包的方式。我一直无法找到如何在 python 中执行此操作。有一个 scipy
我将 scipy.optimize.minimize ( https://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html ) 函数与
在过去的几天里,我一直在尝试使用 python 绘制圆形数据,方法是构建一个范围从 0 到 2pi 的圆形直方图并拟合 Von Mises 分布。我真正想要实现的是: 具有拟合 Von-Mises 分
我有一个简单的循环,它在每次迭代中都会创建一个 LSTM(具有相同的参数)并将其拟合到相同的数据。问题是迭代过程中需要越来越多的时间。 batch_size = 10 optimizer = opti
我有一个 Python 系列,我想为其直方图拟合密度。问题:是否有一种巧妙的方法可以使用 np.histogram() 中的值来实现此结果? (请参阅下面的更新) 我目前的问题是,我执行的 kde 拟
我有一个简单的 keras 模型(正常套索线性模型),其中输入被移动到单个“神经元”Dense(1, kernel_regularizer=l1(fdr))(input_layer) 但是权重从这个模
我正在尝试解决 Boston Dataset 上的回归问题在random forest regressor的帮助下.我用的是GridSearchCV用于选择最佳超参数。 问题一 我是否应该将 Grid
使用以下函数,可以在输入点 P 上拟合三次样条: def plotCurve(P): pts = np.vstack([P, P[0]]) x, y = pts.T i = np.aran
我有 python 代码可以生成数字 x、y 和 z 的三元组列表。我想使用 scipy curve_fit 来拟合 z= f(x,y)。这是一些无效的代码 A = [(19,20,24), (10,
我正在尝试从 this answer 中复制代码,但是我在这样做时遇到了问题。我正在使用包 VGAM 中的gumbel 发行版和 fitdistrplus . 做的时候出现问题: fit = fi
我是一名优秀的程序员,十分优秀!