如何产生对比鲜明的彩色图像? [英] how produce color contrast stretched image?
本文介绍了如何产生对比鲜明的彩色图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个站点上找到了一个灰度对比拉伸图像的公式,对其进行了编码和完成.但是任何人都可以对其进行编辑以得到彩色对比度拉伸图像吗?
i found a formula on a site for grey contrast stretched image, i code it and its done. but can any one edit it for color contrast stretched image?
CBitmap img;
CDC dc;
BITMAP bmp;
img.LoadBitmapW(IDB_BIT);
img.GetBitmap(&bmp);
CDC* pDC = this->GetDC();
dc.CreateCompatibleDC(pDC);
CBitmap* pOld = dc.SelectObject(&img);
for(int y = 0; y < bmp.bmHeight; y++)
{
for(int x = 0; x < bmp.bmWidth; x++)
{
COLORREF rgb = dc.GetPixel(x, y);
BYTE r = GetRValue(rgb);
BYTE g = GetGValue(rgb);
BYTE b = GetBValue(rgb);
BYTE bw = (BYTE)(0.3 * r + 0.59 * g + 0.11 * b + 0.5);
if(min>bw)
min=bw;
if(max<bw)
max=bw;
}
}
for(int y = 0; y < bmp.bmHeight; y++)
{
for(int x = 0; x < bmp.bmWidth; x++)
{
COLORREF rgb = dc.GetPixel(x, y);
BYTE r = GetRValue(rgb);
BYTE g = GetGValue(rgb);
BYTE b = GetBValue(rgb);
BYTE bw = (RGB(r,g,b)-min);
bw=bw*(255/(max-min));
bw+=max;
dc.SetPixel(x, y, RGB(bw, bw, bw));
}
}
pDC->BitBlt(200, 50, bmp.bmWidth, bmp.bmHeight, &dc, 0, 0, SRCCOPY);
dc.SelectObject(pOld);
推荐答案
您发布的算法下面的思想如下:
The idea beneath the algorithm you have posted is the following:
- 将位图转换为灰度
- 从整个位图获取最小和最大灰度级
- 重新映射每个灰度,以便将原始范围 [min,max] 线性映射到 [0,255]
- convert the bitmap to grayscale
- get the minimum and maximum gray levels from the whole bitmap
- remap each gray level so that the original range [min, max] is mapped linearly to [0, 255]
要将算法扩展为彩色图像,您可以按照以下步骤进行操作:
To extend the algorithm to color images you can work as follow:
for(int y = 0; y < bmp.bmHeight; y++)
{
for(int x = 0; x < bmp.bmWidth; x++)
{
COLORREF rgb = dc.GetPixel(x, y);
BYTE r = GetRValue(rgb);
BYTE g = GetGValue(rgb);
BYTE b = GetBValue(rgb);
if (min > r) min = r;
if (min > g) min = g;
if (min > b) min = b;
if (max < r) max = r;
if (max < g) max = g;
if (max < b) max = b;
}
}
for(int y = 0; y < bmp.bmHeight; y++)
{
for(int x = 0; x < bmp.bmWidth; x++)
{
COLORREF rgb = dc.GetPixel(x, y);
BYTE r = (GetRValue(rgb) - min) * 255 / (max - min);
BYTE g = (GetGValue(rgb) - min) * 255 / (max - min);
BYTE b = (GetBValue(rgb) - min) * 255 / (max - min);
dc.SetPixel(x, y, RGB(r, g, b));
}
}
我建议您不要使用CDC.GetPixel
,因为它非常慢且效率低下.您具有CBitmap
类,GetBitmapBits
和SetBitmapBits
方法.请访问以下网址,以获取有关图像编辑的更多信息 http://en.wikipedia.org/wiki/Image_editing [^ ].
I would recommend you NOT to useCDC.GetPixel
because it''s very slow and inefficient.You haveCBitmap
class andGetBitmapBits
andSetBitmapBits
methods.Please visit the following url for more information of images editing http://en.wikipedia.org/wiki/Image_editing[^].
这篇关于如何产生对比鲜明的彩色图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文