Edge Detection
-
hallo!
bräuchte bitte eure hilfe! muss ein Edge Detection programm in C schreiben!
also ein farb bild in schwarz weiss umwandeln!!bis jetzt habe ich folgendes geschaft:
die datei offnen, alle werte einlesen und dann wieder ausgeben!wie programmiere ich jetzt den algorytmus damit das programm das farb bild in ein schawrz bild umwandelt( die werte von denn pixel )!!
was ich bis jetzt prg habe
#include <stdio.h>
#include <stdlib.h>int main(int argc, char *argv[])
{
FILE *readFp, *writeFp;
char c;
char string[100];
unsigned int width, lines, cdepth;struct pixel
{
unsigned int r;
unsigned int g;
unsigned int b;
};struct pixel *ptr;
width = 0;
lines = 0;
cdepth = 0;
readFp = fopen(argv[1],"r");
writeFp = fopen(argv[2],"w");
while (1)
{
c = fgetc(readFp);
if (c==EOF)
break;
if (c=='#')
fgets(string,100,readFp);
else
{
if (c == ' ')
fputc('\n',writeFp);
else
fputc(c,writeFp);
}
}
fclose(readFp);
fclose(writeFp);readFp = fopen(argv[2],"r");
writeFp = fopen(argv[3],"w");
while (1)
{
fgets(string,100,readFp);
if (feof(readFp) != 0)
break;
if (string[0]=='\n')
continue;
else
fputs(string,writeFp);
}
fclose(readFp);
fclose(writeFp);// lies das Bild ein
readFp = fopen(argv[3],"r");
writeFp = fopen(argv[4],"w");
fgets(string,100,readFp);
if (string[0]!='P' || string[1]!='3' || string[2]!='\n')
exit(0);
fscanf(readFp,"%d",&width);
fscanf(readFp,"%d",&lines);
fscanf(readFp,"%d",&cdepth);
#if 1
printf("breadth=%d, lines=%d, tone=%d",width,lines,cdepth);
#endif
ptr = calloc(width*lines, sizeof(struct pixel));
if (ptr==NULL)
exit(-1);free(ptr);
fclose(readFp);
fclose(writeFp);
return 0;
}danke für eure hilfe schon im voraus
-
Hast du schon mal nach deinem Problem gegooglet?
Vielleicht hilft dir das hier weiter: http://en.wikipedia.org/wiki/Edge_detection
Gruß mcr
-
kebapmaster schrieb:
wie programmiere ich jetzt den algorytmus damit das programm das farb bild in ein schawrz bild umwandelt( die werte von denn pixel )!!
sachwarzbild? na, alle pixel auf 0 setzen.
ne, im ernst, mal 'ne fixe idee: rechne R-G+B. ist der betrag davon über einem gewissen schwellwert, dann mach das pixel weiss, ist er darunter, dann mach es schwarz. oder willst du ein graustufen-bild haben?
-
was du beschreibst ist eine binarisierung.
eine edge detection ist üblicherweise eine FaltungEdit:
schau dir mal die openCV bibliothek an, das ist die Standard-Bildverarbeitungsbibliothek überhaupt.da gibts auch verschiedene Kantenfilter.
Die bekanntesten sind wohl der Sobel und der Canny
-
danke für deinen beitrag aber ich muss es in C programmieren!!
ich weiss nicht wie ich dass angehen soll!!
-
Die OpenCv is ne biblothek mit einem c interface, sie sollte also auch aus c-code benutzt werden können.
(hab ich selbst schon gemacht)noch mal die frage:
Was willst du machen?
der titel lautet du willst eine kantenerkennung.
im text schreibst du du willst ein Schwarz/Weiß-Bild?dein struct Pixel sieht komisch aus, oder sind die farbkanäle wirklich 32 bit breit?
Falls ja, dann auf jeden fall sicherlich nicht signed, oder?wirklich SW-Bild oder Graustufen?
In was für einem Format liegt dein Bild denn vor?
Wenn du irgendwelche standardformate öffnen willst, dann würde ich das auf keinen fall selbst schreiben, da die header recht komplex sind und doch recht viel aufwand nötig ist um alle eigenarten eines Formates abzudecken.Was ist denn die eigendliche aufgabenstellung?
Wenn die das programmieren einer Kantenerkennung ist, dann hat der Prof, oder wer auch immer, sicher nix dagegen, wenn man eine Bibliothek zum laden der Bilder benutzt. Das man den entsprechenden algorithmuis selbst implementieren sollte und nicht den der Bibliothek nimmt, ist klar.Ich hab mal ein kleines opencv-C-Programm hochgeladen, welches nur ein bild öffnet und anzeigt.
--> ocvCtest.zipOpenCV muss natürlich installiert sein und im Visual studio die lib und includepfade gesetzt sein:
unter vs2005:
tools->options->Projects and solutions->vc++ Directories
Bsp (stand ocv 1.0):
inculde:
F:\installed\SDK\OpenCV\cv\include
F:\installed\SDK\OpenCV\cxcore\include
F:\installed\SDK\OpenCV\cvaux\include
F:\installed\SDK\OpenCV\otherlibs\highgui
F:\installed\SDK\OpenCV\otherlibs\cvcam\includelib:
F:\installed\SDK\OpenCV\lib
-
meine aufgabenstellung:
Task: Write a task that detects color differences within an image by comparing (subtracting) source pixels with adjacent ones. If the latter differ by more than a given threshold from the source pixel than they will be output as white pixels in the output image whereas all other pixels below the threshold will be output as black pixels. The program shall operate using *.ppm images (see below for a short description on how to obtain a *.ppm file and how it is made up).
(i) In particular the program starts at the left top corner of an image by reading the first pixel fpx and writes a first black pixel in the top left postion of the output *.ppm file.
(ii) Than it compares this one fpx with a pixel to the right rpx. Comparison is done by subtracting the three colors RGB from each pixel and comparing whether the results are below a certain threshold (i.e. R1-R2<r, G1-G2<g and B1-B2<b). In the case when every difference is below the specified threshold, than the pixels rpx is written in black to an output *.ppm file. Otherwise, the pixel rpx is written in white.
(iii) Next the first pixel fpx is compared with a pixel below lpx by performing the same subtractions and comparisons. When all results are below the specified thresholds pixel lpx is written in black otherwise in white.
(iv) The program starts over by performing steps (i ... iii) once more. The now first pixel, however, is the right neighbour of the first pixel in the row and the color of the first output pixel is determined from the previous step (ii).
(a) In the case when the last pixel in a row is reached, only a comparison and according actions are performed towards the pixel below.
(b) Than the entire processing is started over by commencing at the next line. In this case, however, all pixels in the output line are already set by the processing of the previous line. The first pixel in the new row is left as it was set before; all pixels to the right, however, are either left white when the pixel was set to white before (even though a comparison would set it to black right now) or are changed from black to white when a comparison requests such a change.
(c) When the last line is processed no comparisons and actions with pixels below the last line are performed.
The program shall be able to deal with command line arguments according to the following syntax notation:
edge_detect -i <infile> -o <outfile> [-r <r_threshold>] [-g <g_threshold>] [-b <b_threshold>] [-h]Herein, the options -r, -g, -b and -h are optional. The options -r, -g and -b specify the threshold values for every color using a threshold value between 0 and 255; per defualt the threshold values of all three parameters shall be set to 10. The -h options shall be used to output a help/usage message.
You may use the free image viewer Irfanview to create a *.ppm file out of any other image format. Therefore open, e.g., a JPEG file in Irfanview and select File » Save As … in the pop-up window choose PPM as file type ... activate "Show Options Dialog" ... in the latter select "ASCII encoding" ... and finally select Save.
dankeschön im voraus für eure hlfe
mfg
-
ich würd das ganze in strukturen und funktionen auslagern um das ganze ein wenig übersichtlicher zu machen:
typedef unsigned int uint32; typedef struct Pixel{ uint32 r; uint32 g; uint32 b; }Pixel; typedef struct P3Image{ uint32 width; uint32 height; uint32 maxVal; Pixel* data; }P3Image; /** * returns a pointer to a pixel in the image * @param i_image the image where the pixel should be read from * @param x the x coord * @param y the y coord * @return pointer to the pixel on position (x,y) */ inline Pixel* P3Image_getPixel(P3Image* i_image, uint32 x, uint32 y) { ASSERT(x < i_image->width); ASSERT(y < i_image->height); return &(i_image->data[x +i_image->width* y]); } /** * Deletes the given Image * @param i_image the image to delete */ void P3Image_Free(P3Image* i_image) { free(i_image->data); free(i_image); } /** * creates a new image. * The caller has to ensure that the allocated memory will be freed * by calling P3Image_Free * @param i_width the number of cols the image should have * @param i_height the number of rows the image should have * @return the image that was created or NULL if an error occured. */ P3Image* P3Image_create( uint32 i_width, uint32 i_height) { P3Image* image = malloc(sizeof(P3Image)); if(image != NULL) { image->data = malloc(sizeof(Pixel) * i_width * i_height); if(image->data == NULL) { free(image); image = NULL; }else{ image->width = i_width; image->height = i_height; } } return image; } /** * Reads an image from file. * The caller has to ensure that the allocated memory will be freed * by calling P3Image_Free * @param i_filename the name of the file to open * @return the image that was read from file or NULL if an error occured. */ P3Image* P3Image_ReadFromFile( const char *i_filename) { } /** * Writes an P3 ppm image to a file * @param i_image the image that to write to file * @param i_filename the name of the file to write * @return TRUE if successful else FALSE */ BOOL P3Image_WriteToFile( const P3Image* i_image, const char *i_filename) { FILE* file; uint32 i; uint32 maxval; ASSERT(i_image); maxval = i_image->maxVal; file = fopen(i_filename, "w"); if(!file) return FALSE; for(i=0; i<i_image->width * i_image->height; ++i) { maxval = max(maxval, max( i_image->data[i].r, max( i_image->data[i].g, i_image->data[i].b))); } fprintf(file, "P3\n# created by vlad\n%d %d\n%d\n",i_image->width, i_image->height, maxval); for(i=0; i<i_image->width * i_image->height; ++i) { fprintf(file, "%d %d %d\n", i_image->data[i].r, i_image->data[i].g, i_image->data[i].b); } fclose(file); return TRUE; }
habs jetzt nicht getestet, mag sein, dass ein paar typos drin sind.
mal sehen, vielleicht habe ich später lust, die lese und schreibefunktionen zu coden.
-
Danke für deine hilfe!!!!
Habe es mal ausprobiert kommen hal 5-8 fehler werde es versuchen selber zu lösen. wenn nicht könntest du mir da weiterhelfen bitte!
nochmals dankeschön!
Mit welchen programm hast du es programmiert?? Weil die programmiersprache kenne ich nicht!! (bin anfänger)mfg
kebapmaster
-
mit welchem Programm? Ich habs erst in nem editor geschrieben und bin dann doch ins Visual Studio gewechselt. sprache ist natürlich C.
Das ist übrigens ein komisches Filter.
Oder ich habe den text nicht verstanden, is ganz schön doof geschrieben.