我正在寻找处理语言中的Canny Edge Detection的复制粘贴实现.我对图像处理没有任何想法,关于Processing的知识很少,虽然我很了解 java.

有些加工专家可以告诉我是否有办法在处理过程中实施这个http://www.tomgibara.com/computer-vision/CannyEdgeDetector.java?

解决方法

我想如果你用Java处理处理,那么一些问题可以很容易地解决.这意味着您可以使用java类.

对于演示,我使用的是您共享的implementation.

>>原始图片

>>更改了图片

>>代码

import java.awt.image.BufferedImage;
import java.util.Arrays;

PImage orig;
PImage changed;

void setup() {
  orig = loadImage("c:/temp/image.png");
  size(250,166);

  CannyEdgeDetector detector = new CannyEdgeDetector();

  detector.setLowThreshold(0.5f);
  detector.setHighThreshold(1f);

   detector.setSourceImage((java.awt.image.BufferedImage)orig.getimage());
   detector.process();
   BufferedImage edges = detector.getEdgesImage();
   changed = new PImage(edges);
  noLoop();
}

void draw() 
{
  //image(orig,width,height);

  image(changed,height);
}

// The code below is taken from "http://www.tomgibara.com/computer-vision/CannyEdgeDetector.java" 
// I have stripped the comments for conciseness

public class CannyEdgeDetector {

    // statics

    private final static float GAUSSIAN_CUT_OFF = 0.005f;
    private final static float MAGNITUDE_SCALE = 100F;
    private final static float MAGNITUDE_LIMIT = 1000F;
    private final static int MAGNITUDE_MAX = (int) (MAGNITUDE_SCALE * MAGNITUDE_LIMIT);

    // fields

    private int height;
    private int width;
    private int picsize;
    private int[] data;
    private int[] magnitude;
    private BufferedImage sourceImage;
    private BufferedImage edgesImage;

    private float gaussianKernelRadius;
    private float lowThreshold;
    private float highThreshold;
    private int gaussianKernelWidth;
    private boolean contrastnormalized;

    private float[] xConv;
    private float[] yConv;
    private float[] xGradient;
    private float[] yGradient;

    // constructors

    /**
     * Constructs a new detector with default parameters.
     */

    public CannyEdgeDetector() {
        lowThreshold = 2.5f;
        highThreshold = 7.5f;
        gaussianKernelRadius = 2f;
        gaussianKernelWidth = 16;
        contrastnormalized = false;
    }



    public BufferedImage getSourceImage() {
        return sourceImage;
    }


    public void setSourceImage(BufferedImage image) {
        sourceImage = image;
    }


    public BufferedImage getEdgesImage() {
        return edgesImage;
    }


    public void setEdgesImage(BufferedImage edgesImage) {
        this.edgesImage = edgesImage;
    }


    public float getLowThreshold() {
        return lowThreshold;
    }


    public void setLowThreshold(float threshold) {
        if (threshold < 0) throw new IllegalArgumentException();
        lowThreshold = threshold;
    }

    public float getHighThreshold() {
        return highThreshold;
    }


    public void setHighThreshold(float threshold) {
        if (threshold < 0) throw new IllegalArgumentException();
        highThreshold = threshold;
    }

    public int getGaussianKernelWidth() {
        return gaussianKernelWidth;
    }

    public void setGaussianKernelWidth(int gaussianKernelWidth) {
        if (gaussianKernelWidth < 2) throw new IllegalArgumentException();
        this.gaussianKernelWidth = gaussianKernelWidth;
    }

    public float getGaussianKernelRadius() {
        return gaussianKernelRadius;
    }

    public void setGaussianKernelRadius(float gaussianKernelRadius) {
        if (gaussianKernelRadius < 0.1f) throw new IllegalArgumentException();
        this.gaussianKernelRadius = gaussianKernelRadius;
    }

    public boolean isContrastnormalized() {
        return contrastnormalized;
    }

    public void setContrastnormalized(boolean contrastnormalized) {
        this.contrastnormalized = contrastnormalized;
    }

    // methods

