zoukankan      html  css  js  c++  java
  • matlab练习程序(生成希尔伯特曲线)

    能够使用这样一条线遍历图像中所有的像素,不过这里没有这样做,而只是生成了这样一条曲线。

    程序中h,w是最终图像的高和宽,n为希尔伯特曲线阶数。

    这里如果n等于log2(h)或log2(w),则图像就全为白了,也算是正好遍历所有像素了。

    当然,n很大的话,图像也是全为白的,不过,那样不算正好遍历吧。

    代码中生成曲线的核心函数可以在这里找到

    生成图像如下:

    matlab代码如下:

    main.m

    clear all;close all;clc;
    
    h=256;
    w=256;
    n=5;   
    imgn=zeros(h,w);
    [x,y]=hilbert(n);       
    x=floor((x+0.5)*w)+1;
    y=floor((y+0.5)*h)+1;
    
    l=length(x);
    for i=1:l-1
        imgn=drawline(imgn,x(i),y(i),x(i+1),y(i+1));  
    end
    
    imshow(imgn)

    hilbert.m

    function [x,y] = hilbert(n)
    %HILBERT Hilbert curve.
    %
    % [x,y]=hilbert(n) gives the vector coordinates of points
    %   in n-th order Hilbert curve of area 1.
    %
    % Example: plot of 5-th order curve
    %
    % [x,y]=hilbert(5);line(x,y)
    %
    
    %   Copyright (c) by Federico Forte
    %   Date: 2000/10/06 
    
    if n<=0
      x=0;
      y=0;
    else
      [xo,yo]=hilbert(n-1);
      x=.5*[-.5+yo -.5+xo .5+xo  .5-yo];
      y=.5*[-.5+xo  .5+yo .5+yo -.5-xo];
    end

    drawline.m

    function img=drawline(img,x1,y1,x2,y2)   %因为图像坐标和数学函数坐标y轴朝向相反,所以这里所有y变量取相反数
        y1=-y1;
        y2=-y2;
        
        if x1~=x2
            k=(y2-y1)/(x2-x1);
            b=y1-k*x1;
    
            mi=min(x1,x2);
            ma=max(x1,x2);
            for i=mi:ma
                img(-round(i*k+b),i)=1; 
            end
        end
        
        if y1~=y2
            k=(x2-x1)/(y2-y1);
            b=x1-k*y1;  
            mi=min(y1,y2);
            ma=max(y1,y2);
            for i=mi:ma
                img(-i,round(i*k+b))=1; 
            end        
        end
     
    end
  • 相关阅读:
    React 之使用 fetch
    react-native 环境搭建
    create-react-app 配置 less
    React新的前端思维方式
    字体图标 —— IconMoon
    你不知道的javascript 之 >>
    前端的自我修养
    jquery 学习
    html的meta总结
    git基本操作 nginx基本操作
  • 原文地址:https://www.cnblogs.com/tiandsp/p/3946375.html
Copyright © 2011-2022 走看看