Commit d7c801ce authored by Ralf Stemmer's avatar Ralf Stemmer

Added Sobel Filter V2

parent 6a67e573
# SDF Sobel-Filter
A Bare-Metal Sobel-Filter implementation following the SDF Model of Computation.
\ No newline at end of file
A Bare-Metal Sobel-Filter implementation following the SDF Model of Computation.
Version 1 uses a 9·9 matrix, version 2 a 3·3 matrix.
Further more version 2 uses a feedback channel to avoid an internal state in GetPixels.
/*
* sobel2.c
*
* Created on: 02.03.20
* Author: Ralf Stemmer
*/
#include <sobel2.h>
int image_x = 0;
int image_y = 0;
void GetPixel(token_t position[2], token_t tokensOut[])
{
int16_t pixel_x = (int16_t) position[0];
int16_t pixel_y = (int16_t) position[1];
// Get a chunk of pixels as input for GX and GY
int i = 0;
for(int8_t matrix_y = 0; matrix_y < ConvMatrixDim; matrix_y++)
{
for(int8_t matrix_x = 0; matrix_x < ConvMatrixDim; matrix_x++)
{
int16_t chunk_x;
int16_t chunk_y;
chunk_x = pixel_x + matrix_x - (ConvMatrixDim >> 1);
chunk_y = pixel_y + matrix_y - (ConvMatrixDim >> 1);
if(chunk_x < 0)
chunk_x = 0;
else if(chunk_x >= ImageWidth)
chunk_x = ImageWidth - 1;
if(chunk_y < 0)
chunk_y = 0;
else if(chunk_y >= ImageHeight)
chunk_y = ImageHeight - 1;
tokensOut[i] = Image[chunk_y * ImageWidth + chunk_x];
i++;
}
}
// Calculate position for the next pixel
#ifdef EXTENDEDALU
pixel_x = (pixel_x + 1) % ImageWidth;
if(pixel_x == 0)
pixel_y = (pixel_y + 1) % ImageHeight;
#else
pixel_x++;
if(pixel_x >= ImageWidth)
{
pixel_x = 0;
pixel_y++;
if(pixel_y >= ImageHeight)
{
pixel_x = 0;
pixel_y = 0;
}
}
#endif
// Write next pixel position into the output tokens
position[0] = pixel_x;
position[1] = pixel_y;
return;
}
void GX(token_t tokensIn[], token_t tokensOut[1])
{
tokensOut[0] = 0;
for(uint8_t i = 0; i < ConvMatrixSize; i++)
{
tokensOut[0] += tokensIn[i] * ConvMatrix_X[ConvMatrixSize-1 - i];
}
return;
}
void GY(token_t tokensIn[], token_t tokensOut[1])
{
tokensOut[0] = 0;
for(uint8_t i = 0; i < ConvMatrixSize; i++)
{
tokensOut[0] += tokensIn[i] * ConvMatrix_Y[ConvMatrixSize-1 - i];
}
return;
}
token_t ABS(token_t tokensIn1[1], token_t tokensIn2[1])
{
if(tokensIn1[0] < 0) tokensIn1[0] = tokensIn1[0] * -1;
if(tokensIn2[0] < 0) tokensIn2[0] = tokensIn2[0] * -1;
token_t result;
// Usually, the formula is sqrt(GX² + GY²), we hacked it this way:
result = (tokensIn1[0] + tokensIn2[0]) & 0x000000FF;
//result = (tokensIn1[0] + tokensIn2[0]) >> 2;
return result;
}
// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
/*
* sobel2.h
*
* Created on: 02.03.2020
* Author: Ralf Stemmer
*/
#ifndef SOBEL2_H
#define SOBEL2_H
#ifndef LIBSDF_H
typedef int token_t;
#endif
#include <stdint.h>
// Definition of the matrices used for the Sobel algorithm
extern const uint8_t ConvMatrixDim; // 3, 9
extern const uint8_t ConvMatrixSize; // 9, 81
extern const int8_t ConvMatrix_X[];
extern const int8_t ConvMatrix_Y[];
// Definition of the input image
extern const uint8_t ImageHeight; // \_ Should never be more than 250 to have
extern const uint8_t ImageWidth; // / some safety margin against overflows
extern const uint8_t Image[]; // -- Pointer to Image[ImageHeight · ImageWidth]; 1 Pixel = 1 Byte (0 ... 255)
// The following arrays without explicit sized
// have an implicit size of ConvMatrixSize
void GetPixel(
token_t position[2], /* in/out ([x,y])*/
token_t tokensOut[] /* out */
);
void GX(
token_t tokensIn[], /* in */
token_t tokensOut[1] /* out */
);
void GY(
token_t tokensIn[], /* in */
token_t tokensOut[1] /* out */
);
token_t ABS(
token_t tokensIn1[1], /* in */
token_t tokensIn2[1] /* in */
);
#endif
#!/usr/bin/env bash
SOURCEPATH="./Sobel-Filter"
INSTALLPATH="./mb"
BUILDPATH="./build"
INCLUDEPATHS="-I$SOURCEPATH -I./mb"
SHARED_CFLAGS="-O0 -c -mcpu=v9.6 -mlittle-endian -fdata-sections -ffunction-sections -std=c99"
GCC=mb-gcc
OBJCOPY=mb-objcopy
AR=mb-ar
for lib in libsobel.a libsobel-ea.a
for lib in libsobel.a libsobel-ea.a libsobel2.a libsobel2-ea.a
do
if [ -f "$INSTALLPATH/$lib" ] ; then
echo -e "\e[1;31m$lib does already exist."
......@@ -33,6 +31,7 @@ function Build
{
LIBNAME="$1"
CFLAGS="$2"
INCLUDEPATHS="-I$SOURCEPATH -I./mb"
SOURCES=$(find $SOURCEPATH -type f -name "*.c")
for s in $SOURCES ;
......@@ -56,8 +55,12 @@ function Build
mkdir -p $INSTALLPATH
# Build Libraries
SOURCEPATH="./Sobel-Filter"
Build sobel "$SHARED_CFLAGS -mxl-soft-mul -mxl-soft-div -msoft-float"
Build sobel-ea "$SHARED_CFLAGS -mno-xl-soft-mul -mno-xl-soft-div -msoft-float"
SOURCEPATH="./Sobel-Filter2"
Build sobel2 "$SHARED_CFLAGS -mxl-soft-mul -mxl-soft-div -msoft-float"
Build sobel2-ea "$SHARED_CFLAGS -mno-xl-soft-mul -mno-xl-soft-div -msoft-float"
# Copy Headers
cp $SOURCEPATH/*.h $INSTALLPATH/.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment