Skip to content

Commit

Permalink
fix issue in the references and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierbrgt committed Jul 4, 2024
1 parent 9e3f01b commit 655e000
Show file tree
Hide file tree
Showing 40 changed files with 132 additions and 92 deletions.
4 changes: 2 additions & 2 deletions Include/cv/feature_detection.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ uint16_t arm_cv_get_scratch_size_canny_sobel(int width);
extern void arm_cv_canny_edge_sobel(const arm_cv_image_gray8_t* ImageIn,
arm_cv_image_gray8_t* ImageOut,
q15_t* Buffer,
uint8_t low_threshold,
uint8_t high_threshold);
uint8_t lowThreshold,
uint8_t highThreshold);

#ifdef __cplusplus
}
Expand Down
129 changes: 110 additions & 19 deletions Source/FeatureDetection/arm_cannysobel.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <stdio.h>
#include "cv/feature_detection.h"
#include "dsp/basic_math_functions.h"
#include "dsp/fast_math_functions.h"
Expand All @@ -44,7 +44,11 @@ Macro that implement core operation of threshold and hysteresis for the canny ed
The different values of x are angle dependent see helper below, and relative to the 8 neighboring pixels
*/
#define DECISION(x0, x1, x2, x3, x4, x5, x6, x7, y0, thresh, width, datain, dataout, offs, mag, col, row) \
if (mag <= datain[((col - x0) % NB_LINE_BUF) * width + row - y0] || \
if(idx==width+1)\
{\
printf(" 1 %d 2%d 3%d\n 4%d 5%d\n 6%d 7%d 8%d\n center %d mag %d\n", datain[((col - 3) % NB_LINE_BUF) * width + row - 1],datain[((col - 3) % NB_LINE_BUF) * width + row ],datain[((col - 3) % NB_LINE_BUF) * width + row + 1],datain[((col - 2) % NB_LINE_BUF) * width + row - 1],datain[((col - 2) % NB_LINE_BUF) * width + row + 1],datain[((col - 1) % NB_LINE_BUF) * width + row - 1],datain[((col - 1) % NB_LINE_BUF) * width + row ],datain[((col - 1) % NB_LINE_BUF) * width + row + 1],datain[((col - 2) % NB_LINE_BUF) * width + row ],mag);\
}\
if (mag <= datain[((col - x0) % NB_LINE_BUF) * width + row - y0] || \
mag <= datain[((col - x1) % NB_LINE_BUF) * width + row + y0]) \
{ \
dataout[offs] = 0; \
Expand Down Expand Up @@ -79,7 +83,11 @@ The different values of x are angle dependent see helper below, and relative to
continue;

#define DECISION_LAST(x0, x2, x3, x4, x5, x6, x7, y0, thresh, width, datain, dataout, offs, mag, col, row) \
if (mag <= datain[((col - x0) % NB_LINE_BUF) * width + row - y0]) \
if(idx==width+1)\
{\
printf(" 1 %d 2%d 3%d\n 4%d 5%d\n 6%d 7%d 8%d\n i %d", datain[((col - 3) % NB_LINE_BUF) * width + row - 1],datain[((col - 3) % NB_LINE_BUF) * width + row ],datain[((col - 3) % NB_LINE_BUF) * width + row + 1],datain[((col - 2) % NB_LINE_BUF) * width + row - 1],datain[((col - 2) % NB_LINE_BUF) * width + row + 1],datain[((col - 1) % NB_LINE_BUF) * width + row - 1],datain[((col - 1) % NB_LINE_BUF) * width + row ],datain[((col - 1) % NB_LINE_BUF) * width + row + 1],datain[((col - 2) % NB_LINE_BUF) * width + row ]);\
}\
if (mag <= datain[((col - x0) % NB_LINE_BUF) * width + row - y0]) \
{ \
dataout[offs] = 0; \
continue; \
Expand Down Expand Up @@ -132,7 +140,11 @@ The different values of x are angle dependent see helper below, and relative to
DECISION_LAST(1, 1, 1, 2, 2, 1, 1, -1, threshold, width, datain, dataout, idx, mag, col, row)

#define THRESHOLDING_HYSTERESIS(angle, thresh, width, data_mag, data_out, idx, mag, x, y) \
if ((angle) < (DEG_TO_RAD_Q2_13(22))) \
if(idx==width+1)\
{\
printf(" anglet %d\n", angle);\
}\
if ((angle) < (DEG_TO_RAD_Q2_13(22))) \
{ \
VERTICAL_CASE(thresh, width, data_mag, data_out, idx, mag, x, y) \
} \
Expand All @@ -154,7 +166,11 @@ The different values of x are angle dependent see helper below, and relative to
}

#define THRESHOLDING_HYSTERESIS_BOTTOM_BORDER(angle, thresh, width, data_mag, data_out, idx, mag, x, y) \
if ((angle) < (DEG_TO_RAD_Q2_13(22))) \
if(idx==width+1)\
{\
printf(" angleb %d\n", angle);\
}\
if ((angle) < (DEG_TO_RAD_Q2_13(22))) \
{ \
VERTICAL_CASE_BOT_BORDER(thresh, width, data_mag, data_out, idx, mag, x, y) \
} \
Expand Down Expand Up @@ -312,7 +328,11 @@ static void arm_cv_gradient_magnitude_tail(int rowIdx, int rowIdxOut, int width,
uint8_t *data_in = imageIn->pData;
for (int y = MASK_16(width - 1); y < width; y++)
{
if ((y == 0 || y == width - 1) && rowIdx != 0 && rowIdx != imageIn->height - 1)
if(y==1)
{
printf("magintr %d\n", magOut[(rowIdx - 1) % NB_LINE_BUF * width + y]);
}
if ((y == 0 || y == width - 1) && rowIdx != 0 && rowIdx != imageIn->height - 1)
{
dataGrad2[rowIdxOut * width + y].y =
Q5_10_TO_Q15(data_in[(rowIdx - 1) * width + y] + (data_in[rowIdx * width + y] << 1) +
Expand All @@ -321,18 +341,34 @@ static void arm_cv_gradient_magnitude_tail(int rowIdx, int rowIdxOut, int width,
dataOut[rowIdx * width + y] = 0;
continue;
}
if (rowIdx == 0 || y == 0 || y == width - 1)
if(y==1)
{
printf("magintr %d\n", magOut[(rowIdx - 1) % NB_LINE_BUF * width + y]);
}
if(y==1)
{
printf("magintr %d\n", magOut[(rowIdx - 1) % NB_LINE_BUF * width + y]);
}
if (rowIdx == 0 || y == 0 || y == width - 1)
{
magOut[((((rowIdx - 1) % NB_LINE_BUF * width)) + y)] = 0;
dataOut[rowIdx * width + y] = 0;
continue;
}
if(y==1)
{
printf("idx compute %d, idx 1 %d, idx 2 %d\n", rowIdxOut * width + y, (rowIdx - 1) % NB_LINE_BUF * width + (y - 1), (rowIdx - 1) % NB_LINE_BUF * width + (y + 1));
}
dataGrad2[rowIdxOut * width + y].y = Q5_10_TO_Q15(
data_in[(rowIdx - 1) * width + y] + (data_in[rowIdx * width + y] << 1) + data_in[(rowIdx + 1) * width + y]);
dataGrad2[rowIdxOut * width + y].x =
Q5_10_TO_Q15(data_in[rowIdx * width + (y - 1)] + (data_in[rowIdx * width + (y)] << 1) +
data_in[rowIdx * width + (y + 1)]);
if (rowIdx == 1)
if(y==1)
{
printf("magintr %d\n", magOut[(rowIdx - 1) % NB_LINE_BUF * width + y]);
}
if (rowIdx == 1)
{
continue;
}
Expand All @@ -341,7 +377,11 @@ static void arm_cv_gradient_magnitude_tail(int rowIdx, int rowIdxOut, int width,
dataGrad2[(rowIdx - 1) % NB_LINE_BUF * width + (y + 1)].y;
dataGrad1[(rowIdx - 1) % NB_LINE_BUF * width + y].x = gradx;
dataGrad1[(rowIdx - 1) % NB_LINE_BUF * width + y].y = grady;
if (gradx == 0 && grady == 0)
if(y==1)
{
printf("magintr %d, gradx %d, grady %d\n y0 %d, y1 %d\n", magOut[(rowIdx - 1) % NB_LINE_BUF * width + y], gradx, grady, dataGrad2[(rowIdx - 1) % NB_LINE_BUF * width + (y - 1)].y, dataGrad2[(rowIdx - 1) % NB_LINE_BUF * width + (y + 1)].y);
}
if (gradx == 0 && grady == 0)
{
magOut[((((rowIdx - 1) % NB_LINE_BUF * width)) + y)] = 0;
dataOut[(rowIdx - 1) * width + y] = 0;
Expand All @@ -353,6 +393,10 @@ static void arm_cv_gradient_magnitude_tail(int rowIdx, int rowIdxOut, int width,
arm_sqrt_q31(out, &root);
out = Q31_TO_Q15(root);
magOut[(rowIdx - 1) % NB_LINE_BUF * width + y] = (q15_t)out;
if(y==1)
{
printf("magintr %d\n", magOut[(rowIdx - 1) % NB_LINE_BUF * width + y]);
}
}
}
#else
Expand All @@ -366,23 +410,35 @@ static void arm_cv_compute_buffer_line_canny_sobel(const arm_cv_image_gray8_t *i
uint8_t *data_in = imageIn->pData;
uint8_t *data_out = imageOut->pData;

grad2[xm * width].x = 0;
grad2[xm * width].y =
Q5_10_TO_Q15((data_in[(rowIdx - 1) * width] + (data_in[rowIdx * width] << 1) + data_in[(rowIdx + 1) * width]));
data_out[(rowIdx - 1) * width] = 0;
for (int y = 1; y < width - 1; y++)
{
if(y==1)
{
printf("magintr %d\n", magOut[(rowIdx - 1) % NB_LINE_BUF * width + y]);
}
idx = (rowIdx - 1) * width + y;

grad2[xm * width + y].y = Q5_10_TO_Q15(data_in[(rowIdx - 1) * width + y] + (data_in[rowIdx * width + y] << 1) +
data_in[(rowIdx + 1) * width + y]);
grad2[xm * width + y].x =
Q5_10_TO_Q15(data_in[rowIdx * width + (y - 1)] + (data_in[rowIdx * width + (y)] << 1) +
data_in[rowIdx * width + (y + 1)]);

if(y==1)
{
printf("idx compute %d, idx 1 %d, idx 2 %d\n", xm * width + y, ((rowIdx - 1) % NB_LINE_BUF) * width + (y - 1), ((rowIdx - 1) % NB_LINE_BUF) * width + (y + 1));
}
q15_t gradx = grad2[((rowIdx - 2) % NB_LINE_BUF) * width + y].x - grad2[(xm)*width + y].x;
q15_t grady = grad2[((rowIdx - 1) % NB_LINE_BUF) * width + (y - 1)].y -
grad2[((rowIdx - 1) % NB_LINE_BUF) * width + (y + 1)].y;
if (gradx == 0 && grady == 0)
if(y==1)
{
printf("magintr %d, gradx %d, grady %d\n y0 %d, y1 %d\n", magOut[(rowIdx - 1) % NB_LINE_BUF * width + y], gradx, grady, grad2[((rowIdx - 1) % NB_LINE_BUF) * width + (y - 1)].y, grad2[((rowIdx - 1) % NB_LINE_BUF) * width + (y + 1)].y);
}
if (gradx == 0 && grady == 0)
{
int idxp = (rowIdx - 1) % NB_LINE_BUF * width + y;

Expand All @@ -399,8 +455,17 @@ static void arm_cv_compute_buffer_line_canny_sobel(const arm_cv_image_gray8_t *i
out = Q31_TO_Q15(root);
grad1[(rowIdx - 1) % NB_LINE_BUF * width + y].y = grady;
grad1[(rowIdx - 1) % NB_LINE_BUF * width + y].x = gradx;
magOut[(rowIdx - 1) % NB_LINE_BUF * width + y] = (q15_t)out;
if(y==1)
{
printf("magitr %d\n", magOut[(rowIdx - 1) % NB_LINE_BUF * width + y]);
}
magOut[(rowIdx - 1) % NB_LINE_BUF * width + y] = (q15_t)out;
if(y==1)
{
printf("magitr %d\n", magOut[(rowIdx - 1) % NB_LINE_BUF * width + y]);
}
}
grad2[xm * width].x = 0;
grad2[xm * width + width - 1].y =
Q5_10_TO_Q15(data_in[(rowIdx - 1) * width + width - 1] + (data_in[rowIdx * width + width - 1] << 1) +
data_in[(rowIdx + 1) * width + width - 1]);
Expand Down Expand Up @@ -502,7 +567,8 @@ void arm_cv_canny_edge_sobel(const arm_cv_image_gray8_t *imageIn, arm_cv_image_g
x = 1;
data_grad2[x * width].x = 0;
data_out[x * width] = 0;
data_grad2[x * width].y = (data_in[(x - 1) * width] + (data_in[x * width] << 1) + data_in[(x + 1) * width]) << 5;
data_grad2[x * width].y = Q5_10_TO_Q15(data_in[(x - 1) * width] + (data_in[x * width] << 1) + data_in[(x + 1) * width]);
printf("datagrad %d, indice %d\n", data_grad2[x * width].y, x * width);
for (int y = 1; y < ((width) >> 4) + 1; y++)
{
int idx = width + ((y - 1) << 4) + 1;
Expand Down Expand Up @@ -543,7 +609,8 @@ void arm_cv_canny_edge_sobel(const arm_cv_image_gray8_t *imageIn, arm_cv_image_g
// Tail
arm_cv_gradient_magnitude_tail(x, x3, width, data_grad1, data_grad2, imageIn, data_mag, data_out);
}
// Core loop
printf("mag 1x1 %d\n", data_mag[width+1]);
// Core loop
for (int x = 3; x < imageIn->height; x++)
{
int x3 = x % NB_LINE_BUF;
Expand Down Expand Up @@ -610,6 +677,7 @@ void arm_cv_canny_edge_sobel(const arm_cv_image_gray8_t *imageIn, arm_cv_image_g
}
}
}
printf("mag 1x1 %d\n", data_mag[width+1]);
// Last lines
for (int x = imageIn->height; x < imageIn->height + 1; x++)
{
Expand Down Expand Up @@ -713,7 +781,7 @@ void arm_cv_canny_edge_sobel(const arm_cv_image_gray8_t *imageIn, arm_cv_image_g
}
data_out[x * width + width - 1] = 0;
x = 1;
data_grad2[x * width].y = (data_in[(x - 1) * width] + (data_in[x * width] << 1) + data_in[(x + 1) * width]);
data_grad2[x * width].y = Q5_10_TO_Q15(data_in[(x - 1) * width] + (data_in[x * width] << 1) + data_in[(x + 1) * width]);
data_out[x * width] = 0;
for (int y = 1; y < width - 1; y++)
{
Expand All @@ -729,13 +797,15 @@ void arm_cv_canny_edge_sobel(const arm_cv_image_gray8_t *imageIn, arm_cv_image_g
x = 2;
// first line
arm_cv_compute_buffer_line_canny_sobel(imageIn, imageOut, data_grad1, data_grad2, data_mag, x);
// core loop
printf("mag 1x1 %d\n", data_mag[width+1]);
// core loop
for (int x = 3; x < imageIn->height; x++)
{
arm_cv_compute_buffer_line_canny_sobel(imageIn, imageOut, data_grad1, data_grad2, data_mag, x);

for (int y = 1; y < width - 1; y++)
{
printf("a0\n");
int idx = (x - 2) * width + y;
int mag = data_mag[((x - 2) % NB_LINE_BUF) * (width) + y];
if (mag < low_threshold)
Expand All @@ -753,14 +823,20 @@ void arm_cv_canny_edge_sobel(const arm_cv_image_gray8_t *imageIn, arm_cv_image_g
}
}
}
printf("mag 1x1 %d\n", data_mag[width+1]);
// last line
x = imageIn->height;
data_out[x * width] = 0;
data_out[(x-1) * width] = 0;
for (int y = 1; y < width - 1; y++)
{
int idx = (x - 2) * width + y;
if(y==1)
{
printf("a1\n");
}
int idx = (x - 2) * width + y;
int mag = data_mag[((x - 2) % NB_LINE_BUF) * (width) + y];
data_out[idx + width] = 0;
printf("%d, %d\n", data_out[idx], data_out[1*width+1]);
if (mag < low_threshold)
{
data_out[idx] = 0;
Expand All @@ -777,9 +853,24 @@ void arm_cv_canny_edge_sobel(const arm_cv_image_gray8_t *imageIn, arm_cv_image_g
angle = 0;
}
arm_abs_q15(&angle, &angle, 1);
if(y==1)
{
printf("a1\n");
printf("%d, %d\n", data_out[idx], data_out[1*width+1]);
}
THRESHOLDING_HYSTERESIS_BOTTOM_BORDER(angle, high_threshold, width, data_mag, data_out, idx, mag, x, y)
if(y==1)
{
printf("a1\n");
printf("%d, %d\n", data_out[idx], data_out[1*width+1]);
}
}
if(y==1)
{
printf("%d\n", data_out[idx]);
}
}
data_out[x * width + width - 1] = 0;
printf("%d\n", data_out[1*width+1]);
data_out[(x-1) * width + width - 1] = 0;
}
#endif
Binary file modified Testing/board/RefPatterns/test_110_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_113_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_131_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_134_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_135_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_13_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_152_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_156_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_164_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_168_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_173_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_177_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_185_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_18_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_190_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_191_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_194_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_210_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_215_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_23_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_24_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_25_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_30_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_38_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_42_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_43_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_44_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_46_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_47_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_51_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_67_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_68_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_84_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_86_img_0.tiff
Binary file not shown.
Binary file modified Testing/board/RefPatterns/test_89_img_0.tiff
Binary file not shown.
4 changes: 2 additions & 2 deletions Testing/board/scripts/details/description.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ def canny_sobel_test(imgid, imgdim, funcid=2, img_type="gray8"):
"check" : SimilarImage(1)
}

def canny_sobel_test_autoref(imgid, imgdim, funcid=2, img_type="gray8"):
def canny_sobel_test_autoref(imgid, imgdim, funcid=1, img_type="gray8"):
return {"desc":f"Gauss {img_type} image {imgdim[0]}x{imgdim[1]}",
"funcid": funcid,
"useimg": [imgid],
"reference": CannyEdgeAutoRef(),
"check" : SimilarImage(1)
"check" : SimilarTensorFixp(0)
}
53 changes: 1 addition & 52 deletions Testing/board/scripts/details/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,55 +140,4 @@ def __call__(self,ref,result):

return False
return(True)

class SimilarImage(Comparison):
def __init__(self,t=0):
super().__init__()

self._t = t

def __call__(self,ref,result):
for s,d in zip(ref,result):
st = s.tensor
dt = d.tensor
# Cast to signed so that the difference below is giving
# the right value
if st.dtype == np.uint8:
st = st.astype(dtype=np.int16)
dt = dt.astype(dtype=np.int16)
if st.dtype == np.uint16:
st = st.astype(dtype=np.int32)
dt = dt.astype(dtype=np.int32)
if st.dtype == np.uint32:
st = st.astype(dtype=np.int64)
dt = dt.astype(dtype=np.int64)

diff = np.abs(st-dt)
errorVal = np.max(diff)
if errorVal > self._t:
MAXNB = 10
self.add_error(f"Different tensors. Max error = {errorVal}")
tooBig = diff > self._t
# Remove the channel to get the indice of the pixel
# and remove duplicates in case there may be errors for
# several channels of the same pixel
tooBigPos = list(np.argwhere(tooBig))
indices = list(set([tuple(x) for x in tooBigPos]))

allErrors = [diff[x] for x in indices]
nb_errors = len(allErrors)
indices = indices[:MAXNB]
allErrors = allErrors[:MAXNB]

ref = [st[x] for x in indices]
result = [dt[x] for x in indices]

ref=ref[:MAXNB]
result=result[:MAXNB]
if nb_errors > 10:
self.add_error(f"Latest {MAXNB} error indices: {indices}")
self.add_error(f"References : {ref}")
self.add_error(f"Results : {result}")
self.add_error(f"Number of errors : {nb_errors}")
return False
return(True)

Loading

0 comments on commit 655e000

Please sign in to comment.