Image Compression

本文介绍了一种基于图像区域相似性的二维图像压缩方法。该方法通过递归地将图像分解为四个子区域,并根据预设的阈值进行损失或无损压缩。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

<think>好的,我现在需要解决Albumentations库中ImageCompression的quality_lower参数无效的问题。用户遇到了警告,提示quality_lower参数被忽略。首先,我得弄清楚这个问题的原因是什么。 根据经验,可能的情况是参数设置不正确或者版本问题。Albumentations的ImageCompression变换可能有不同的参数要求。比如,某些版本可能要求quality_lower和quality_upper同时设置,或者其中一个参数被弃用了。用户可能只设置了quality_lower而没有设置quality_upper,导致库无法识别而忽略该参数。 接下来,我需要查阅Albumentations的官方文档,确认ImageCompression的参数使用方式。在文档中,ImageCompression通常需要指定quality_lower和quality_upper两个参数,它们定义了压缩质量的范围。如果只设置其中一个,库可能无法处理,从而发出警告。例如,正确的用法应该是同时设置这两个参数,如quality_lower=50, quality_upper=100。 另外,检查用户使用的Albumentations版本也很重要。如果用户使用的是旧版本,可能参数名称或用法有变化。比如,某些版本可能将参数名改为min_quality和max_quality,或者调整了参数的默认值。升级到最新版本可能解决这个问题。 还有可能是用户在使用ImageCompression时没有正确传递参数。例如,应该使用关键字参数而不是位置参数。需要确认代码中的调用方式是否正确,比如是否正确写成ImageCompression(quality_lower=50, quality_upper=100, always_apply=False, p=0.5)。 此外,查看库的源代码或GitHub上的issues可能会有帮助。其他用户可能遇到过类似的问题,开发者可能已经修复或者在讨论中提供了解决方案。比如,在某个issue中,开发者提到需要同时设置quality_lower和quality_upper,否则会使用默认值,导致警告出现。 综合以上分析,解决方案应该是确保同时设置quality_lower和quality_upper参数,并且检查库的版本是否需要更新。同时,验证代码中的参数传递是否正确,避免语法错误。如果问题依旧存在,可能需要查看具体的警告信息,进一步调试或向社区寻求帮助。</think>### Albumentations ImageCompression quality_lower 参数警告解决方案 在使用Albumentations的`ImageCompression`变换时,如果出现`quality_lower`参数被忽略的警告,通常是由于以下原因及解决方案: --- #### 1. **参数未成对使用** - **问题原因**:`ImageCompression`要求同时设置`quality_lower`和`quality_upper`参数。若仅设置其中一个,库会忽略该参数并采用默认值[^1]。 - **示例错误代码**: ```python ImageCompression(quality_lower=50, always_apply=False, p=0.5) # 缺少 quality_upper ``` - **解决方案**: ```python ImageCompression(quality_lower=50, quality_upper=100, always_apply=False, p=0.5) ``` --- #### 2. **版本兼容性问题** - **问题原因**:旧版本Albumentations(如1.0.0以下)可能对参数命名或行为有差异。 - **检查版本**: ```bash pip show albumentations ``` - **升级到最新版本**: ```bash pip install --upgrade albumentations ``` --- #### 3. **参数范围错误** - **问题原因**:`quality_lower`和`quality_upper`需满足: - 取值范围:`0 ≤ quality_lower ≤ quality_upper ≤ 100` - 若设置`quality_lower=100`且`quality_upper=100`,等同于无压缩。 - **正确示例**: ```python ImageCompression(quality_lower=50, quality_upper=90) # 压缩质量在50-90之间随机选择 ``` --- #### 4. **警告忽略(临时方案)** 若需临时屏蔽警告(不推荐长期使用): ```python import warnings warnings.filterwarnings("ignore", message="quality_lower parameter is ignored") ``` --- #### 验证代码 ```python from albumentations import ImageCompression import cv2 # 正确参数设置 transform = ImageCompression(quality_lower=50, quality_upper=100, p=1.0) image = cv2.imread("test.jpg") augmented = transform(image=image) # 应无警告 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值