简体   繁体   English

在 C 中裁剪 PGM

[英]Cropping a PGM in C

I am trying to teach myself digital image process.我正在尝试自学数字图像处理。 I have read my pgm into a struct.我已将我的 pgm 读入一个结构。 In this struct I have pixel array.在这个结构中,我有像素数组。 I can print it just fine.我可以打印得很好。 Now I want to crop this photo based on a user input.现在我想根据用户输入裁剪这张照片。 I take these inputs and just get a black rectangle based on the coordinates entered.我接受这些输入,然后根据输入的坐标得到一个黑色矩形。 What I am doing wrong?我做错了什么?

 int temp, y1, y2, x1, x2, wide, high;

 printf("Please Enter x1 y1 x2 y2");


scanf("%i %i %i %i", &x1, &y1, &x2, &y2);
if(y1> y2)
{
    temp = y1;
    y1 = y2;
    y2 = temp;
}
if(x1> x2)
{
    temp = x1;
    x1 = x2;
    x2 = temp;
} 
wide = x2-x1+1;
high = y2-y1+1;

fprintf(outstream, "%2s\n%i %i\n%i\n", header->filetype, wide, high,header->maxgray);

pixel image[header->height][header->width];
pixel *pix = malloc((wide*high) *sizeof(char));

int a = 0;

    for(int b= 0; b<header->height; ++b)
    {
        for(int c=0; c<header->width; ++c)
        {

           image[b][c] = header->p[a];
            ++a;
        }
    }

int k = 0;
for(int i = 0; i< high; ++i)
{
    for(int j = 0; j<wide; ++j)
    {
        pix[k]=image[i][j];

    }
}
fwrite(pix, 1, (wide*high) * sizeof(pixel), outstream);

You are forgetting to increment k .您忘记增加k You also want your loops to be like this:您还希望您的循环是这样的:

for(int i = y1; i <= y2; ++i )
{
    for( int j = x1; j <= x2; ++j )
    {
        pix[k] = image[i][j]
        ++k;
    }
}

so that you actually crop out the rectangle you want, and not just the upper left rectangle of appropriate width and height.这样您就可以实际裁剪出所需的矩形,而不仅仅是具有适当宽度和高度的左上矩形。

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

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