【问题标题】:OpenCV - Fit a curve to a set of pointsOpenCV - 将曲线拟合到一组点
【发布时间】:2014-05-18 21:31:41
【问题描述】:

下面是我的帖子:OpenCV - Remove "white" artifacts from image and fit a curve

我通过在任何给定位置查找对角线上的点来删除图像中无关的白点。我的结果现在看起来像这样:

现在,我想将曲线拟合到图像中的其余点。我一直在浏览 OpenCV 以前的帖子,其中有些人建议 approxPolyDP。有没有更简单的拟合曲线并在 OpenCV 中显示的方法?

这里使用霍夫曲线的想法对我来说有点过于复杂了,因为我的图像本质上是二进制的:http://homepages.inf.ed.ac.uk/rbf/BOOKS/BANDB/LIB/bandb4_3.pdf

最终图像是w256 x h1024的(0-255)灰度图。

编辑

我只是使用线段连接图像中的点。但是,我想通过将平滑曲线拟合到所有点来连接这些点。我已经搜索了这种方法,但找不到方法。

【问题讨论】:

  • 请参阅approxPolyDP 可能会有所帮助。
  • “平滑度”意味着 (a) 对生成这些点的过程有一些假设 - 你有它的物理模型吗?以及与它们相关的“噪声”/“错误”(它们与假设模型的偏差)? (b) 考虑曲线的一些应用 - 生成漂亮的图片来查看是一个应用,但也许您对样本和模型的插值/外推有一些特定需求?您需要同时回答 (a) 和 (b) 才能取得进展。
  • @Haris 我尝试在我的代码中使用 findContours 和 approxPolyDP。但是,由于访问冲突错误,我无法让它工作。
  • @FrancescoCallari a.没有 B.不,我只需要为这些点拟合一条曲线。

标签: c++ opencv image-processing


【解决方案1】:

我认为 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通道修改(为了紧凑)。

我想你会明白的。

#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();
}

【讨论】:

  • 但是当我们完成代码之后,有没有什么函数可以移动BGR的曲线?
  • 我不明白你的问题,请你重新表述一下。
  • 你给了我同样的建议,所以我说的是这个场景stackoverflow.com/questions/23641208/…
  • 主要的难点是什么?是鼠标控制曲线吗?
  • 是的,它是鼠标控制曲线,我希望我们可以通过鼠标改变曲线形状
猜你喜欢
  • 1970-01-01
  • 2012-06-22
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多