在 C 中裁剪 PGM
我正在尝试自学数字图像处理。我已将我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘记增加
k
。您还希望您的循环像这样:以便您实际上裁剪出所需的矩形,而不仅仅是具有适当宽度和高度的左上角矩形。
You are forgetting to increment
k
. You also want your loops to be like this:so that you actually crop out the rectangle you want, and not just the upper left rectangle of appropriate width and height.