使用最近邻法放大图像

Scaling up an image using nearest-neighbor

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

加载的图片放在我的structImage。像素被放置在 img->pixelsimg->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 < img->height; i++)
    {
        tmp2->pixels[i] = (Pixel*)malloc(sizeof(Pixel)*tmp2->width);
        for (unsigned int j = 0; j < img->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 < tmp2->height; i++)
    {
        img->pixels[i] = (Pixel*)malloc(sizeof(Pixel)*img->width);
        for (unsigned int j = 0; j < tmp2->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 < img->height - img->height / 4; i++) {
    tmp->pixels[i] = (Pixel*)malloc(sizeof(Pixel) * tmp->width);
    for (unsigned j = img->width / 4; j < img->width - img->width / 4; j++) {
        tmp->pixels[i][j] = img->pixels[i][j];
    }
}

for (unsigned i = 0; i < img->height; i++) {
    free(img->pixels[i]);
}
free(img->pixels);

img->height = tmp->height;
img->width = tmp->width;
img->pixels = tmp->pixels;
free(tmp);

我发现你把事情搞得太复杂了(例如,在图像上走动两次)。
这是代码(我发布了整个程序 - 我做了关于 Pixel[ 的假设=43=] 和 Image 可能与您所拥有的不匹配),但是如果您复制/粘贴 makeBigger 它应该可以在您的代码中使用 OOTB:

code00.c:

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>


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;
    // Initialize the image
    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);

    // Destroy the image
    for (i = 0; i < img.height; i++)
        free(img.pixels[i]);
    free(img.pixels);

    printf("\nDone.\n");
    return 0;
}

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

  • 构造一个临时图像,它将成为放大后的图像
  • 只遍历临时图像一次(在我们分配它们时填充它的像素);要保持对原始图像的缩放并确保将适当的像素“复制”到新图像中,只需将索引除以缩放因子:tmp->pixels[i][j] = img->pixels[i / scale][j / scale]
  • 取消分配 原始 图像内容:因为每个像素行都是 malloced,它也应该是 freed(单独使用free(img->pixels);会产生内存泄漏
  • 存储临时图像内容(到原始图像内容中)然后释放它

输出:

[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/Whosebug/q041861274]> ~/sopr.sh
### Set shorter prompt to better fit when pasted in Whosebug (or other) pages ###

[064bit prompt]> ls
code00.c
[064bit prompt]> gcc -o code00.exe code00.c
[064bit prompt]> ./code00.exe
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


Done.