# edge detection

Hi there, I’m implementing a function for canny edge detection(don’t want to implement through library) but getting a lot of trouble plz guide me how i can make it work.
Basically i’m converting this code from matlab function mentioned below:

Open MV CODE:

``````import sensor, image,time,math
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
img=sensor.snapshot()
numrow=sensor.width()
numcol=sensor.height()
def edgedetect(img,edgeNum,a):
for i in range[1:32]:
for j in range[1:33]:
imagi[i,j]=255
for col in range(1,numcol):
sv = (img(1,col) + img(2,col) + img(3,col)) / 3
ev=(img(30,col) + img(31,col) + img(32,col)) / 3
threshold= (0.3*(ev-sv)) + sv
temp[a]=threshold
a=a+1

for row in range(2,29):
if (img(row,col)>threshold) & (img(row + 1,col)>threshold) & (img(row+2,col)>threshold) & (img(row+3,col)>threshold):
if (img(row - 1,col)) > (0.1* img(row,col)):
edgeNum = edgeNum + 1
edgecoor[edgeNum,1] = col
edgecoor[edgeNum,2] = row - 1
imagi[row-1,col]=0
else:
edgeNum = edgeNum + 1
edgecoor[edgeNum,1] = col
edgecoor[edgeNum,2] = row
imagi[row,col]=0
break
return edgecoor,imagi,edgeNum

edgecoor,imagi,edgeNum=edgedetect(img,edgeNum=0,a=1)
``````

Mathlab code: Accurately working

``````function [edgecoor,image,edgeNum]=edgedetect(imagebuffer,edgeNum,a)
subplot(311)
imshow(imagebuffer)
imagebuffer=rgb2gray(imagebuffer);
subplot(312)
imshow(imagebuffer)
imagebuffer=imresize(imagebuffer, [32 31]);
%displayig original image
subplot(313)
imshow(imagebuffer)
[numrow,numcol]=size((imagebuffer));
for i=1:31
for j=1:32
image(i,j)=255;
end
end
for col=1:numcol                                                                                                            %Finding an edge in each column from top to bottom
sv = (imagebuffer(1,col) + imagebuffer(2,col) + imagebuffer(3,col)) / 3;                                             %detemining space value by taking average of top three pixels
ev=(imagebuffer(30,col) + imagebuffer(31,col) + imagebuffer(32,col)) / 3;                                            %detemining earth value by taking average of bottom three pixels
threshold= (0.3*(ev-sv)) + sv;
%Threshold value i.e.avg sv + (30%(e.v-s.v))
temp(a)=threshold;
a=a+1;
for row =2: 29
if (imagebuffer(row,col)>threshold) && (imagebuffer(row + 1,col)>threshold) && (imagebuffer(row+2,col)>threshold) && (imagebuffer(row+3,col)>threshold)         %verify that the current pixel and three below are higher than threshold
if (imagebuffer(row - 1,col)) > (0.1* imagebuffer(row,col))                                               %if col, then we check if the value is 10% of the pixel value
edgeNum = edgeNum + 1;
edgecoor(edgeNum,1) = col   ;                                                                              %if higher, then pixel is mapped as edge pixel of that rowumn
edgecoor(edgeNum,2) = row - 1;
image(row-1,col)=0;
else
edgeNum = edgeNum + 1;
edgecoor(edgeNum,1) = col;
edgecoor(edgeNum,2) = row;
image(row,col)=0;
end
break;
end
end
end
``````

`````` tags. See the button in the forum post editor.
``````
``````import sensor, image,time,math
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
img=sensor.snapshot()
numrow=sensor.width()
numcol=sensor.height()
def edgedetect(img,edgeNum,a):
for i in range[1:32]:
for j in range[1:33]:
imagi[i,j]=255
for col in range(1,numcol):
sv = (img(1,col) + img(2,col) + img(3,col)) / 3
ev=(img(30,col) + img(31,col) + img(32,col)) / 3
threshold= (0.3*(ev-sv)) + sv
temp[a]=threshold
a=a+1

for row in range(2,29):
if (img(row,col)>threshold) & (img(row + 1,col)>threshold) & (img(row+2,col)>threshold) & (img(row+3,col)>threshold):
if (img(row - 1,col)) > (0.1* img(row,col)):
edgeNum = edgeNum + 1
edgecoor[edgeNum,1] = col
edgecoor[edgeNum,2] = row - 1
imagi[row-1,col]=0
else:
edgeNum = edgeNum + 1
edgecoor[edgeNum,1] = col
edgecoor[edgeNum,2] = row
imagi[row,col]=0
break
return edgecoor,imagi,edgeNum

edgecoor,imagi,edgeNum=edgedetect(img,edgeNum=0,a=1)
``````

Here I fixed some of your “code”. Some variables are not defined, I just removed them. I don’t think it’s working as expected, and I don’t think you’ll have enough memory to implement this, but anyway:

``````sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

img=sensor.snapshot()
numrow=sensor.width()
numcol=sensor.height()

def edgedetect(img, edgeNum, a):
for i in range(1, 32):
for j in range(1, 33):
img.set_pixel(i, j, 255)

