edge detection

Discussion related to "under the hood" OpenMV topics.
hamidnawaz28
Posts: 9
Joined: Wed Oct 24, 2018 2:18 am

edge detection

Postby hamidnawaz28 » Sat Oct 27, 2018 6:56 am

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:

Code: Select all

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

Code: Select all

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
User avatar
kwagyeman
Posts: 2386
Joined: Sun May 24, 2015 2:10 pm

Re: edge detection

Postby kwagyeman » Sat Oct 27, 2018 8:01 pm

Please post your code in the

Code: Select all

 tags. See the button in the forum post editor.
Nyamekye,
hamidnawaz28
Posts: 9
Joined: Wed Oct 24, 2018 2:18 am

Re: edge detection

Postby hamidnawaz28 » Sun Oct 28, 2018 1:09 am

Code: Select all

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)
User avatar
iabdalkader
Posts: 593
Joined: Sun May 24, 2015 3:53 pm

Re: edge detection

Postby iabdalkader » Sun Oct 28, 2018 12:12 pm

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:

Code: Select all

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()
User avatar
kwagyeman
Posts: 2386
Joined: Sun May 24, 2015 2:10 pm

Re: edge detection

Postby kwagyeman » Sun Oct 28, 2018 11:50 pm

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.
Nyamekye,
hamidnawaz28
Posts: 9
Joined: Wed Oct 24, 2018 2:18 am

Re: edge detection

Postby hamidnawaz28 » Mon Oct 29, 2018 11:34 am

how we can resize our image like
img=imresize(img, [32 31]);
hamidnawaz28
Posts: 9
Joined: Wed Oct 24, 2018 2:18 am

Re: edge detection

Postby hamidnawaz28 » Mon Oct 29, 2018 11:36 am

how we can resize our images like
imagebuffer=imresize(imagebuffer, [32 31]);
User avatar
kwagyeman
Posts: 2386
Joined: Sun May 24, 2015 2:10 pm

Re: edge detection

Postby kwagyeman » Mon Oct 29, 2018 11:56 am

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.
Nyamekye,
hamidnawaz28
Posts: 9
Joined: Wed Oct 24, 2018 2:18 am

Re: edge detection

Postby hamidnawaz28 » Sat Nov 03, 2018 9:12 am

plz help me to get edges of the sensor image

Code: Select all

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()
User avatar
kwagyeman
Posts: 2386
Joined: Sun May 24, 2015 2:10 pm

Re: edge detection

Postby kwagyeman » Sat Nov 03, 2018 1:43 pm

Hi, please see the filtering examples -> mean filter with adaptive thresholding.
Nyamekye,
hamidnawaz28
Posts: 9
Joined: Wed Oct 24, 2018 2:18 am

Re: edge detection

Postby hamidnawaz28 » Sun Nov 04, 2018 8:38 am

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.
Attachments
Capture.JPG
User avatar
kwagyeman
Posts: 2386
Joined: Sun May 24, 2015 2:10 pm

Re: edge detection

Postby kwagyeman » Sun Nov 04, 2018 12:42 pm

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.
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 8 guests