Image Compression(DFS)

该博客讨论了一种基于图像区域分解的无损和有损图像压缩策略。使用深度优先搜索来创建表示图像的树结构,当区域颜色达到阈值百分比时停止分解。博客提供了示例和输入输出说明,解释了如何根据阈值压缩和解压图像。
摘要由CSDN通过智能技术生成

Description

 Strategies for compressing two-dimensional images are often based on finding regions with high similarity. In this problem, we explore a particular approach based on a hierarchical decomposition of the image. For simplicity, we consider only bitmapped images such as the one on the right:

    The image is encoded as a tree, with the root representing the entire image region. If a region is monochromatic, then the node for that region is a leaf storing the color of the region. Otherwise, the region is divided into four parts about its center, and the approach is applied recursively to each quadrant. For a non-leaf node, its four children represent the four quadrants ordered as upper-right, upper-left, lower-left, lower-right respectively.   

                                                                                                                                                       

     As an example, on the right is the tree encoding of the above image.               

    The original image is not monochromatic, so we considered the four quadrants. The top-right quadrant is monochromatic white, so the first child of the root node is a leaf with value 0. The top-left quadrant is not monochromatic, so it is further divided into four subquadrants, each of which is trivially monochromatic. This results in the subtree with leaf values 0, 0, 1, 0. The final two quadrants are monochromatic with respective values 0 and 1.      

                                                                   

       As a larger example, here is an 8 × 8 image and the tree encoding of it.

                         

     Thus far we have described a lossless compression scheme, but the approach can be used for lossy compression with the following adjustment. Instead of continuing the decomposition until reaching a monochromatic region, a threshold such as 75% is used, and a leaf is created whenever a region has at least that percentage of either color. As an example, here is the encoding of the above 8 × 8 image if using 75% as the threshold.

                                  

   Notice that 75% of the top-left quadrant of the full image is black, and therefore the second child of the root is 1, and that more than 75% of the bottom-left quadrant of the full image is white, and therefore the third child of the root is 0. However, neither white nor black reaches 75% in the top-right quadrant, so the recursive decomposition continues, but all four of those subquadrants achieve the 75% threshold and become leaves. If we were to uncompress the image based on this new lossy encoding, we get back the following result.

                                                                          

 Input

    The input will consist of a series of data sets, followed by a line containing only ‘0’. Each data set begins with a line containing values W and T, where W is the width of the bitmap and T is the threshold percentage. Images will always be square with 1 ≤ W ≤ 64 being a power of two. Threshold T will be an integer with 51 ≤ T ≤ 100. Following the specification of W and T are W additional lines, each of which is a string of width W containing only characters ‘0’ and ‘1’, representing a row of the image bitmap, from top to bottom.

Output

    For each data set, you should print an initial line of the form ‘Image #:’ numbering the images starting with 1. Following that should be W lines, with each line representing a row of the resulting bitmap as a string of characters ‘0’ and ‘1’, from top to bottom.

Sample Input

4 80

0000

1000

0011

0011

8 75

11111000

11110000

11000011

11000011

11000100

00000100

00010011

00010011

4 75

1101

1111

0111

0011

0

Sample Output

Image 1:

0000

1000

0011

0011

Image 2:

11110000

11110000

11110011

11110011

00000100

00000100

00000011

00000011

Image 3:

1111

1111

1111

1111

解析

题意大致是给定一个w*w的矩阵,每个点用0、1表示,判断若0(1)占有的百分比大于或等于题目给出的比率,就将当前矩阵所有点改成0(1),若小于就将当前矩阵四分,然后从子矩阵判断,执行相同操作,直到判断出符合条件为止。用DFS,只是入口有限制,即每次要判断不符合条件才能进行DFS(看了大佬的代码)

代码

