gpt4 book ai didi

c++ - 双三次图像调整算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:47:26 26 4
gpt4 key购买 nike

我一直在尝试了解图像大小调整算法,例如最近邻、双三次和双线性插值算法。我研究了一点数学,现在我正在研究现有的实现以理解和欣赏它们的工作原理。

我从 this implementation 开始 我在 Google 代码上找到的 Bi-Cubic Resize 的 ,它使用 OpenCV,并提供了测试和示例代码。我用它作为我自己实现的起点,它不使用 OpenCV,而只是对 std::vector<unsigned char> 中包含的原始位图进行操作。 :

std::vector<unsigned char> bicubicresize(const std::vector<unsigned char>& in, std::size_t src_width,
std::size_t src_height, std::size_t dest_width, std::size_t dest_height)
{
std::vector<unsigned char> out(dest_width * dest_height * 3);

const float tx = float(src_width) / dest_width;
const float ty = float(src_height) / dest_height;
const int components = 3;
const int bytes_per_row = src_width * components;

const int components2 = components;
const int bytes_per_row2 = dest_width * components;

int a, b, c, d, index;
unsigned char Ca, Cb, Cc;
unsigned char C[5];
unsigned char d0, d2, d3, a0, a1, a2, a3;

for (int i = 0; i < dest_height; ++i)
{
for (int j = 0; j < dest_width; ++j)
{
const int x = int(tx * j);
const int y = int(ty * i);
const float dx = tx * j - x;
const float dy = ty * i - y;

index = y * bytes_per_row + x * components;
a = y * bytes_per_row + (x + 1) * components;
b = (y + 1) * bytes_per_row + x * components;
c = (y + 1) * bytes_per_row + (x + 1) * components;

for (int k = 0; k < 3; ++k)
{
for (int jj = 0; jj <= 3; ++jj)
{
d0 = in[(y - 1 + jj) * bytes_per_row + (x - 1) * components + k] - in[(y - 1 + jj) * bytes_per_row + (x) * components + k];
d2 = in[(y - 1 + jj) * bytes_per_row + (x + 1) * components + k] - in[(y - 1 + jj) * bytes_per_row + (x) * components + k];
d3 = in[(y - 1 + jj) * bytes_per_row + (x + 2) * components + k] - in[(y - 1 + jj) * bytes_per_row + (x) * components + k];
a0 = in[(y - 1 + jj) * bytes_per_row + (x) * components + k];
a1 = -1.0 / 3 * d0 + d2 - 1.0 / 6 * d3;
a2 = 1.0 / 2 * d0 + 1.0 / 2 * d2;
a3 = -1.0 / 6 * d0 - 1.0 / 2 * d2 + 1.0 / 6 * d3;
C[jj] = a0 + a1 * dx + a2 * dx * dx + a3 * dx * dx * dx;

d0 = C[0] - C[1];
d2 = C[2] - C[1];
d3 = C[3] - C[1];
a0 = C[1];
a1 = -1.0 / 3 * d0 + d2 -1.0 / 6 * d3;
a2 = 1.0 / 2 * d0 + 1.0 / 2 * d2;
a3 = -1.0 / 6 * d0 - 1.0 / 2 * d2 + 1.0 / 6 * d3;
Cc = a0 + a1 * dy + a2 * dy * dy + a3* dy * dy * dy;
out[i * bytes_per_row2 + j * components2 + k] = Cc;
}
}
}
}

return out;
}



现在,据我所知,此实现存在致命缺陷,因为以下行:

d0 = in[(y - 1 + jj) * bytes_per_row + (x - 1) * components + k] - in[(y - 1 + jj) * bytes_per_row + (x) * components + k];



在我看来,当 y 时,这一行将总是访问越界数组索引。是0 .和 y永远是0最初,因为 y 是用 y = ty * i 初始化的, 和 i是一个从0 开始的迭代器变量.所以自 y将始终从 0 开始, 表达式 (y - 1 + jj) * bytes_per_row + (x - 1) * components + k (用于计算 ARRAY INDEX)将始终为负数。并且...显然,负数数组索引是无效的。

问题:在我看来,这段代码不可能正常工作。我不知何故错了吗?

最佳答案

一种方法是定义一个 GetPixel() 函数:

GetPixel(int x, int y, int channel)
{
if (x >= 0 && x <= width-1)
if (y >=0 && y <= height-1)
return QueriedPixelFromImage(x,y,channel);
return 0; // out of bounds
}

你将替换

d0 = in[(y - 1 + jj) * bytes_per_row + (x - 1) * components + k]

通过

d0 = GetPixel(x-1,y-1+jj,k)

关于c++ - 双三次图像调整算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15141534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com