0

我正在尝试自学数字图像处理。我已将我的 pgm 读入一个结构。在这个结构中,我有像素数组。我可以打印得很好。现在我想根据用户输入裁剪这张照片。我接受这些输入,然后根据输入的坐标得到一个黑色矩形。我做错了什么?

 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);
4

1 回答 1

1

您忘记增加k. 您还希望您的循环是这样的:

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

这样您就可以实际裁剪出所需的矩形,而不仅仅是具有适当宽度和高度的左上矩形。

于 2011-07-06T03:59:31.943 回答