    public void process() {
        width = sourceImage.getWidth();
        height = sourceImage.getHeight();
        picsize = width * height;
        initArrays();
        readluminance();
        if (contrastnormalized) normalizeContrast();
        computeGradients(gaussianKernelRadius,gaussianKernelWidth);
        int low = Math.round(lowThreshold * MAGNITUDE_SCALE);
        int high = Math.round( highThreshold * MAGNITUDE_SCALE);
        performHysteresis(low,high);
        thresholdEdges();
        writeEdges(data);
    }

    // private utility methods

    private void initArrays() {
        if (data == null || picsize != data.length) {
            data = new int[picsize];
            magnitude = new int[picsize];

            xConv = new float[picsize];
            yConv = new float[picsize];
            xGradient = new float[picsize];
            yGradient = new float[picsize];
        }
    }
    private void computeGradients(float kernelRadius,int kernelWidth) {

        //generate the gaussian convolution masks
        float kernel[] = new float[kernelWidth];
        float diffKernel[] = new float[kernelWidth];
        int kwidth;
        for (kwidth = 0; kwidth < kernelWidth; kwidth++) {
            float g1 = gaussian(kwidth,kernelRadius);
            if (g1 <= GAUSSIAN_CUT_OFF && kwidth >= 2) break;
            float g2 = gaussian(kwidth - 0.5f,kernelRadius);
            float g3 = gaussian(kwidth + 0.5f,kernelRadius);
            kernel[kwidth] = (g1 + g2 + g3) / 3f / (2f * (float) Math.PI * kernelRadius * kernelRadius);
            diffKernel[kwidth] = g3 - g2;
        }

        int initX = kwidth - 1;
        int maxX = width - (kwidth - 1);
        int initY = width * (kwidth - 1);
        int maxY = width * (height - (kwidth - 1));

        //perform convolution in x and y directions
        for (int x = initX; x < maxX; x++) {
            for (int y = initY; y < maxY; y += width) {
                int index = x + y;
                float sumX = data[index] * kernel[0];
                float sumY = sumX;
                int xOffset = 1;
                int yOffset = width;
                for(; xOffset < kwidth ;) {
                    sumY += kernel[xOffset] * (data[index - yOffset] + data[index + yOffset]);
                    sumX += kernel[xOffset] * (data[index - xOffset] + data[index + xOffset]);
                    yOffset += width;
                    xOffset++;
                }

                yConv[index] = sumY;
                xConv[index] = sumX;
            }

        }

        for (int x = initX; x < maxX; x++) {
            for (int y = initY; y < maxY; y += width) {
                float sum = 0f;
                int index = x + y;
                for (int i = 1; i < kwidth; i++)
                    sum += diffKernel[i] * (yConv[index - i] - yConv[index + i]);

                xGradient[index] = sum;
            }

        }

        for (int x = kwidth; x < width - kwidth; x++) {
            for (int y = initY; y < maxY; y += width) {
                float sum = 0.0f;
                int index = x + y;
                int yOffset = width;
                for (int i = 1; i < kwidth; i++) {
                    sum += diffKernel[i] * (xConv[index - yOffset] - xConv[index + yOffset]);
                    yOffset += width;
                }

                yGradient[index] = sum;
            }

        }

        initX = kwidth;
        maxX = width - kwidth;
        initY = width * kwidth;
        maxY = width * (height - kwidth);
        for (int x = initX; x < maxX; x++) {
            for (int y = initY; y < maxY; y += width) {
                int index = x + y;
                int indexN = index - width;
                int indexS = index + width;
                int indexW = index - 1;
                int indexE = index + 1;
                int indexNW = indexN - 1;
                int indexNE = indexN + 1;
                int indexSW = indexS - 1;
                int indexSE = indexS + 1;

                float xGrad = xGradient[index];
                float yGrad = yGradient[index];
                float gradMag = hypot(xGrad,yGrad);

                //perform non-maximal supression
                float nMag = hypot(xGradient[indexN],yGradient[indexN]);
                float sMag = hypot(xGradient[indexS],yGradient[indexS]);
                float wMag = hypot(xGradient[indexW],yGradient[indexW]);
                float eMag = hypot(xGradient[indexE],yGradient[indexE]);
                float neMag = hypot(xGradient[indexNE],yGradient[indexNE]);
                float seMag = hypot(xGradient[indexSE],yGradient[indexSE]);
                float swMag = hypot(xGradient[indexSW],yGradient[indexSW]);
                float nwMag = hypot(xGradient[indexNW],yGradient[indexNW]);
                float tmp;

                if (xGrad * yGrad <= (float) 0 /*(1)*/
                    ? Math.abs(xGrad) >= Math.abs(yGrad) /*(2)*/
                        ? (tmp = Math.abs(xGrad * gradMag)) >= Math.abs(yGrad * neMag - (xGrad + yGrad) * eMag) /*(3)*/
                            && tmp > Math.abs(yGrad * swMag - (xGrad + yGrad) * wMag) /*(4)*/
                        : (tmp = Math.abs(yGrad * gradMag)) >= Math.abs(xGrad * neMag - (yGrad + xGrad) * nMag) /*(3)*/
                            && tmp > Math.abs(xGrad * swMag - (yGrad + xGrad) * sMag) /*(4)*/
                    : Math.abs(xGrad) >= Math.abs(yGrad) /*(2)*/
                        ? (tmp = Math.abs(xGrad * gradMag)) >= Math.abs(yGrad * seMag + (xGrad - yGrad) * eMag) /*(3)*/
                            && tmp > Math.abs(yGrad * nwMag + (xGrad - yGrad) * wMag) /*(4)*/
                        : (tmp = Math.abs(yGrad * gradMag)) >= Math.abs(xGrad * seMag + (yGrad - xGrad) * sMag) /*(3)*/
                            && tmp > Math.abs(xGrad * nwMag + (yGrad - xGrad) * nMag) /*(4)*/
                    ) {
                    magnitude[index] = gradMag >= MAGNITUDE_LIMIT ? MAGNITUDE_MAX : (int) (MAGNITUDE_SCALE * gradMag);
                    //NOTE: The orientation of the edge is not employed by this
                    //implementation. It is a simple matter to compute it at
                    //this point as: Math.atan2(yGrad,xGrad);
                } else {
                    magnitude[index] = 0;
                }
            }
        }
    }

