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