tigerBin 发表于5个月前

• 发表于 5个月前
• 阅读 17
• 收藏 0
• 评论 0

（i，j）表示 destination 目标图片中的像素点， P(x0， y0) 表示 source源图像中的像素点

P(i, j)就是要求的像素点

1.首先，计算 水平压缩因子与垂直压缩因子，

``````double horFactor = srcWidth / dstWidth,

double verFactor = srcHeight / dstHeight``````

2. 计算 P(x0, y0)

``````double x0 = i * horFactor

double y0 = j * verFactor``````

3. 计算 Source中 距离P（x0, y0）最近的四个像素点的坐标（x1, y1), (x1, y2), (x2, y1), (x2, y2)

``````x1  =int(x0)
x2 = x1 + 1
y1 = int(y0)
y2 = y1 + 1``````

4.计算权值

5.利用公式求解 P（i，j）

6. Source Code

``````uint8_t  *Scale(uint8_t *src, int srcWidth, int srcHeight, int dstWidth, int dstHeight)
{
uint8_t *buf = new uint8_t[dstWidth * dstHeight * 3];

double horFactor = double(srcWidth) / dstWidth;  //水平缩放因子
double verFactor = double(srcHeight) / dstHeight; //垂直缩放因子

double w0, w1, w2, w3; // weight
int x1, y1, x2, y2; // (x1, y1), (x2, y2)
double fx1, fx2, fy1, fy2;

for (int i = 0; i < dstHeight; i++)
{
double x0 = i * verFactor;
x1 = int(i * verFactor);
x2 = x1 + 1;

fx1 = x2 - x0;
fx2 = x0 - x1;
for (int j = 0; j < dstWidth; j++)
{
double y0 = j * horFactor;
y1 = int(j * horFactor);
y2 = y1 + 1;

fy1 = y2 - y0;
fy2 = y0 - y1;

// 计算权值
w0 = fx1 * fy1;
w1 = fx1 * fy2;
w2 = fx2 * fy1;
w3 = fx2 * fy2;

int dstOffset = (i * dstWidth + j) * 3;
int srcOffset1 = (x1 * srcWidth + y1) * 3;
int srcOffset2 = (x2 * srcWidth + y1) * 3;

buf[dstOffset + 0] = w0 * src[srcOffset1] + w1 * src[srcOffset1 + 3] + w2 * src[srcOffset2] + w3 * src[srcOffset2 + 3];     //B
buf[dstOffset + 1] = w0 * src[srcOffset1+1] + w1 * src[srcOffset1 + 4] + w2 * src[srcOffset2+1] + w3 * src[srcOffset2 + 4]; //G
buf[dstOffset + 2] = w0 * src[srcOffset1+2] + w1 * src[srcOffset1 + 5] + w2 * src[srcOffset2+2] + w3 * src[srcOffset2 + 5]; //R
}
}

return buf;
}``````

``````int main(int argc, char *argv[])
{

JpegDecoder decoder("02.jpg");
auto &img = decoder.Decoder();

uint8_t *scale = Scale(img.Data, img.Width, img.Height, img.Width / 2, img.Height / 2);

cv::Mat src, dst;
src.create(img.Height, img.Width, CV_8UC3);
dst.create(img.Height / 2, img.Width / 2, CV_8UC3);

src.data = img.Data;
dst.data = scale;

cv::imshow("src", src);
cv::imshow("dst", dst);

cvWaitKey();
return 0;
}``````

×