for col in range(1, numcol):
sv = (img.get_pixel(1, col) + img.get_pixel(2, col) + img.get_pixel(3, col)) / 3
ev = (img.get_pixel(30, col) + img.get_pixel(31, col) + img.get_pixel(32, col)) / 3
threshold = (0.3*(ev-sv)) + sv
a=a+1

for row in range(2,29):
if (img.get_pixel(row, col)>threshold) and (img.get_pixel(row + 1,col)>threshold) and (img.get_pixel(row+2,col)>threshold) and (img.get_pixel(row+3,col)>threshold):
if (img.get_pixel(row - 1, col)) > (0.1* img.get_pixel(row, col)):
edgeNum = edgeNum + 1
img.set_pixel(row-1, col, 0)
else:
edgeNum = edgeNum + 1
img.set_pixel(row, col, 0)
break
return img, edgeNum

img, edgeNum = edgedetect(img, edgeNum=0, a=1)
img=sensor.snapshot()
``````

Hi, do you have the ability to edit the C code? If you have a linux PC then you can. It doesn’t take that long to pull our firmware and compile it. In C you can do whatever algorithm you want.

how we can resize our image like
img=imresize(img, [32 31]);

how we can resize our images like
imagebuffer=imresize(imagebuffer, [32 31]);

Use the pool() methods. They don’t give you an exact size however, they just down sample.

Note that our image resizing functionality is limited because we made a decision not to put this in back when we were on the M4. It’s something I know needs to be added back. However, our current man power levels keeps it from getting worked on.

plz help me to get edges of the sensor image

``````import image
import sensor
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.B64X64)
sensor.skip_frames(time = 2000)

def edgedetect(img, edgeNum, a):

for i in range(1, 32):
for j in range(1, 33):
img.set_pixel(i, j, 255)

for col in range(1, numcol):
sv = (img.get_pixel(1, col) + img.get_pixel(2, col) + img.get_pixel(3, col)) / 3
ev = (img.get_pixel(29, col) + img.get_pixel(30, col) + img.get_pixel(31, col)) / 3
threshold = (0.3*(ev-sv)) + sv
a=threshold
a=a+1

for row in range(2,29):
if (img.get_pixel(row, col)>threshold) and (img.get_pixel(row + 1,col)>threshold) and (img.get_pixel(row+2,col)>threshold) and (img.get_pixel(row+3,col)>threshold):
if (img.get_pixel(row - 1, col)) > (0.1* img.get_pixel(row, col)):
edgeNum = edgeNum + 1
img.set_pixel(row-1, col, 0)
else:
edgeNum = edgeNum + 1
img.set_pixel(row, col, 0)
break
return img, edgeNum

img=sensor.snapshot()
img=img.mean_pool(2,2)
numrow=img.width()
numcol=img.height()
img, edgeNum = edgedetect(img, edgeNum=0, a=1)
img=sensor.snapshot()
``````

Hi, please see the filtering examples → mean filter with adaptive thresholding.

in my case that filter is not helpful so help me to improve this code so that i can do any further coding using the results.
i have attached the desired results.

Hi, did you try to adjust the filter offset value and kernel size? No algorithm is going to produce the image on the right perfectly. You have to turn different knobs and apply filtering operations until you get the right result.

I don’t really have time to write code anymore for users. I can just point you in the right direction.

Please try out the mean filter with adaptive thresholding again and try to adjust the kernel size and the offset value until you start getting a line of the edge. Once you get that we just need to do noise filtering to remove things we don’t want.

why after running this code my frame-buffer screen stuck. And help me to implement this attached algorithm.

``````import image
import sensor
import time
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.VGA)
sensor.skip_frames(time = 2000)
clock = time.clock() # Tracks FPS.
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
def edgedetect(img, edgeNum, a):
for i in range(0, 32):
for j in range(0, 32):
img.set_pixel(i, j, 255)
for col in range(0,32):
sv = (img.get_pixel(1, col) + img.get_pixel(2, col) + img.get_pixel(3, col)) / 3
ev = (img.get_pixel(29, col) + img.get_pixel(30, col) + img.get_pixel(31, col)) / 3
threshold = 10
for row in range(1,32):
if (img.get_pixel(row, col)>threshold) and (img.get_pixel(row + 1,col)>threshold) and (img.get_pixel(row+2,col)>threshold) and (img.get_pixel(row+3,col)>threshold):
if (img.get_pixel(row - 1, col)) > (0.1* img.get_pixel(row, col)):
edgeNum = edgeNum + 1
img.set_pixel(row-1, col,0),
else:
edgeNum = edgeNum + 1
img.set_pixel(row, col, 0)
break
save()
return img, edgeNum

img=sensor.snapshot()
img=img.mean_pool(20,15)
row=img.width()
col=img.height()
img, edgeNum = edgedetect(img, edgeNum=0, a=1)
img=sensor.snapshot()
print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
``````

Hi, the code gets stuck here:

``````while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
def edgedetect(img, edgeNum, a):
``````

Because you’ve broken the while loop.