繁体   English   中英

使用最近邻放大图像

[英]Scaling up an image using nearest-neighbor

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

加载的图像放在我的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 < 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);

我看到你把事情复杂化了(例如在图像上走两次)。
这是代码(我正在发布整个程序 - 我对PixelImage做出的假设可能与您拥有的不匹配),但是如果您复制/粘贴makeBigger它应该在您的代码OOTB 中工作

代码00.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;
}

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

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

输出

 [cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q041861274]> ~/sopr.sh *** Set shorter prompt to better fit when pasted in StackOverflow (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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM