#include <stdlib.h> //#include <string.h> //#include <stdio.h> //#include <math.h> #include "test_02_fnc.h" unsigned char getpixel(int x, int y, unsigned char * srcD, int width, int height) { if (x < 0) { x = 0; } else if (x >= width) { x = width - 1; } if (y < 0) { y = 0; } else if (y >= height) { y = height - 1; } return *(srcD + y * width + x); } int imageConvolution(float * dstD, unsigned char * srcD, int width, int height, float * kernel, int size) { if (!dstD || !srcD || !kernel) { return 0; } for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { short pixel = 0; for (int j = 0; j < size; j++) { for (int i = 0; i < size; i++) { pixel += *(kernel + j * size + i) * getpixel(x + i - size / 2, y + j - size / 2, srcD, width, height); } } *(dstD + y * width + x) = pixel; } } return 1; } int imageFilterSobel(unsigned char * dstD, unsigned char * srcD, int width, int height) { if (!dstD || !srcD) { return 0; } const short sobel[3][3] = {{1, 0, -1}, {2, 0, -2}, {1, 0, -1}}; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { short pixel_v = 0; short pixel_h = 0; for (int j = 0; j < 3; j++) { for (int i = 0; i < 3; i++) { pixel_v += sobel[j][i] * getpixel(x + i - 1, y + j - 1, srcD, width, height); pixel_h += sobel[i][j] * getpixel(x + i - 1, y + j - 1, srcD, width, height); } } pixel_h /= 4; pixel_v /= 4; unsigned short pixel = abs(pixel_h) + abs(pixel_v); if (pixel > 255) pixel = 255; //Orez *(dstD + y * width + x) = pixel; } } return 1; } int uchar_cmp(const void *a, const void *b) { const unsigned char *ca = (const unsigned char *) a; const unsigned char *cb = (const unsigned char *) b; return *ca - *cb; } int imageFilterMedian(unsigned char * dstD, unsigned char * srcD, int width, int height, int radius) { if (!dstD || !srcD) { return 0; } int edge = 2 * radius + 1; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { unsigned char pixels[edge * edge]; for (int j = 0; j < edge; j++) { for (int i = 0; i < edge; i++) { pixels[j * edge + i] = getpixel(x + i - edge / 2, y + j - edge / 2, srcD, width, height); } } qsort(&pixels, edge * edge, sizeof(unsigned char), uchar_cmp); *(dstD + y * width + x) = pixels[(edge * edge) / 2]; } } return 1; } /* Stručně popište chování konvoluce na kraji obrazu. ================================================== Opakuji se krajni pixely. Proč se tak děje? ================= Je to nejsnazsi cesta jak "roztahnout" obrazek, aby nedochazelo ke zmenam ve frekvencich. Jaké znáte druhy konvoluce? =========================== Diskretni, spojita, kruhova, 1D, 2D, 3D, rychla, ... Proč se většinou konvoluční jádra normalizují? ============================================== Aby nebyl vysledny obrazek prilis svetly. */