#include<stdio.h>
#define MAX 80
char img[MAX][MAX],op[MAX][MAX];
int T;
int judge(int x1,int x2,int y1,int y2,int n)
{
    int i,j,total=0;
    for(i=x1-1;i<x2;i++)
    {
        for(j=y1-1;j<y2;j++)
        {
            if(img[i][j]=='0')
                total+=1;
        }
    }
    if(total*100>=n*T)
    {
        for(i=x1-1;i<x2;i++)
        {
            for(j=y1-1;j<y2;j++)
            {
                op[i][j]='0';
            }
        }
        return 1;
    }
    else if((n-total)*100>=n*T)
    {
        for(i=x1-1;i<x2;i++)
        {
            for(j=y1-1;j<y2;j++)
            {
                op[i][j]='1';
            }
        }
        return 1;
    }
    else
        return 0;
}
void solve(int x1,int x2,int y1,int y2,int n)
{
    if(!judge(x1,x2,y1,y2,n))
    {
        solve(x1,(x1+x2)/2,y1,(y1+y2)/2,n/4);
        solve((x1+x2)/2+1,x2,y1,(y1+y2)/2,n/4);
        solve(x1,(x1+x2)/2,(y1+y2)/2+1,y2,n/4);
        solve((x1+x2)/2+1,x2,(y1+y2)/2+1,y2,n/4);
    }
}
int main()
{
    int n,i,kase=1;
    while(~scanf("%d",&n))
    {
        if(n==0) break;
        scanf("%d",&T);
        for(i=0;i<n;i++)
        {
            scanf("%s",img[i]);
        }
        solve(1,n,1,n,n*n);
        printf("Image %d:\n",kase++);
        for(i=0;i<n;i++)
        {
            op[i][n]='\0';
            printf("%s\n",op[i]);
        }
    }
    return 0;
}
5-03-20 00:25:24,075 INFO metrics.TopMetrics: NNTop conf: dfs.namenode.top.windows.minutes = 1,5,25 2025-03-20 00:25:24,081 INFO namenode.FSNamesystem: Retry cache on namenode is enabled 2025-03-20 00:25:24,081 INFO namenode.FSNamesystem: Retry cache will use 0.03 of total heap and retry c ache entry expiry time is 600000 millis2025-03-20 00:25:24,085 INFO util.GSet: Computing capacity for map NameNodeRetryCache 2025-03-20 00:25:24,085 INFO util.GSet: VM type = 64-bit 2025-03-20 00:25:24,085 INFO util.GSet: 0.029999999329447746% max memory 405.5 MB = 124.6 KB 2025-03-20 00:25:24,085 INFO util.GSet: capacity = 2^14 = 16384 entries 2025-03-20 00:25:24,293 INFO namenode.FSImage: Allocated new BlockPoolId: BP-241692134-192.168.3.111-17 424015242842025-03-20 00:25:24,312 INFO common.Storage: Storage directory /data/namenode has been successfully for matted.2025-03-20 00:25:24,347 INFO namenode.FSImageFormatProtobuf: Saving image file /data/namenode/current/f simage.ckpt_0000000000000000000 using no compression2025-03-20 00:25:24,571 INFO namenode.FSImageFormatProtobuf: Image file /data/namenode/current/fsimage. ckpt_0000000000000000000 of size 399 bytes saved in 0 seconds .2025-03-20 00:25:24,586 INFO namenode.NNStorageRetentionManager: Going to retain 1 images with txid >= 02025-03-20 00:25:24,600 INFO blockmanagement.DatanodeManager: Slow peers collection thread shutdown 2025-03-20 00:25:24,604 INFO namenode.FSNamesystem: Stopping services started for active state 2025-03-20 00:25:24,605 INFO namenode.FSNamesystem: Stopping services started for standby state 2025-03-20 00:25:24,609 INFO namenode.FSImage: FSImageSaver clean checkpoint: txid=0 when meet shutdown .2025-03-20 00:25:24,609 INFO namenode.NameNode: SHUTDOWN_MSG: /************************************************************ SHUTDOWN_MSG: Shutting down NameNode at node11/192.168.3.111 ************************************************************/ [root@node11 ~]# start-dfs.sh Starting namenodes on [
最新发布
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值