四叉树——图片应用实例

四叉树——图片应用实例四叉树前言应用实例前言四元树又称四叉树是一种树状数据结构 在每一个节点上会有四个子区块 四元树常应用于二维空间数据的分析与分类 它将数据区分成为四个象限 数据范围可以是方形或矩形或其他任意形状 这种数据结构是由拉斐尔 芬科尔 RaphaelFinke 与 J L Bentley 在 1974 年发展出来 应用实例四叉树索引结构可以快速的在二维空间划分数据 下面是一张图片的四

四叉树

前言

应用实例

四叉树索引结构可以快速的在二维空间划分数据 ,下面是一张图片的四叉树结构实例代码,根据图片中心划分,拥有非1:1图片的自适应性

tree.h

class Node { 
    public: PNG *p; //the upper left pixel Node **children; //pointer to four other node int width; //当前像素区块的宽度 int height; //当前像素区块的高度 bool leaf; //是否是叶子节点,true 代表是叶子节点 int x; //当前像素区块左上角顶点像素的横坐标 int y; //当前像素区块左上角顶点像素的纵坐标 int mean_r; //Rmean int mean_g; //Gmean int mean_b; //Bmean public: int count; Node(); Node(PNG* corner, int input_width, int input_height, int x, int y); Node(Node &other); Node(Node &&other); Node& operator=(Node &other); Node& operator=(Node &&other); ~Node(); void print(); pxl *get_pxl(); }; class Tree { 
    public: Node *root; //根结点 public: Tree(); ~Tree(); Tree(Tree &other); Tree& operator=(Tree &other); void judge(int threshold); pxl *get_pxl(); void load_png(PNG *png); int internal_judge(Node* n); void internal_build(Node* n); void print(); }; 

tree.cpp

Node::Node() { 
    //TODO mean_r = 0; mean_g = 0; mean_b = 0; p = nullptr; width = 0; height = 0; x = 0; y = 0; count = 0; leaf = true; children = nullptr; } Node::Node(PNG* corner, int input_width, int input_height, int x, int y) { 
    //TODO count = 0; children = new Node*[4]; children[0] = nullptr; children[1] = nullptr; children[2] = nullptr; children[3] = nullptr; mean_r = 0; mean_g = 0; mean_b = 0; if (input_width <= 0 || input_height <= 0) { 
    p = nullptr; leaf = true; return; } p = corner; width = input_width; height = input_height; this->x = x; this->y = y; if (input_width == 1 && input_height == 1) { 
    leaf = true; mean_r = corner->get_pxl(x, y)->red; mean_g = corner->get_pxl(x, y)->green; mean_b = corner->get_pxl(x, y)->blue; } else { 
    leaf = false; } } void Tree::internal_build(Node* n) { 
    int hw = n->width >> 1; int hh = n->height >> 1; int x = n->x; int y = n->y; if (hw > 0 && hh > 0) { 
    n->children[0] = new Node(n->p, hw, hh, x, y); if (!n->children[0]->leaf) internal_build(n->children[0]); } if ((n->width - hw) > 0 && hh > 0) { 
    n->children[1] = new Node(n->p, n->width - hw, hh, x + hw, y); if (!n->children[1]->leaf) internal_build(n->children[1]); } if (hw > 0 && (n->height) > 0) { 
    n->children[2] = new Node(n->p, hw, n->height - hh, x, hh + y); if (!n->children[2]->leaf) internal_build(n->children[2]); } if ((n->width - hw) > 0 && (n->height - hh) > 0) { 
    n->children[3] = new Node(n->p, n->width - hw, n->height - hh, x + hw, hh + y); if (!n->children[3]->leaf) internal_build(n->children[3]); } int c = 0; for (int i = 0; i < 4; ++i) { 
    if (n->children[i]) { 
    ++c; n->mean_r += n->children[i]->mean_r; n->mean_g += n->children[i]->mean_g; n->mean_b += n->children[i]->mean_b; } } n->mean_r /= c; n->mean_g /= c; n->mean_b /= c; } void Tree::load_png(PNG *png) { 
    //TODO root = new Node(png, png->get_width(), png->get_height(), 0, 0); if (!root->leaf) { 
    internal_build(root); } } 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/217057.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 上午10:26
下一篇 2026年3月18日 上午10:27


相关推荐

  • Java Calendar 设置时间

    Java Calendar 设置时间Calendarcal=Calendar.getInstance();cal.setTime(newSimpleDateFormat(“yyyy-MM-ddHH时”).parse(request.getParameter(“firstTime”)));  //获取时间参数intyear=cal.get(Calendar.YEAR);intmonth=cal.

    2022年5月13日
    101
  • C++进阶

    C++进阶函数模板函数模板语法函数模板作用:建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表。语法:template<typenameT>

    2021年12月13日
    48
  • mac录屏软件推荐_mac 录屏软件

    mac录屏软件推荐_mac 录屏软件工作和生活中,我们常常需要录制电脑屏幕,例如老师和学生上网课、游戏大神分享操作技巧。那么,在Mac上有哪一些好用的录屏软件呢?作为一个工具软件重度爱好者,我整理了以下五大最好用的录屏软件,大家可以自行参考种草:一、FilmageScreenRecorderforMacFilmageScreen是一款简单易操作,功能十分强大的一站式视频软件,集屏幕录制、摄像头录制、音频录制、视频剪辑、视频格式转换为一体,可以说是真正满足你对于视频操作的所有需求。目前是只有Mac端,所以用Mac的童鞋们千万不.

    2026年3月11日
    3
  • CentOS7 下rpm安装jdk1.8「建议收藏」

    CentOS7 下rpm安装jdk1.8「建议收藏」【1】查看并卸载自带的openjdk查看系统中默认安装的jdk:rpm-qa|grepjdk卸载JDK相关文件:yum-yremovejava-1.7.0-openjdk*“*”表示卸载掉java1.7.0的所有openjdk相关文件。或者如下卸载jdk:yum-yremovejava-1.8.0-openjdk-headless-1.8.0.65-3.b17.el7.x86_64

    2022年6月7日
    58
  • Mybatis配置文件——全配置解析

    Mybatis配置文件——全配置解析为了让大家方便的使用 特意把总结放在文章开头 有帮助到你就点个关注呗配置名称配置含义配置简介包裹所有配置标签整个配置文件的顶级标签 properties 属性该标签可以引入外部配置的属性 也可以自己配置 该配置标签所在的同一个配置文件的其他配置均可以引用此配置中的属性 setting 全局配置参数用来配置一些改变运行时行为的信息 例如是否使用缓存机制 是否使用延迟加载 是否使用错误处理机制等 此项数据过于庞大 在下面细讲类型别名

    2026年3月19日
    2
  • python(62):保留两位小数[通俗易懂]

    python(62):保留两位小数[通俗易懂]转载:https://blog.csdn.net/jiandanjinxin/article/details/77752297在C/C++语言对于整形数执行除法会进行地板除(舍去小数部分)。例如

    2022年7月5日
    28

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号