    private float hypot(float x,float y) {
        return (float) Math.hypot(x,y);
    }

    private float gaussian(float x,float sigma) {
        return (float) Math.exp(-(x * x) / (2f * sigma * sigma));
    }

    private void performHysteresis(int low,int high) {

        Arrays.fill(data,0);

        int offset = 0;
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (data[offset] == 0 && magnitude[offset] >= high) {
                    follow(x,y,offset,low);
                }
                offset++;
            }
        }
    }

    private void follow(int x1,int y1,int i1,int threshold) {
        int x0 = x1 == 0 ? x1 : x1 - 1;
        int x2 = x1 == width - 1 ? x1 : x1 + 1;
        int y0 = y1 == 0 ? y1 : y1 - 1;
        int y2 = y1 == height -1 ? y1 : y1 + 1;

        data[i1] = magnitude[i1];
        for (int x = x0; x <= x2; x++) {
            for (int y = y0; y <= y2; y++) {
                int i2 = x + y * width;
                if ((y != y1 || x != x1)
                    && data[i2] == 0 
                    && magnitude[i2] >= threshold) {
                    follow(x,i2,threshold);
                    return;
                }
            }
        }
    }

    private void thresholdEdges() {
        for (int i = 0; i < picsize; i++) {
            data[i] = data[i] > 0 ? -1 : 0xff000000;
        }
    }

    private int luminance(float r,float g,float b) {
        return Math.round(0.299f * r + 0.587f * g + 0.114f * b);
    }

    private void readluminance() {
        int type = sourceImage.getType();
        if (type == BufferedImage.TYPE_INT_RGB || type == BufferedImage.TYPE_INT_ARGB) {
            int[] pixels = (int[]) sourceImage.getData().getDataElements(0,height,null);
            for (int i = 0; i < picsize; i++) {
                int p = pixels[i];
                int r = (p & 0xff0000) >> 16;
                int g = (p & 0xff00) >> 8;
                int b = p & 0xff;
                data[i] = luminance(r,g,b);
            }
        } else if (type == BufferedImage.TYPE_BYTE_GRAY) {
            byte[] pixels = (byte[]) sourceImage.getData().getDataElements(0,null);
            for (int i = 0; i < picsize; i++) {
                data[i] = (pixels[i] & 0xff);
            }
        } else if (type == BufferedImage.TYPE_USHORT_GRAY) {
            short[] pixels = (short[]) sourceImage.getData().getDataElements(0,null);
            for (int i = 0; i < picsize; i++) {
                data[i] = (pixels[i] & 0xffff) / 256;
            }
        } else if (type == BufferedImage.TYPE_3BYTE_BGR) {
            byte[] pixels = (byte[]) sourceImage.getData().getDataElements(0,null);
            int offset = 0;
            for (int i = 0; i < picsize; i++) {
                int b = pixels[offset++] & 0xff;
                int g = pixels[offset++] & 0xff;
                int r = pixels[offset++] & 0xff;
                data[i] = luminance(r,b);
            }
        } else {
            throw new IllegalArgumentException("Unsupported image type: " + type);
        }
    }

    private void normalizeContrast() {
        int[] histogram = new int[256];
        for (int i = 0; i < data.length; i++) {
            histogram[data[i]]++;
        }
        int[] remap = new int[256];
        int sum = 0;
        int j = 0;
        for (int i = 0; i < histogram.length; i++) {
            sum += histogram[i];
            int target = sum*255/picsize;
            for (int k = j+1; k <=target; k++) {
                remap[k] = i;
            }
            j = target;
        }

        for (int i = 0; i < data.length; i++) {
            data[i] = remap[data[i]];
        }
    }

    private void writeEdges(int pixels[]) {
        if (edgesImage == null) {
            edgesImage = new BufferedImage(width,BufferedImage.TYPE_INT_ARGB);
        }
        edgesImage.getWritableTile(0,0).setDataElements(0,pixels);
    }

}

