signed distance field 算法
将二值图转化成signed distance field后,可以在双线性插值下实现平滑放大。
定义:
到前景的distance field:各点到最近前景点的距离。
到背景的distance field:各点到最近背景景点的距离。
则: signed distance field = 到背景的distance field - 到前景的distance field。
注:最好严格按上面定义计算signed distance field。看到有的博文中说先提取轮廓点,然后计算各点到最近轮廓点的距离,并且如果此点是前景点就将计算出的距离加+号,如果此点是背景点就将计算出的距离加-号。这样确实也得到一个signed distance field,但显然这样计算出来的signed distance field跟严格按照上面定义计算出来的结果是不一样的,对结果准确性是否造成影响不太清楚。
若按前面标准定义计算出signed distance field后,轮廓阈值应取为0,即signed distance field中大于等于0的像素复原为前景。
实际存储的时候我是求了一下signed distance field中的最大值max和最小值min,然后通过(signedDis-min)/(max-min)将signedDis映射到[0,1],并且将轮廓阈值0映射为(0-min)/(max-min),即得到了一个取值在[0,1]间的signed distance field,其轮廓阈值为(0-min)/(max-min)。
生成signed distance field的算法,开始我在这个博文(http://blog.csdn.net/tianwaifeimao/article/details/45078661)中找到一个Saito算法,它利用距离平方在x和y上可分开处理的性质提高了计算效率,虽然没有完全达到线性复杂度,但也比暴力算法快得多。算法的正确性很容易看出来,实现出来实测了一下,也没问题。
后来又在网上找到一个称为8ssedt的算法(见:http://www.codersnotes.com/algorithms/signed-distance-fields),博文中给的论文链接打不开,但给出源代码下载,代码很短能看明白,用的是与最短路径的算法相同的思路,针对问题本身的结构做了很巧妙的优化,达到了线性复杂度。(注:前述Saito算法第一步求各点在本行中的最近前景点时也可以利用8ssedt算法的思路进行优化计算)。
8ssedt算法代码如下(转自:http://www.codersnotes.com/algorithms/signed-distance-fields):
#include "SDL/sdl.h"
#include <math.h> #define WIDTH 256
#define HEIGHT 256 struct Point
{
int dx, dy; int DistSq() const { return dx*dx + dy*dy; }
}; struct Grid
{
Point grid[HEIGHT][WIDTH];
}; Point inside = { 0, 0 };
Point empty = { 9999, 9999 };
Grid grid1, grid2; Point Get( Grid &g, int x, int y )
{
// OPTIMIZATION: you can skip the edge check code if you make your grid
// have a 1-pixel gutter.
if ( x >= 0 && y >= 0 && x < WIDTH && y < HEIGHT )
return g.grid[y][x];
else
return empty;
} void Put( Grid &g, int x, int y, const Point &p )
{
g.grid[y][x] = p;
} void Compare( Grid &g, Point &p, int x, int y, int offsetx, int offsety )
{
Point other = Get( g, x+offsetx, y+offsety );
other.dx += offsetx;
other.dy += offsety; if (other.DistSq() < p.DistSq())
p = other;
} void GenerateSDF( Grid &g )
{
// Pass 0
for (int y=0;y<HEIGHT;y++)
{
for (int x=0;x<WIDTH;x++)
{
Point p = Get( g, x, y );
Compare( g, p, x, y, -1, 0 );
Compare( g, p, x, y, 0, -1 );
Compare( g, p, x, y, -1, -1 );
Compare( g, p, x, y, 1, -1 );
Put( g, x, y, p );
} for (int x=WIDTH-1;x>=0;x--)
{
Point p = Get( g, x, y );
Compare( g, p, x, y, 1, 0 );
Put( g, x, y, p );
}
} // Pass 1
for (int y=HEIGHT-1;y>=0;y--)
{
for (int x=WIDTH-1;x>=0;x--)
{
Point p = Get( g, x, y );
Compare( g, p, x, y, 1, 0 );
Compare( g, p, x, y, 0, 1 );
Compare( g, p, x, y, -1, 1 );
Compare( g, p, x, y, 1, 1 );
Put( g, x, y, p );
} for (int x=0;x<WIDTH;x++)
{
Point p = Get( g, x, y );
Compare( g, p, x, y, -1, 0 );
Put( g, x, y, p );
}
}
} int main( int argc, char* args[] )
{
if ( SDL_Init( SDL_INIT_VIDEO ) == -1 )
return 1; SDL_Surface *screen = SDL_SetVideoMode( WIDTH, HEIGHT, 32, SDL_SWSURFACE );
if ( !screen )
return 1; // Initialize the grid from the BMP file.
SDL_Surface *temp = SDL_LoadBMP( "test.bmp" );
temp = SDL_ConvertSurface( temp, screen->format, SDL_SWSURFACE );
SDL_LockSurface( temp );
for( int y=0;y<HEIGHT;y++ )
{
for ( int x=0;x<WIDTH;x++ )
{
Uint8 r,g,b;
Uint32 *src = ( (Uint32 *)( (Uint8 *)temp->pixels + y*temp->pitch ) ) + x;
SDL_GetRGB( *src, temp->format, &r, &g, &b ); // Points inside get marked with a dx/dy of zero.
// Points outside get marked with an infinitely large distance.
if ( g < 128 )
{
Put( grid1, x, y, inside );
Put( grid2, x, y, empty );
} else {
Put( grid2, x, y, inside );
Put( grid1, x, y, empty );
}
}
}
SDL_UnlockSurface( temp ); // Generate the SDF.
GenerateSDF( grid1 );
GenerateSDF( grid2 ); // Render out the results.
SDL_LockSurface( screen );
for( int y=0;y<HEIGHT;y++ )
{
for ( int x=0;x<WIDTH;x++ )
{
// Calculate the actual distance from the dx/dy
int dist1 = (int)( sqrt( (double)Get( grid1, x, y ).DistSq() ) );
int dist2 = (int)( sqrt( (double)Get( grid2, x, y ).DistSq() ) );
int dist = dist1 - dist2; // Clamp and scale it, just for display purposes.
int c = dist*3 + 128;
if ( c < 0 ) c = 0;
if ( c > 255 ) c = 255; Uint32 *dest = ( (Uint32 *)( (Uint8 *)screen->pixels + y*screen->pitch ) ) + x;
*dest = SDL_MapRGB( screen->format, c, c, c );
}
}
SDL_UnlockSurface( screen );
SDL_Flip( screen ); // Wait for a keypress
SDL_Event event;
while( true )
{
if ( SDL_PollEvent( &event ) )
switch( event.type )
{
case SDL_QUIT:
case SDL_KEYDOWN:
return true;
}
} return 0;
}
signed distance field 算法的更多相关文章
- Signed Distance Field Shadow in Unity
0x00 前言 最近读到了一个今年GDC上很棒的分享,是Sebastian Aaltonen带来的利用Ray-tracing实现一些有趣的效果的分享. 其中有一段他介绍到了对Signed Distan ...
- Signed Distance Field Technique
[Distance Field Technique] 一种小纹理高清放大的技术. A distance field is generated from a high resolution image, ...
- distance field(占坑
signed distance field https://kosmonautblog.wordpress.com/2017/05/09/signed-distance-field-rendering ...
- 扒一扒编辑距离(Levenshtein Distance)算法
最近由于工作需要,接触了编辑距离(Levenshtein Distance)算法.赶脚很有意思.最初百度了一些文章,但讲的都不是很好,读起来感觉似懂非懂.最后还是用google找到了一些资料才慢慢理解 ...
- Levenshtein distance 编辑距离算法
这几天再看 virtrual-dom,关于两个列表的对比,讲到了 Levenshtein distance 距离,周末抽空做一下总结. Levenshtein Distance 介绍 在信息理论和计算 ...
- Lucene的FuzzyQuery中用到的Levenshtein Distance(LD)算法
2019独角兽企业重金招聘Python工程师标准>>> Lucene的FuzzyQuery中用到的Levenshtein Distance(LD)算法 博客分类: java 搜索引擎 ...
- hdu 4712 Hamming Distance ( 随机算法混过了 )
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- Levenshtein Distance (编辑距离) 算法详解
编辑距离即从一个字符串变换到另一个字符串所需要的最少变化操作步骤(以字符为单位,如son到sun,s不用变,将o->s,n不用变,故操作步骤为1). 为了得到编辑距离,我们画一张二维表来理解,以 ...
- Levenshtein Distance + LCS 算法计算两个字符串的相似度
//LD最短编辑路径算法 public static int LevenshteinDistance(string source, string target) { int cell = source ...
随机推荐
- Linux下KVM虚拟机基本管理及常用命令(转)
说明:可能有重复 一.KVM的基本管理 1.查看KVM虚拟机配置文件 #Kvm虚拟机默认配置文件位置 [root@kvm qemu]# pwd /etc/libvirt/qemu [root@kvm ...
- 万里长征第二步——django个人博客(第二步 ——日志记录器)
定义日志记录器 可以在setting.py里设置日志记录器 # 自定义日志输出信息 LOGGING = { 'version': 1, 'disable_existing_loggers': True ...
- [转]ssis cannot retrieve the column code page info from the ole db provider
本文转自:http://social.msdn.microsoft.com/Forums/sqlserver/en-US/dc1a61f2-1ab8-4ed3-b85c-db6481800b50/er ...
- android使用C/C++调用SO库
有时候,我们反编译apk得到一个so库,如果直接使用这个so库的话,必须使用原来so库同样的package名字,才能用.这样人家反编译你的apk,就知道你侵犯了人家的版权.为了达到混淆的目的,我们可以 ...
- 大气散射 Aerial Perspective
http://mathinfo.univ-reims.fr/IMG/pdf/PreethamSig2003CourseNotes.pdf https://blog.csdn.net/toughbro/ ...
- Jquery事件冒泡
事件冒泡 什么是事件冒泡 在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么 ...
- 百度编辑器上传大视频报http请求错误怎么办
百度编辑器UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码,所以受到很多开放人员的青睐.但是有时 ...
- leetcode 282. 给表达式添加运算符
给定一个仅包含0-9的字符串和一个目标值,返回在数字之间添加了二元运算符(不是一元的) +.-或*之后所有能得到目标值的情况. 例如: "123", 6 -> [" ...
- Getting to grips with CakePHP’s events system
CakePHP seems to get a slightly unfavourable reputation when compared to the likes of Symfony or Zen ...
- [Android Pro] Android中全局Application的onCreate多次调用问题
一般来说Application的onCreate方法只会执行一次, 如果应用中采用多进程方式,oncreate方法会执行多次,根据不同的进程名字进行不同的初始化, 就是在application中多添加 ...