在 C 中裁剪 PGM

发布于 2024-11-18 10:14:49 字数 998 浏览 2 评论 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);

I am trying to teach myself digital image process. I have read my pgm into a struct. 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九八野马 2024-11-25 10:14:49

您忘记增加k。您还希望您的循环像这样:

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

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

You are forgetting to increment 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文