java – 使用Processing的Canny边缘检测的更多相关文章

  1. python数字图像处理之边缘轮廓检测

    这篇文章主要介绍了python数字图像处理之边缘轮廓检测示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  2. 详解Python中图像边缘检测算法的实现

    这篇文章主要为大家详细介绍了python中图像边缘检测算法的原理及实现,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. 使用Squid部署代理缓存服务CentOS 7.4

    使用Squid部署代理缓存服务正向代理不仅可以让用户使用squid代理服务器上网,还可以基于IP地址、网站关键字、下载文件后缀等实现类似行为管控的功能。然后编辑Squid服务程序的主配置文件,把端口号3128修改为网站源服务器的地址和端口号,此时正向解析服务会被暂停。

  4. php json_encode返回null

    {“sEcho”:”1″,”iTotalRecords”:7521,”iTotaldisplayRecords”:”1″,”aaData”:[[”nordicCapitalBuysSiCProcessing”,”2010-06-21/nordic-capital-buys-sic-processing”,”PEHubMedia”,”Business”,”comple

  5. ubuntu下C++如何调用python程序,gdb调试C++代码

    Linux下gdb调试C++代码:http://jingyan.baidu.com/article/acf728fd464984f8e410a369.html主要ubuntu下使用C++调用Python:#python代码:(processing_module.py)C++代码:(cpp_python.cpp)g++:CMakeLists.txt:很不错的参考:http://gashero.yea

  6. 在PHP中创建基于边缘检测的图像

    我很好奇–有可能在PHP中使用:1)将图像文件发送到服务器2)过程图像=检测边缘并基于边缘创建简单的笔划3)将文件保存在服务器上/发送到用户的浏览器/等等这是一些“样本”文件;P(你可以看到它不是使用任何边缘检测启用的程序,而是手工–只是作为一个例子):http://i51.tinypic.com/5vzo0x.jpg谢谢!如果你可以使用ImageMagick,那就是--charcoalfilte

  7. java – 使用Processing的Canny边缘检测

    解决方法我想如果你用Java处理处理,那么一些问题可以很容易地解决.这意味着您可以使用java类.对于演示,我使用的是您共享的implementation.>>原始图片>>更改了图片>>代码

  8. 点击后,jQuery会阻止其他事件

    我使用jQuery将点击事件绑定到按钮和图像链接。有一种方法可以一次性防止点击发生后其他事件触发?或者我必须保留一个名为_isProcessing的全局变量,并将其设置为每个事件处理程序的true?一种方法是在元素上取消绑定,然后在准备就绪时再次绑定。另一个选择是使用元素.data()设置一个类似的标志,如$.data;完成后将其设置为false。

  9. c – 在OpenCV中边缘检测后找到最佳感兴趣区域

    我想将OCR应用到墙上的7段显示器的一些照片.我的策略如下:>隐藏图像灰度>模糊img减少假边将img限制为二进制img>应用CannyEdge检测>根据数字轮廓给出的模式设置感兴趣区域>缩放ROI和模板匹配区域如何设置投资回报率,使我的程序不必在整个图像中查找模板?

  10. c – Opencv:边缘检测,膨胀和质量绘图

    我的工作基于带有点阵的图像(图1),最终结果如图4所示.我将逐步解释我的工作.图1原始图像步骤1:检测每个对象的边缘,包括点和我想要删除的“环”以获得更好的性能.边缘检测的结果如图2所示.我使用Canny边缘探测器,但它对一些浅灰色点不起作用.我的第一个问题是如何关闭点的轮廓并尽可能减少其他噪音?

