Ex2:用CImg重写、封装给定的Canny代码,并测试(Code0)

Ex2:用CImg重写、封装给定的Canny代码,并测试(Code0)

github地址:https://github.com/linjiafengyang/ComputerVision

代码文件

用CImg重写Code0文件夹中的canny.c和canny.h为canny.cpp和canny.h,并增加一个main.cpp文件作为图像数据输入并测试。
重写过程:首先替换原来的图像输入为CImg图像输入,以及对像素点的for循环更改为CImg版本cimg_forXY(),最后把原来的C语言struct结构体改成C++类。

canny.h文件

#ifndef canny_h
#define canny_h

#include "CImg.h"
using namespace cimg_library;

class Canny
{
public:
    CImg<unsigned char> canny(CImg<unsigned char> grey, int width, int height);
    CImg<unsigned char> cannyparam(CImg<unsigned char> grey, int width, int height,
                          float lowthreshold, float highthreshold,
                          float gaussiankernelradius, int gaussiankernelwidth,  
                          int contrastnormalised);

    Canny *allocatebuffers(const CImg<unsigned char> & grey, int width, int height);
    void killbuffers(Canny *can);
    int computeGradients(Canny *can, float kernelRadius, int kernelWidth);
    void performHysteresis(Canny *can, int low, int high);
    void follow(Canny *can, int x1, int y1, int i1, int threshold);
    void normalizeContrast(CImg<unsigned char> & data, int width, int height);
    float hypotenuse(float x, float y);
    float gaussian(float x, float sigma);

private:
    CImg<unsigned char> data; /* input image */
    int width;
    int height;
    int *idata;          /* output for edges */
    int *magnitude;      /* edge magnitude as detected by Gaussians */
    float *xConv;        /* temporary for convolution in x direction */
    float *yConv;        /* temporary for convolution in y direction */
    float *xGradient;    /* gradients in x direction, as detected by Gaussians */
    float *yGradient;    /* gradients in x direction,a s detected by Gaussians */

};

#endif

canny.cpp文件

#include "canny.h"
#include <math.h>

#define ffabs(x) ( (x) >= 0 ? (x) : -(x) ) 
#define GAUSSIAN_CUT_OFF 0.005f
#define MAGNITUDE_SCALE 100.0f
#define MAGNITUDE_LIMIT 1000.0f
#define MAGNITUDE_MAX ((int) (MAGNITUDE_SCALE * MAGNITUDE_LIMIT))

/*
  Canny edge detection with default parameters
    Params: grey - the greyscale image
            width, height - image width and height
    Returns: binary image with edges as set pixels
*/
CImg<unsigned char> Canny::canny(CImg<unsigned char> grey, int width, int height)
{
    return cannyparam(grey, width, height, 2.5f, 7.5f, 2.0f, 16, 0);
}

/*
Canny edge detection with parameters passed in by user
Params: grey - the greyscale image
width, height - image dimensions
lowthreshold - default 2.5
highthreshold - default 7.5
gaussiankernelradius - radius of edge detection Gaussian, in standard deviations
(default 2.0)
gaussiankernelwidth - width of Gaussian kernel, in pixels (default 16)
contrastnormalised - flag to normalise image before edge detection (defualt 0)
Returns: binary image with set pixels as edges
*/
CImg<unsigned char> Canny::cannyparam(CImg<unsigned char> grey, int width, int height,
                                        float lowthreshold, float highthreshold,
                                        float gaussiankernelradius, int gaussiankernelwidth,
                                        int contrastnormalised)
{
    Canny *can = 0;
    CImg<unsigned
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值