本文主要是介绍C实现纵横比保持的RGB图像缩放,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
双线性插值,实现图像scale down, 记个笔记
#include "stdafx.h"
#include <iostream>
#include "stdio.h"using namespace std;int img_down_scale(const unsigned char* src_img, unsigned char* dst_img, unsigned int src_width, unsigned int src_height, unsigned int dst_width, unsigned int dst_height)
{//bilinear interpolationif (!src_img || !dst_img) {std::cout << "input array can not be null!" << std::endl;return -1;}if (!dst_width|| !dst_height) {std::cout << "image size wrong!" << std::endl;return -2;}double x_ratio = (double)src_width / dst_width;double y_ratio = (double)src_height / dst_height;double aspect_ratio = 0.0f;int border_shift = 0;int h = 0, w = 0;int x0 = 0, x1 = 0, y0 = 0, y1 = 0;int dst_r = 0, dst_g = 0, dst_b = 0;if (x_ratio < y_ratio) {aspect_ratio = x_ratio;border_shift = (int)((src_height - (int)(dst_height * aspect_ratio)) / 2);for (h = 0; h < dst_height; h++) {for (w = 0; w < dst_width; w++) {double src_h = border_shift + h * aspect_ratio;double src_w = w * aspect_ratio;x0 = (int)src_w;x1 = x0 + 1;y0 = (int)src_h;y1 = y0 + 1;int rx0y0 = src_img[3 * (y0*src_width + x0) + 0];int gx0y0 = src_img[3 * (y0*src_width + x0) + 1];int bx0y0 = src_img[3 * (y0*src_width + x0) + 2];int rx1y0 = src_img[3 * (y0*src_width + x1) + 0];int gx1y0 = src_img[3 * (y0*src_width + x1) + 1];int bx1y0 = src_img[3 * (y0*src_width + x1) + 2];int rx0y1 = src_img[3 * (y1*src_width + x0) + 0];int gx0y1 = src_img[3 * (y1*src_width + x0) + 1];int bx0y1 = src_img[3 * (y1*src_width + x0) + 2];int rx1y1 = src_img[3 * (y1*src_width + x1) + 0];int gx1y1 = src_img[3 * (y1*src_width + x1) + 1];int bx1y1 = src_img[3 * (y1*src_width + x1) + 2];dst_r = (y1 - src_h) * ((x1 - src_w)* rx0y0 + (src_w - x0)*rx1y0) + (src_h - y0) *((x1 - src_w)*rx0y1 + (src_w - x0)*rx1y1);dst_g = (y1 - src_h) * ((x1 - src_w)* gx0y0 + (src_w - x0)*gx1y0) + (src_h - y0) *((x1 - src_w)*gx0y1 + (src_w - x0)*gx1y1);dst_b = (y1 - src_h) * ((x1 - src_w)* bx0y0 + (src_w - x0)*bx1y0) + (src_h - y0) *((x1 - src_w)*bx0y1 + (src_w - x0)*bx1y1);dst_r = dst_r > 255 ? 255 : dst_r < 0 ? 0 : dst_r;dst_g = dst_g > 255 ? 255 : dst_g < 0 ? 0 : dst_g;dst_b = dst_b > 255 ? 255 : dst_b < 0 ? 0 : dst_b;dst_img[3 * (h * dst_width + w) + 0] = (unsigned char)dst_r;dst_img[3 * (h * dst_width + w) + 1] = (unsigned char)dst_g;dst_img[3 * (h * dst_width + w) + 2] = (unsigned char)dst_b;}}} else {aspect_ratio = y_ratio;border_shift = (int)((src_width - (int)(dst_width * aspect_ratio)) / 2);for (h = 0; h < dst_height; h++) {double src_h = h * aspect_ratio;for (w = 0; w < dst_width; w++) {double src_w = border_shift + w * aspect_ratio;x0 = (int)src_w;x1 = x0 + 1;y0 = (int)src_h;y1 = y0 + 1;int rx0y0 = src_img[3 * (y0*src_width + x0) + 0];int gx0y0 = src_img[3 * (y0*src_width + x0) + 1];int bx0y0 = src_img[3 * (y0*src_width + x0) + 2];int rx1y0 = src_img[3 * (y0*src_width + x1) + 0];int gx1y0 = src_img[3 * (y0*src_width + x1) + 1];int bx1y0 = src_img[3 * (y0*src_width + x1) + 2];int rx0y1 = src_img[3 * (y1*src_width + x0) + 0];int gx0y1 = src_img[3 * (y1*src_width + x0) + 1];int bx0y1 = src_img[3 * (y1*src_width + x0) + 2];int rx1y1 = src_img[3 * (y1*src_width + x1) + 0];int gx1y1 = src_img[3 * (y1*src_width + x1) + 1];int bx1y1 = src_img[3 * (y1*src_width + x1) + 2];dst_r = (y1 - src_h) * ((x1 - src_w)* rx0y0 + (src_w - x0)*rx1y0) + (src_h - y0) *((x1 - src_w)*rx0y1 + (src_w - x0)*rx1y1);dst_g = (y1 - src_h) * ((x1 - src_w)* gx0y0 + (src_w - x0)*gx1y0) + (src_h - y0) *((x1 - src_w)*gx0y1 + (src_w - x0)*gx1y1);dst_b = (y1 - src_h) * ((x1 - src_w)* bx0y0 + (src_w - x0)*bx1y0) + (src_h - y0) *((x1 - src_w)*bx0y1 + (src_w - x0)*bx1y1);dst_r = dst_r > 255 ? 255 : dst_r < 0 ? 0 : dst_r;dst_g = dst_g > 255 ? 255 : dst_g < 0 ? 0 : dst_g;dst_b = dst_b > 255 ? 255 : dst_b < 0 ? 0 : dst_b;dst_img[3 * (h * dst_width + w) + 0] = (unsigned char)dst_r;dst_img[3 * (h * dst_width + w) + 1] = (unsigned char)dst_g;dst_img[3 * (h * dst_width + w) + 2] = (unsigned char)dst_b;}}}return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{unsigned char *pSrcImg = (unsigned char*)malloc(1000 * 1000 * 3);unsigned char *pDstImg = (unsigned char*)malloc(480 * 320 * 3);FILE *fp = fopen("input_1000x1000.bmp", "rb");fseek(fp, 54, SEEK_SET);fread(pSrcImg, 1000 * 1000 * 3, 1, fp);fclose(fp);img_down_scale(pSrcImg, pDstImg, 1000,1000, 480,320);unsigned char bmpHeader[54] = { 0 };FILE *fp_bmp_header = fopen("semseg_480x320.bmp", "rb");fread(bmpHeader, 54, 1, fp_bmp_header);fclose(fp_bmp_header);FILE *fp_write = fopen("down_scale_480x320.bmp", "wb");if (fp_write){fwrite(bmpHeader, 54, 1, fp_write);fwrite(pDstImg, 480 * 320 * 3, 1, fp_write);fclose(fp_write);fp_write = NULL;}if (pSrcImg) { free(pSrcImg); pSrcImg = NULL; }if (pDstImg) { free(pDstImg); pDstImg = NULL; }return 0;
}
这篇关于C实现纵横比保持的RGB图像缩放的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!