随机推荐

  1. 基于EJB技术的商务预订系统的开发

    用EJB结构开发的应用程序是可伸缩的、事务型的、多用户安全的。总的来说,EJB是一个组件事务监控的标准服务器端的组件模型。基于EJB技术的系统结构模型EJB结构是一个服务端组件结构,是一个层次性结构,其结构模型如图1所示。图2:商务预订系统的构架EntityBean是为了现实世界的对象建造的模型,这些对象通常是数据库的一些持久记录。

  2. Java利用POI实现导入导出Excel表格

    这篇文章主要为大家详细介绍了Java利用POI实现导入导出Excel表格,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. Mybatis分页插件PageHelper手写实现示例

    这篇文章主要为大家介绍了Mybatis分页插件PageHelper手写实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  4. (jsp/html)网页上嵌入播放器(常用播放器代码整理)

    网页上嵌入播放器,只要在HTML上添加以上代码就OK了,下面整理了一些常用的播放器代码,总有一款适合你,感兴趣的朋友可以参考下哈,希望对你有所帮助

  5. Java 阻塞队列BlockingQueue详解

    本文详细介绍了BlockingQueue家庭中的所有成员,包括他们各自的功能以及常见使用场景,通过实例代码介绍了Java 阻塞队列BlockingQueue的相关知识,需要的朋友可以参考下

  6. Java异常Exception详细讲解

    异常就是不正常,比如当我们身体出现了异常我们会根据身体情况选择喝开水、吃药、看病、等 异常处理方法。 java异常处理机制是我们java语言使用异常处理机制为程序提供了错误处理的能力,程序出现的错误,程序可以安全的退出,以保证程序正常的运行等

  7. Java Bean 作用域及它的几种类型介绍

    这篇文章主要介绍了Java Bean作用域及它的几种类型介绍,Spring框架作为一个管理Bean的IoC容器,那么Bean自然是Spring中的重要资源了,那Bean的作用域又是什么,接下来我们一起进入文章详细学习吧

  8. 面试突击之跨域问题的解决方案详解

    跨域问题本质是浏览器的一种保护机制,它的初衷是为了保证用户的安全,防止恶意网站窃取数据。那怎么解决这个问题呢?接下来我们一起来看

  9. Mybatis-Plus接口BaseMapper与Services使用详解

    这篇文章主要为大家介绍了Mybatis-Plus接口BaseMapper与Services使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  10. mybatis-plus雪花算法增强idworker的实现

    今天聊聊在mybatis-plus中引入分布式ID生成框架idworker,进一步增强实现生成分布式唯一ID,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部