使用最近邻居扩展图像

我一直试图让我的程序扩大图像。 我在为缩放图像分配新空间时遇到了一些问题,但我认为它是固定的。 我遇到的问题是当我试图从我的临时内存持有者发回我的图像时程序崩溃。

加载的图像放在我的struct Image 。 像素以img->pixels放置,高度为img->height ,宽度为img->width 。 但我不知道为什么当我将像素从tmp2 struct转移到我的img struct时程序崩溃,而当我执行相反的操作时它不会崩溃。 这是代码:

 void makeBigger(Image *img, int scale) { Image *tmp2; tmp2 = (Image*)malloc(sizeof(Image)); tmp2->height = img->height*scale; tmp2->width = img->width*scale; tmp2->pixels = (Pixel**)malloc(sizeof(Pixel*)*tmp2->height); for (unsigned int i = 0; i height; i++) { tmp2->pixels[i] = (Pixel*)malloc(sizeof(Pixel)*tmp2->width); for (unsigned int j = 0; j width; j++) { tmp2->pixels[i][j] = img->pixels[i][j]; } } free(img->pixels); //scaling up the struct's height and width img->height *= scale; img->width *= scale; img->pixels = (Pixel**)malloc(sizeof(Pixel*)*img->height); for (unsigned int i = 0; i height; i++) { img->pixels[i] = (Pixel*)malloc(sizeof(Pixel)*img->width); for (unsigned int j = 0; j width; j++) { img->pixels[i][j] = tmp2->pixels[i+i/2][j+j/2]; } } } 

如果您对如何使最近邻方法起作用,我会很高兴。 谢谢!

编辑:我正在尝试裁剪内部矩形,以便我可以缩放(缩放)。

 Image *tmp = (Image*)malloc(sizeof(Image)); tmp->height = img->height / 2; tmp->width = img->width / 2; tmp->pixels = (Pixel**)malloc(sizeof(Pixel*) * tmp->height); for (unsigned i = img->height / 4 - 1; i height - img->height / 4; i++) { tmp->pixels[i] = (Pixel*)malloc(sizeof(Pixel) * tmp->width); for (unsigned j = img->width / 4; j width - img->width / 4; j++) { tmp->pixels[i][j] = img->pixels[i][j]; } } for (unsigned i = 0; i height; i++) { free(img->pixels[i]); } free(img->pixels); img->height = tmp->height; img->width = tmp->width; img->pixels = tmp->pixels; free(tmp); 

我看到你过于复杂的事情(例如,两次走过图像)。
这是代码(我发布了整个程序 – 我对PixelImage做出了可能与你的makeBigger假设),但是如果你复制/粘贴makeBigger它应该在你的代码中工作OOTB

 #include  #include  #include  typedef uint32_t Pixel; typedef struct { uint32_t width, height; Pixel **pixels; } Image; void makeBigger(Image *img, int scale) { uint32_t i = 0, j = 0; Image *tmp = (Image*)malloc(sizeof(Image)); tmp->height = img->height * scale; tmp->width = img->width * scale; tmp->pixels = (Pixel**)malloc(sizeof(Pixel*) * tmp->height); for (i = 0; i < tmp->height; i++) { tmp->pixels[i] = (Pixel*)malloc(sizeof(Pixel) * tmp->width); for (j = 0; j < tmp->width; j++) { tmp->pixels[i][j] = img->pixels[i / scale][j / scale]; } } for (i = 0; i < img->height; i++) free(img->pixels[i]); free(img->pixels); img->width = tmp->width; img->height = tmp->height; img->pixels = tmp->pixels; free(tmp); } void printImage(Image *img) { printf("Width: %d, Height: %d\n", img->width, img->height); for (uint32_t i = 0; i < img->height; i++) { for (uint32_t j = 0; j < img->width; j++) printf("%3d", img->pixels[i][j]); printf("\n"); } printf("\n"); } int main() { uint32_t i = 0, j = 0, k = 1; Image img; // Allocate and initialize the image data img.height = 2; img.width = 3; img.pixels = (Pixel**)malloc(sizeof(Pixel*) * img.height); for (i = 0; i < img.height; i++) { img.pixels[i] = (Pixel*)malloc(sizeof(Pixel) * img.width); for (j = 0; j < img.width; j++) img.pixels[i][j] = k++; } printImage(&img); makeBigger(&img, 2); printImage(&img); // Deallocate the image data for (i = 0; i < img.height; i++) free(img.pixels[i]); free(img.pixels); return 0; } 

注释makeBigger相关 - 旨在替换作为参数给出的图像的内容):

  • 构建一个将放大的临时图像
  • 仅遍历临时图像一次 (在我们分配它们时填充其像素); 为了保持缩放到原始图像并确保将相应的像素“复制”到新图像中,只需将索引除以缩放因子: tmp->pixels[i][j] = img->pixels[i / scale][j / scale]
  • 取消分配原始图像内容:由于每个像素行都是malloc ed,它也应该是free d( free(img->pixels);单独会产生内存泄漏
  • 将临时图像内容保存到原始内容中,然后取消分配

运行上面的程序(使用gcc编译 ),将输出以下内容:

Width: 3, Height: 2 1 2 3 4 5 6

Width: 6, Height: 4 1 1 2 2 3 3 1 1 2 2 3 3 4 4 5 5 6 6 4 4 5 5 6 6