图像处理之一阶微分应用

简介: 图像处理之一阶微分应用

图像处理之一阶微分应用

一:数学背景


首先看一下一维的微分公式Δf = f(x+1) – f(x), 对于一幅二维的数字图像f(x,y)而言,需要完


成XY两个方向上的微分,所以有如下的公式:


1336886119_7825.png


分别对X,Y两个方向上求出它们的偏微分,最终得到梯度Delta F.


对于离散的图像来说,一阶微分的数学表达相当于两个相邻像素的差值,根据选择的梯度算


子不同,效果可能有所不同,但是基本原理不会变化。最常见的算子为Roberts算子,其它


常见还有Sobel,Prewitt等算子。以Roberts算子为例的X,Y的梯度计算演示如下图:

1336886163_8877.png


二:图像微分应用


图像微分(梯度计算)是图像边缘提取的重要的中间步骤,根据X,Y方向的梯度向量值,可以


得到如下两个重要参数振幅magnitude, 角度theta,计算公式如下:

1336886253_6876.png


Theta = tan-1(yGradient/xGradient)


magnitude表示边缘强度信息


theta预言边缘的方向走势。


假如对一幅数字图像,求出magnitude之后与原来每个像素点对应值相加,则图像边缘将被


大大加强,轮廓更加明显,是一个很典型的sharp filter的效果。



三:程序效果


X, Y梯度效果,及magnitude效果


1336886302_3703.png


图像微分的Sharp效果:


1336886397_8711.png


四:程序源代码

package com.process.blur.study;
 
import java.awt.image.BufferedImage;
 
// roberts operator
// X direction 1, 0
//             0,-1
// Y direction 0, 1
//        -1, 0
 
public class ImageGradientFilter extends AbstractBufferedImageOp {
  public final static int X_DIRECTION = 0;
  public final static int Y_DIRECTION = 2;
  public final static int XY_DIRECTION = 4;
  
  private boolean sharp;
  private int direction;
  
  public ImageGradientFilter() {
    direction = XY_DIRECTION; // default;
    sharp = false;
  }
  
  public boolean isSharp() {
    return sharp;
  }
 
  public void setSharp(boolean sharp) {
    this.sharp = sharp;
  }
 
  public int getDirection() {
    return direction;
  }
 
  public void setDirection(int direction) {
    this.direction = direction;
  }
 
  @Override
  public BufferedImage filter(BufferedImage src, BufferedImage dest) {
    int width = src.getWidth();
        int height = src.getHeight();
 
        if (dest == null )
          dest = createCompatibleDestImage( src, null );
 
        int[] inPixels = new int[width*height];
        int[] outPixels = new int[width*height];
        getRGB( src, 0, 0, width, height, inPixels );
        int index = 0;
        double mred, mgreen, mblue;
        int newX, newY;
        int index1, index2, index3;
        for(int row=0; row<height; row++) {
          int ta = 0, tr = 0, tg = 0, tb = 0;
          for(int col=0; col<width; col++) {
            index = row * width + col;
            
            // base on roberts operator
            newX = col + 1;
            newY = row + 1;
            if(newX > 0 && newX < width) {
              newX = col + 1;
            } else {
              newX = 0;
            }
            
            if(newY > 0 && newY < height) {
              newY = row + 1;
            } else {
              newY = 0;
            }
            index1 = newY * width + newX;
            index2 = row * width + newX;
            index3 = newY * width + col;
            ta = (inPixels[index] >> 24) & 0xff;
                tr = (inPixels[index] >> 16) & 0xff;
                tg = (inPixels[index] >> 8) & 0xff;
                tb = inPixels[index] & 0xff;
                
            int ta1 = (inPixels[index1] >> 24) & 0xff;
                int tr1 = (inPixels[index1] >> 16) & 0xff;
                int tg1 = (inPixels[index1] >> 8) & 0xff;
                int tb1 = inPixels[index1] & 0xff;
                
                int xgred = tr -tr1;
                int xggreen = tg - tg1;
                int xgblue = tb - tb1;
                
            int ta2 = (inPixels[index2] >> 24) & 0xff;
                int tr2 = (inPixels[index2] >> 16) & 0xff;
                int tg2 = (inPixels[index2] >> 8) & 0xff;
                int tb2 = inPixels[index2] & 0xff;
                
            int ta3 = (inPixels[index3] >> 24) & 0xff;
                int tr3 = (inPixels[index3] >> 16) & 0xff;
                int tg3 = (inPixels[index3] >> 8) & 0xff;
                int tb3 = inPixels[index3] & 0xff;
                
                int ygred = tr2 - tr3;
                int yggreen = tg2 - tg3;
                int ygblue = tb2 - tb3;
                
                mred = Math.sqrt(xgred * xgred + ygred * ygred);
                mgreen = Math.sqrt(xggreen * xggreen + yggreen * yggreen);
                mblue = Math.sqrt(xgblue * xgblue + ygblue * ygblue);
                if(sharp) {
                  tr = (int)(tr + mred);
                  tg = (int)(tg + mgreen);
                  tb = (int)(tb + mblue);
                  outPixels[index] = (ta << 24) | (clamp(tr) << 16) | (clamp(tg) << 8) | clamp(tb);
                } else {
                    outPixels[index] = (ta << 24) | (clamp((int)mred) << 16) | (clamp((int)mgreen) << 8) | clamp((int)mblue);
                    // outPixels[index] = (ta << 24) | (clamp((int)ygred) << 16) | (clamp((int)yggreen) << 8) | clamp((int)ygblue);
                    // outPixels[index] = (ta << 24) | (clamp((int)xgred) << 16) | (clamp((int)xggreen) << 8) | clamp((int)xgblue);
                }
                
                
          }
        }
        setRGB(dest, 0, 0, width, height, outPixels );
        return dest;
  }
 
