【发布时间】:2020-03-21 18:10:23
【问题描述】:
我有一个关于 zybooks 的项目。而且我的代码似乎工作正常,因为它可以正确突出显示不同图像的边缘。但是,zybooks 是自动化的,它使我的边缘检测功能失败。 这是我的两个函数的代码:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
typedef struct _image {
int** pixels;
int width;
int height;
} Image;
Image* readImage(char* filename) {
Image *pic= malloc(sizeof(Image));
char type[3];
int maxvalue;
FILE *f1=NULL;
f1= fopen(filename,"r");
if(f1==NULL){
printf("Unable to read image: %s\n",filename);
return NULL;
}
fscanf(f1,"%s",type);
fscanf(f1,"%d",&pic->width);
fscanf(f1,"%d",&pic->height);
fscanf(f1,"%d",&maxvalue);
pic->pixels = (int **)malloc(sizeof(int *) * pic->height);
for (int i = 0; i < pic->height; i++) {
pic->pixels[i] = (int *)malloc(sizeof(int) * pic->width);
}
for (int i = 0; i < pic->height; i++) {
for (int j = 0; j < pic->width; j++) {
fscanf(f1,"%d",&pic->pixels[i][j]);
}
}
fclose(f1);
return pic;
}
Image* edgeDetect(Image* img, int threshold) {
Image *edges;
edges=malloc(sizeof(Image));
edges->pixels = (int **)malloc(sizeof(int *) * img->height);
for (int i = 0; i < img->height; i++) {
edges->pixels[i] = (int *)malloc(sizeof(int) * img->width);
}
edges->height = img->height;
edges->width = img->width;
for (int i = 0; i < img->height; i++) {
for (int j = 0; j < img->width; j++) {
edges->pixels[i][j]=0;
}
}
for(int i=0; i< edges->height;i++){
for(int j=0; j<edges->width; j++){
if( i > 0 && i< (edges->height-1) && j>0 && j<(edges->width-1) ){
if(abs(img->pixels[i][j] - img->pixels[i][j+1]) > threshold || abs(img->pixels[i][j] - img->pixels[i-1][j]) > threshold)
edges->pixels[i][j]=255;
}
}
}
return edges;
}
int saveImage(char* filename, Image* img) {
FILE* f1= NULL;
f1= fopen(filename,"w");
if(f1==NULL){
printf("Unable to write image: %s\n",filename);
return 1;
}
fprintf(f1,"P2\n");
fprintf(f1,"%d %d\n",img->width,img->height);
fprintf(f1,"255\n");
for (int i = 0; i < img->height; i++) {
for (int j = 0; j < img->width; j++) {
fprintf(f1,"%d ",img->pixels[i][j]);
}
fprintf(f1,"\n");
}
fclose(f1);
return 0;
}
void freeImage(Image* img) {
for (int i = 0; i < img->height; i++) {
free(img->pixels[i]);
}
free(img->pixels);
free(img);
}
int main(int argc, char** argv) {
if( argc !=4){
printf("Usage: ./a.out input.pgm output.pgm threshold\n");
return 1;
}
int threshold= atoi(argv[3]);
Image *data;
data=readImage(argv[1]);
if (data==NULL)
return 1;
Image *edge;
edge= edgeDetect(data,threshold);
freeImage(data);
int result= saveImage(argv[2],edge);
if (result==1)
return 1;
freeImage(edge);
return 0;
}
任何提示都会有所帮助。我知道代码可能会令人困惑,因为它可能不遵循标准格式,但这确实是我的第一个编码课程。
【问题讨论】:
-
⟼请记住,尤其是在 Stack Overflow 上学习和提问时,尽可能让代码保持井井有条是很重要的。 Consistent indentation 有助于传达结构,更重要的是传达意图,这有助于我们快速找到问题的根源,而无需花费大量时间来尝试解码正在发生的事情。
-
这看起来像是内存泄漏,因为你从来没有
free任何东西,但它确实返回了指向已分配内存的指针。 -
贴出的代码无法编译!除其他外,它缺少头文件所需的
#include语句:stdio.h和stdlib.h -
我没有发布完整的代码。我现在就这样做
-
您了解 PPM 图像中的 光栅 样本(R、G、B)可以是 ASCII 文本(类型
"P3")或原始 - 二进制(类型 @ 987654332@) 其中如果maxcolor < 256每个样本为 1 个字节,否则每个样本为 2 个字节。您是否仅限于基本"P3"图像类型?
标签: c multidimensional-array struct function-pointers