Image Compression

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yu121380/article/details/81584519

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

Sample Output

题意:题目有点长,但是有用的不多。给出一张图,由0  1组成。w表示行和列。t表示占比率。求:要是1的总的占比率大于t则,图每个点都为1,要是0的总的占比率大于t则,图每个点都为0,若都不满足,这把他分为4个子图,再进行判断。如此递归

解析:递归判断4个子图。

#include<set>
#include<map>
#include<list>
#include<queue>
#include<cmath>
#include<stack>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;

#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}
const int maxn=70;
int t;
char a[maxn][maxn];
int check(int x1,int y1,int x2,int y2,int n)
{
	int cnt=0;
	for(int i=x1-1; i<y1; i++)
	{
		for(int j=x2-1; j<y2; j++)
		{
			if(a[i][j]=='0')cnt++;
		}
	}
	//printf("%d\n",cnt);
	if(cnt*100>=t*n)
	{
		for(int i=x1-1; i<y1; i++)
		{
			for(int j=x2-1; j<y2; j++)
			{
				a[i][j]='0';
			}
		}
		return 1;
	}
	else if((n-cnt)*100>=t*n)
	{
		for(int i=x1-1; i<y1; i++)
		{
			for(int j=x2-1; j<y2; j++)
			{
				a[i][j]='1';
			}
		}
		return 1;
	}
	else return 0;
}
void f(int a,int b,int c,int d,int n)
{
	if(check(a,b,c,d,n)==0)
	{
		f(a,(a+b)/2,(c+d)/2+1,d,n/4);
		f(a,(a+b)/2,c,(c+d)/2,n/4);
		f((a+b)/2+1,b,c,(c+d)/2,n/4);
		f((a+b)/2+1,b,(c+d)/2+1,d,n/4);
	}
	else return ;
}
int main()
{
	int w,cas=1;
	while(~scanf("%d",&w),w)
	{ 
		scanf("%d",&t);
		int cnt=0;
		for(int i=0; i<w; i++)
		scanf("%s",a[i]);
		printf("Image %d:\n",cas++);
		
		int total=w*w;
		f(1,w,1,w,total);
		for(int i=0; i<w; i++)
		printf("%s\n",a[i]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/yu121380/article/details/81584519