文章目錄
- 前言
- 雙線性插值基礎
- 理論基礎
- 演算法
- Cpp實作
前言
做雙線性插值用到的一個函式,CDib庫的檔案定義方式參考微軟官方檔案 Dib庫,
雙線性插值基礎
理論基礎
? 這個文章覺得理論講的還好,但演算法講的有點亂一篇文章為你講透雙線性插值 - 知乎 (zhihu.com)
演算法
? 這個參考知乎徹底搞懂雙線性插值 - 知乎 (zhihu.com),演算法部分講的比上面那個清楚,有Python實作,
Cpp實作
void CDib::Zoom(double times,CDib* buffer)
{
//空圖片容錯
if (!this) return;
//備份長寬 銷毀原資料
int Width = GetWidth()*times;
int Height = GetHeight()*times;
Destroy();
//創建新的資料區
int BPP = 8;
Create(Width, Height, BPP, 0);
m_nWidth = Width;
m_nHeight = Height;
//調色板賦值
if (IsIndexed())
{
int nColors = 256;
if (nColors > 0)
{
RGBQUAD *pal = new RGBQUAD[nColors];
for (int i = 0; i < nColors; i++)
{
pal[i].rgbRed = i;
pal[i].rgbBlue = i;
pal[i].rgbGreen = i;
pal[i].rgbReserved = BYTE(0);
}
SetColorTable(0, nColors, pal);
delete[] pal;
}
}
//獲取資料區大小
m_nWidthBytes = abs(GetPitch());
m_nBitCount = GetBPP();
m_pDibBits = (unsigned char*)GetBits() + (m_nHeight - 1)*GetPitch();
//資料區賦值(逐像素計算)
int srcWidth = buffer->GetWidth();
int srcHeight = buffer->GetHeight();
double srcX, srcY;
int srcX_0, srcY_0, srcX_1, srcY_1;
double value1, value2; // 存盤對x軸線性插值后的值
unsigned char* temp1,* temp2,* temp3,* temp4; //暫存對應像素點地址
for (int dstY = 0; dstY < m_nHeight; dstY++)
{
for (int dstX= 0; dstX < m_nWidthBytes; dstX++)
{
//按比例對應
/*srcX = (dstX)* (srcWidth * 1.0/ m_nWidth);
srcY = (dstY) * (srcHeight * 1.0/ m_nHeight);*/
// 按幾何中心對應
srcX = max((dstX + 0.5) * (srcWidth * 1.0 / m_nWidth) - 0.5 , 0);
srcY = max((dstY + 0.5) * (srcHeight * 1.0/ m_nHeight) - 0.5 , 0);
srcX_0 = min((int)floor(srcX), buffer->m_nWidth - 1);
srcY_0 = min((int)floor(srcY), buffer->m_nHeight - 1);
srcX_1 = min(srcX_0 + 1 , buffer->m_nWidth - 1);
srcY_1 = min(srcY_0 + 1, buffer->m_nHeight - 1);
//計算
temp1 = buffer->m_pDibBits + srcY_0 * buffer->m_nWidthBytes + srcX_0;
temp2 = buffer->m_pDibBits + srcY_0 * buffer->m_nWidthBytes + srcX_1;
temp3 = buffer->m_pDibBits + srcY_1 * buffer->m_nWidthBytes + srcX_0;
temp4 = buffer->m_pDibBits + srcY_1 * buffer->m_nWidthBytes + srcX_1;
value1 = *temp1 * (srcX - srcX_0) + *temp2 * (srcX_1 - srcX);
value2 = *temp3 * (srcX - srcX_0) + *temp4 * (srcX_1 - srcX);
//賦值
*(m_pDibBits + dstY * m_nWidthBytes + dstX) = \
value1*(srcY - srcY_0) + value2 * (srcY_1 - srcY);
int show = *(m_pDibBits + dstY * m_nWidthBytes + dstX);
}
}
覺得有用就點個贊,我比較喜歡臭美,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/279906.html
標籤:其他
上一篇:光流 | 基于光流的實時運動物體檢測(MATLAB代碼)
下一篇:OpenCV入門
