社区
C++ Builder
帖子详情
图像问题 请问如何把 真彩图像 分别转为 256色索引图 和 灰度图
XWGer
2006-01-08 11:32:08
如题,分不够再加 谢谢大家
...全文
416
6
打赏
收藏
图像问题 请问如何把 真彩图像 分别转为 256色索引图 和 灰度图
如题,分不够再加 谢谢大家
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
6 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
constantine
2006-01-09
打赏
举报
回复
http://community.csdn.net/Expert/topic/4476/4476125.xml?temp=.2957575
看看这个帖子
#pragma hdrstop
#include <vcl.h>
#include <windows.h>
#include "dither.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
LColor ColorCount[4096]; //为记录颜色使用频率的数组
BYTE ColorTable[4096]; // 为记录颜色索引值的数组
//---------------------------------------------------------------------------
//统计颜色使用频率
void CountColor(Graphics::TBitmap *BitMap,LColor * ClrCount)
{
PRGBColor Ptr;
int i, j;
int CIndex;
for(i=0;i<4096;i++) // 初始化ColorCount数组
{
ClrCount[i].Color = i;
ClrCount[i].Times = 0;
}
for(i=0;i<BitMap->Height;i++)
{
Ptr = (TRGBColor*)BitMap->ScanLine[i];
for(j=0;j<BitMap->Width;j++)
{ //取 R、G、B三种颜色的前4位组成12位,共4096种颜色
CIndex = (Ptr->R & 0xF0)<<4;
CIndex = CIndex + (Ptr->G & 0xF0);
CIndex = CIndex + ((Ptr->B & 0xF0)>>4);
ClrCount[CIndex].Times++; //计算颜色的使用次数
Ptr++;
}
}
}
//---------------------------------------------------------------------------
// 清除使用次数为 0 的颜色数据,返回值为当前图像中颜色的种类
int Delzero(LColor *ClrCount)
{
int i,CIndex=0;
for(i=0;i<4096;i++)
{
if(ClrCount[i].Times)
{
ClrCount[CIndex].Color = ClrCount[i].Color;
ClrCount[CIndex].Times = ClrCount[i].Times;
ClrCount[i].Times = 0;
CIndex++;
}
}
return CIndex;
}
//---------------------------------------------------------------------------
// 快速排序, 将各种颜色 按使用的频率排序(Hight -- Low )
void QuickSort(LColor *A,int iLo,int iHi)
{
int Lo, Hi, Mid;
LColor Temp;
Lo = iLo;
Hi = iHi;
Mid = A[(Lo+Hi)/2].Times;
do
{
while(A[Lo].Times>Mid) Lo++;
while(A[Hi].Times<Mid) Hi--;
if(Lo <= Hi)
{
Temp.Color = A[Lo].Color;
Temp.Times = A[Lo].Times;
A[Lo].Color = A[Hi].Color;
A[Lo].Times = A[Hi].Times;
A[Hi].Color = Temp.Color;
A[Hi].Times = Temp.Times;
Lo++;
Hi--;
}
}while(Hi > Lo);
if(Hi>iLo) QuickSort(A, iLo, Hi);
if(Lo<iHi) QuickSort(A, Lo, iHi);
}
//---------------------------------------------------------------------------
void Sort(LColor *A,int Top)
{
QuickSort(A, 0, Top);
}
//---------------------------------------------------------------------------
// 构建调色表
HPALETTE BuildColorTable(LColor *ClrCount,PLogPalette Pal)
{
int i;
Pal->palVersion = 0x300;
Pal->palNumEntries = 256;
for(i=0;i<=255;i++)
{
Pal->palPalEntry[i].peRed = ((ClrCount[i].Color & 0xF00)>>4) + 7;
Pal->palPalEntry[i].peGreen = (ClrCount[i].Color & 0xF0) + 7;
Pal->palPalEntry[i].peBlue = ((ClrCount[i].Color & 0xF)<<4) + 7;
Pal->palPalEntry[i].peFlags = 0;
}
return CreatePalette(Pal);
}
//---------------------------------------------------------------------------
//根据统计的信息调整图像中的颜色, 将不常用的颜色用常用的颜色代替
void AdjustColor(int ClrNumber,LColor *ClrCount)
{
int i,C,Error,m,iTmp;
BYTE CIndex;
// for i := 0 to 4096 do ColorTable[i] := 0;
for(i=0;i<=255;i++)
ColorTable[ClrCount[i].Color] = i;
for(i=256;i<ClrNumber;i++)
{
Error=10000; //求最小误差
CIndex=0;
C=ClrCount[i].Color;
//从256个颜色索引值中取最接近的值替代
for(m=0;m<=255;m++)
{
iTmp = abs(ClrCount[m].Color) - C;
if(iTmp < Error)
{
Error = iTmp;
CIndex = m;
}
}
ColorTable[ClrCount[i].Color] = CIndex;
}
}
//---------------------------------------------------------------------------
void __fastcall Trueto256(Graphics::TBitmap *SBitMap,Graphics::TBitmap *DBitMap)
{
PLogPalette Pal;
int i, j, t, ColorNumber;
PRGBColor SPtr;
BYTE *DPtr;
if(SBitMap->Empty)
return;
CountColor(SBitMap, ColorCount); //统计颜色的使用频率
ColorNumber = Delzero(ColorCount); //去处不使用的颜色
Sort(ColorCount, ColorNumber); // 将颜色按使用频率排序
AdjustColor(ColorNumber, ColorCount);
//with DBitMap do
DBitMap->PixelFormat = pf8bit;
SBitMap->PixelFormat = pf24bit;
DBitMap->Width = SBitMap->Width;
DBitMap->Height = SBitMap->Height;
Pal = (PLogPalette)malloc(sizeof(TLogPalette)+sizeof(TPaletteEntry)*255);
BuildColorTable(ColorCount, Pal);
DBitMap->Palette = BuildColorTable(ColorCount, Pal); // Set DBitMap.Palette
free(Pal);
for(i=0;i<DBitMap->Height;i++)
{
SPtr = (PRGBColor)SBitMap->ScanLine[i];
DPtr = (BYTE *)DBitMap->ScanLine[i];
for(j=0;j<DBitMap->Width;j++)
{
t = (SPtr->R & 0xF0)<<4;
t = t + (SPtr->G & 0xF0);
t = t + ((SPtr->B & 0xF0)>>4);
*DPtr = ColorTable[t];
SPtr++;
DPtr++;
}
}
}
//---------------------------------------------------------------------------
头文件 dither.h
//---------------------------------------------------------------------------
#ifndef ditherH
#define ditherH
//---------------------------------------------------------------------------
struct TRGBColor;
typedef TRGBColor *PRGBColor;
#pragma pack(push, 1)
struct TRGBColor
{
BYTE B;
BYTE G;
BYTE R;
} ;
#pragma pack(pop)
typedef BYTE *PByte;
#pragma pack(push, 4)
struct LColor
{
int Color;
int Times;
} ;
#pragma pack(pop)
//-- var, const, procedure ---------------------------------------------------
void __fastcall Trueto256(Graphics::TBitmap* SBitmap, Graphics::TBitmap* DBitMap);
#endif
cczlp
2006-01-09
打赏
举报
回复
灰度
typedef struct {
WORD palVersion;
WORD palNumEntries;
PALETTEENTRY dummy[256];
} LOGPAL;
LOGPAL GrayPal;
int i;
Image1->Picture->LoadFromFile("c:\\aa.bmp");
GrayPal.palVersion=0x300;
GrayPal.palNumEntries=256;
for(i=0;i<256;i++)
{
GrayPal.dummy[i].peRed=i;
GrayPal.dummy[i].peGreen=i;
GrayPal.dummy[i].peBlue=i;
GrayPal.dummy[i].peFlags=0;
}
Image1->Picture->Bitmap->PixelFormat=pf8bit;
Image1->Picture->Bitmap->Palette= CreatePalette((const tagLOGPALETTE *)&GrayPal);
jixingzhong
2006-01-09
打赏
举报
回复
真彩图像 分别转为 256色索引图 和 灰度图
------------------
要转换为 灰度图,
好像要一个 象素颜色转换 公式的,
不过具体忘了 ....
读取象素的颜色值,
然后计算灰度,
就 OK 了 ...
LLBer
2006-01-09
打赏
举报
回复
厉害啊!我问一句,怎么调用这个函数呢?比如我现在有一幅图 (aa.bmp)想把他转成256的,怎么搞
麻酱面条
2006-01-08
打赏
举报
回复
这方面的东西应该不少,用 google 搜索一下哈
icwin
2006-01-08
打赏
举报
回复
up
matlab
灰度
图
像分类处理,Matlab
图
像
处理基础知识
本文详细介绍了Matlab中的
图
像
处理基础知识,包括
图
像
的存储方式、表达方式以及不同类型的
图
像
,如二进制
图
像
、
索引
图
、
灰度
图
和RGB
图
。此外,还讲解了如何进行
图
像
颜
色
转换,如使用dither函数进行
真彩
色
到
索引
色
的转换,以及
灰度
图
、RGB
图
之间的相互转换。同时,提到了
灰度
图
像的量化和筛选转换方法。
图
像
处理基础---RGB
图
灰度
图
索引
图
调
色
板
本文详细介绍了MATLAB中四种主要
图
像
类型:二进制
图
、
索引
图
、
灰度
图
及RGB
图
的特点及其相互间的转换方法。并通过具体示例展示了如何使用MATLAB函数实现这些转换。
彩
色
图
像
、
灰度
图
像、
索引
图
像
和二值
图
像
的区别
本文深入解析了RGB彩
色
图
像
、
灰度
图
像和
索引
图
像
的原理与特点,包括它们的颜
色
表示方式、存储方式以及在实际应用中的优劣。特别关注了
索引
图
像
的调
色
板概念及其在
色
彩复杂度上的差异。
彩
色
图
、
灰度
图
和二值
图
本文介绍了
图
像
处理中的几种基本
色
彩模式,包括RGB、CMYK、灰度和二值
图
。RGB模式用于电子设备,有约1678万种颜
色
;CMYK适用于印刷,通过减
色
原理实现
色
彩;HSL模式更符合人眼对颜
色
的感知;Lab模式基于人对颜
色
的感觉,调整亮度和
色
彩更加直观;
索引
图
使用调
色
板存储颜
色
,常用于网络发布;
灰度
图
用单一值表示亮度,二值
图
只有黑白两种状态,常用于简单
图
像
处理和文本识别。
C++ Builder
13,874
社区成员
102,696
社区内容
发帖
与我相关
我的任务
C++ Builder
C++ Builder相关内容讨论区
复制链接
扫一扫
分享
社区描述
C++ Builder相关内容讨论区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章