  public static int clamp(int c) {
    if (c < 0)
      return 0;
    if (c > 255)
      return 255;
    return c;
  }
}

转载时请务必注明

相关文章
|
网络协议 Android开发 开发工具
国内常用的Android镜像下载地址(附教育网主要镜像站)
终于建了一个自己个人小站:https://huangtianyu.gitee.io,以后优先更新小站博客,欢迎进站,O(∩_∩)O~~ Android developer 最新国内镜像:http://wear.
28695 0
|
存储 Java 索引
Elastic实战:彻底解决spring-data-elasticsearch日期、时间类型数据读取报错问题
在使用spring-data-elasticsearch读取es中时间类型的数据时出现了日期转换报错,不少初学者会在这里困惑很久,所以今天我们专门来解读该问题的几种解决方案。
3127 0
Elastic实战:彻底解决spring-data-elasticsearch日期、时间类型数据读取报错问题
|
6月前
|
存储 安全 算法
从数学到 HTTPS:一篇讲透离散对数、DH/DHE/ECDHE 与 TLS 密钥交换
本文从高中指数对数出发,层层深入剖析离散对数难题如何成为HTTPS密钥交换的数学基石;详解DH、DHE(前向安全)、ECDHE(椭圆曲线+临时密钥)的演进逻辑与本质区别;最终落地位于TLS握手全流程,讲透“不安全信道如何安全协商密钥”。240字
642 0
|
9月前
|
安全 JavaScript API
HCL AppScan Standard 10.11 发布,新增功能简介
HCL AppScan Standard 10 (Windows) - Web 应用程序安全测试
495 0
HCL AppScan Standard 10.11 发布,新增功能简介
|
8月前
|
数据采集 人工智能 自然语言处理
新手必看:OpenClaw 核心技能开启方案
OpenClaw(小龙虾)Skill技能库涵盖文件管理、办公自动化、浏览器操作、系统管理及内容处理五大场景,支持自然语言指令一键执行整理、PDF处理、网页采集、邮件收发等高频任务,零门槛部署,即装即用,大幅提升办公效率。(239字)
|
7月前
|
缓存 自然语言处理 PHP
PHP8中的JIT(即时编译)原理与性能实测
PHP自1995年诞生以来,一直以“简单易用、快速开发”著称,但也长期背负着“性能不如Java/Go”的标签。
585 0
|
SQL 存储 关系型数据库
MySQL创建数据表(CREATE TABLE语句)
MySQL创建数据表(CREATE TABLE语句)
2489 0
|
机器学习/深度学习 并行计算 数据可视化
目标分类笔记(二): 利用PaddleClas的框架来完成多标签分类任务(从数据准备到训练测试部署的完整流程)
这篇文章介绍了如何使用PaddleClas框架完成多标签分类任务,包括数据准备、环境搭建、模型训练、预测、评估等完整流程。
1714 0
目标分类笔记(二): 利用PaddleClas的框架来完成多标签分类任务(从数据准备到训练测试部署的完整流程)
|
Ubuntu 开发工具 git
git 超实用教程【人人必会!】(含大厂的 git 操作规范)
git 超实用教程【人人必会!】(含大厂的 git 操作规范)
973 0
|
Java
Java中判断String类型为空和null的方法
Java中判断`String`变量是否为空或`null`需用`== null`和`.isEmpty()`。示例代码提供两种方法:`main`方法直接判断,`safeGetString`方法提供默认值。当输入为`null`或空时,返回默认值。代码包含三个测试案例,分别处理`null`、空字符串和非空非`null`字符串。
1437 0