Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix : image/conv2 & dsp/conv, p_median3x3_f32 : hardwired network #148

Merged
merged 2 commits into from
Jun 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ FUNCTION | NOTES
[p_max()](src/math/p_max.c) | finds max val
[p_min()](src/math/p_min.c) | finds min val
[p_mean()](src/math/p_mean.c) | mean operation
[p_median()](src/math/p_mean.c) | finds middle value
[p_median()](src/math/p_median.c) | finds middle value
[p_mode()](src/math/p_mode.c) | finds most common value
[p_mul()](src/math/p_mul.c) | multiplication
[p_popcount()](src/math/p_popcount.c) | count the number of bits set
Expand Down
2 changes: 1 addition & 1 deletion include/pal_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/*2d convolution */
void p_conv2d_f32(const float *x, float *m, float *r, int rows, int cols,
int msize);
int mrows, int mcols);

/*2d box (i.e mean) filter(3x3) */
void p_box3x3_f32(const float *x, float *r, int rows, int cols);
Expand Down
12 changes: 8 additions & 4 deletions src/dsp/p_conv.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ void p_conv_f32(const float *x, const float *h, float *r, int nx, int nh)
{
const float *xc = x;
float *rx = r;
for ( int i = 0; i < nx; i++) {
int i,j ;
for ( i = 0; i < nx+nh-1; i++) { *(rx++) = 0; }
rx = r ;
for ( i = 0; i < nx; i++) {
float xv = *xc++;
rx++;
for (int j = 0; j < nh; j++) {
*(rx + j) += xv * *(h + j);

for (j = 0; j < nh; j++) {
*(rx + j) += xv * *(h + j);
}
rx++;
}
}
38 changes: 17 additions & 21 deletions src/image/p_conv2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,39 @@
*
* @param cols Number of columns in input image
*
* @param msize Size of convolution kernel
* @param mrows number of rows in convolution kernel
*
* @param mcols number of cols in convolution kernel
*
* @return None
*
*/

void p_conv2d_f32(const float *x, float *m, float *r, int rows, int cols,
int msize)
int mrows, int mcols)

{
int i, j, k;
float P, part;
int i, j, ki, kj;
float P;
const float *px, *pm;
float *pr;

px = x;
pm = m;
pr = r;

for (i = msize * 0.5; i < (rows - msize * 0.5); i++) {
for (j = msize * 0.5; j < (cols - msize * 0.5); j++) {
for (i = 0; i < rows - mrows+1 ; i++) {
for (j = 0; j < cols - mcols+1 ; j++) {
P = 0.0f;
pm = m;
for (k = 0; k < msize; k++) {
p_dot_f32(px, pm, &part, msize);
P += part;
px += cols;
pm += msize;
pm = m+(mcols * mrows)-1;
for (ki=0 ; ki< mrows ; ki++){
for (kj=0 ; kj< mcols ; kj++){
P+= (*px++)* (*pm--) ;
}
px += cols - mcols;
}
*pr = P;
pr++;
// move image pointer one index forward compared to
// the position from before `for` loop
px += 1 - msize * cols;
px -= (mrows * cols) -1 ;
*(pr++) = P;
}
// move image pointer to the beginning of line
// beneath the current line
px += (int)(msize * 0.5) * 2;
px+=mcols-1 ;
}
}
40 changes: 39 additions & 1 deletion src/image/p_median3x3.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
#include <pal.h>
/*
* The following routines have been built from knowledge gathered
* around the Web. I am not aware of any copyright problem with
* them, so use it as you want.
* N. Devillard - 1998
*/

typedef float pixelvalue ;

#define PIX_SORT(a,b) { if ((a)>(b)) PIX_SWAP((a),(b)); }
#define PIX_SWAP(a,b) { pixelvalue temp=(a);(a)=(b);(b)=temp; }

/*----------------------------------------------------------------------------
Function : opt_med9()
In : pointer to an array of 9 pixelvalues
Out : a pixelvalue
Job : optimized search of the median of 9 pixelvalues
Notice : in theory, cannot go faster without assumptions on the
signal.
Formula from:
XILINX XCELL magazine, vol. 23 by John L. Smith

The input array is *NOT* modified in the process
The result array is guaranteed to contain the median
value
---------------------------------------------------------------------------*/

pixelvalue opt_med9(pixelvalue * pointer)
{
pixelvalue p[9];
memcpy(p, pointer, 9*sizeof(pixelvalue) );
PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
PIX_SORT(p[0], p[1]) ; PIX_SORT(p[3], p[4]) ; PIX_SORT(p[6], p[7]) ;
PIX_SORT(p[1], p[2]) ; PIX_SORT(p[4], p[5]) ; PIX_SORT(p[7], p[8]) ;
PIX_SORT(p[0], p[3]) ; PIX_SORT(p[5], p[8]) ; PIX_SORT(p[4], p[7]) ;
PIX_SORT(p[3], p[6]) ; PIX_SORT(p[1], p[4]) ; PIX_SORT(p[2], p[5]) ;
PIX_SORT(p[4], p[7]) ; PIX_SORT(p[4], p[2]) ; PIX_SORT(p[6], p[4]) ;
PIX_SORT(p[4], p[2]) ; return(p[4]) ;
}
/*
* A median 3x3 filter.
*
Expand Down Expand Up @@ -50,7 +88,7 @@ void p_median3x3_f32(const float *x, float *r, int rows, int cols)
buffer[buffer_col + 3] = *(px + cols);
buffer[buffer_col + 6] = *(px + cols + cols);

p_median_f32(buffer, pr, 9);
*pr = opt_med9(buffer);
pr++;
px++;
}
Expand Down