Demo entry 6677116

py

   

Submitted by anonymous on Dec 02, 2017 at 06:49
Language: Python. Code size: 5.9 kB.

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 12 15:29:04 2017

@author: Administrator
"""
import cv2
import numpy as np
import Queue



def read_img(filename):
    img = cv2.imread(filename,cv2.IMREAD_GRAYSCALE)
    return img

def sobel_operator(img):
    grad_x = img.astype('float64')
    grad_y = img.astype('float64')
    grad_img = img.astype('float64')
    for i in range(1,len(img)-1):
        for j in range(1,len(img[0])-1):
            grad_x[i][j] = p = float(img[i+1][j+1])+2*img[i][j+1]+img[i-1][j+1]-img[i-1][j-1]-2*img[i][j-1]-img[i+1][j-1]
            grad_y[i][j] = q = float(img[i-1][j-1])+2*img[i-1][j]+img[i-1][j+1]-img[i+1][j+1]-2*img[i+1][j]-img[i+1][j-1]
            grad_img[i][j] = np.sqrt(p**2+q**2)
        grad_x[i][-1] = p = float(img[i+1][0])+2*img[i][0]+img[i-1][0]-img[i-1][-2]-2*img[i][-2]-img[i+1][-2]
        grad_y[i][-1] = q = float(img[i-1][-2])+2*img[i-1][-1]+img[i-1][0]-img[i+1][0]-2*img[i+1][-1]-img[i+1][-2]
        grad_img[i][-1] = np.sqrt(p**2+q**2)
        grad_x[i][0] = p = float(img[i+1][1])+2*img[i][1]+img[i-1][1]-img[i-1][-1]-2*img[i][-1]-img[i+1][-1]
        grad_y[i][0] = q = float(img[i-1][-1])+2*img[i-1][0]+img[i-1][1]-img[i+1][1]-2*img[i+1][0]-img[i+1][-1]
        grad_img[i][0] = np.sqrt(p**2+q**2)
    for j in range(0,len(img[0])):
        grad_x[0][j]=0
        grad_y[0][j]=0
        grad_x[-1][j]=0
        grad_y[-1][j]=0
        grad_img[-1][j] = 0
        grad_img[0][j] = 0
    return grad_img,grad_x,grad_y


def canny_operator(img):
    grad_x = img.astype('float64')
    grad_y = img.astype('float64')
    grad_img = img.astype('float64')
    for i in range(0,len(img)-1):
        for j in range(0,len(img[0])-1):
            grad_x[i][j] = p = (float(img[i][j+1])-img[i][j]+img[i+1][j+1]-img[i+1][j])/2
            grad_y[i][j] = q = (float(img[i][j])-img[i+1][j]+img[i][j+1]-img[i+1][j+1])/2
            grad_img[i][j] = np.sqrt(p**2+q**2)
        grad_x[i][-1] = p = (float(img[i][-1])-img[i][-1]+img[i+1][-1]-img[i+1][-1])/2
        grad_y[i][-1] = q = (float(img[i][-1])-img[i+1][-1]+img[i][-1]-img[i+1][-1])/2
        grad_img[i][-1] = np.sqrt(p**2+q**2)
    for j in range(0,len(img[0])-1):
        grad_x[-1][j] = p = (float(img[-1][j+1])-img[-1][j]+img[-1][j+1]-img[-1][j])/2
        grad_y[-1][j] = q = (float(img[-1][j])-img[-1][j]+img[-1][j+1]-img[-1][j+1])/2
        grad_img[-1][j] = np.sqrt(p**2+q**2)
    grad_y[-1][-1] = grad_x[-1][-1] = grad_img[-1][-1] = 0
    return grad_img,grad_x,grad_y



def get_max(grad_img,grad_x, grad_y,img):
    grad = grad_img
    for i in range(1,len(img)-1):
        for j in range(1,len(img[0])-1):
            if abs(grad_x[i][j])>abs(grad_y[i][j]):
                weight = abs(grad_y[i][j]/grad_x[i][j])
                if grad_y[i][j]*grad_x[i][j] >=0:
                    dtemp1 = weight*grad_img[i+1][j-1] + (1-weight)*grad_img[i][j-1]
                    dtemp2 = weight*grad_img[i-1][j+1] + (1-weight)*grad_img[i][j+1]
                else:
                    dtemp1 = weight*grad_img[i-1][j-1] + (1-weight)*grad_img[i][j-1]
                    dtemp2 = weight*grad_img[i+1][j+1] + (1-weight)*grad_img[i][j+1]
            elif abs(grad_x[i][j])<abs(grad_y[i][j]):
                weight = abs(grad_x[i][j]/grad_y[i][j])
                if grad_y[i][j]*grad_x[i][j] >=0:
                    dtemp1 = weight*grad_img[i-1][j-1] + (1-weight)*grad_img[i-1][j]
                    dtemp2 = weight*grad_img[i+1][j+1] + (1-weight)*grad_img[i+1][j]
                else:
                    dtemp1 = weight*grad_img[i-1][j+1] + (1-weight)*grad_img[i-1][j]
                    dtemp2 = weight*grad_img[i+1][j-1] + (1-weight)*grad_img[i+1][j]
            else:
                if grad_y[i][j]==0:
                    dtemp1 = dtemp2 = 0
                else:
                    if grad_x[i][j]*grad_y[i][j]>0:
                        dtemp1 = grad_img[i-1][j+1]
                        dtemp2 = grad_img[i+1][j-1]
                    else:
                        dtemp1 = grad_img[i-1][j-1]
                        dtemp2 = grad_img[i+1][j+1]
            if grad_img[i][j]>=dtemp1 and grad_img[i][j]>=dtemp2 :
                pass
            else:
                grad[i][j] = 0
    for i in range(0,len(grad)):
        grad[i][-1] = 0
    for j in range(0,len(grad[0])):
        grad[-1][j] = 0
    return grad

def deal(img,i,j):
    def islegal(m,n,img):
        if m>=0 and m<len(img) and n>=0 and n<len(img[0]):
            return True
        return False
    q = Queue.Queue()
    q.put((i,j))
    while q.empty()==False:
        m,n = q.get()
        for k in range(m-1,m+2):
            for l in range(n-1,n+2):
                if islegal(k,l,img) and img[k][l]==1:
                    img[k][l]=255
                    q.put((k,l))
    return img

def getresult(grad,img,strong_border,weak_border):
    for i in range(0,len(grad)):
        for j in range(0,len(grad[0])):
            if grad[i][j]>=strong_border:
                img[i][j] = 255
            elif grad[i][j]>=weak_border:
                img[i][j] = 1
            else:
                img[i][j] = 0
    for i in range(0,len(grad)):
        for j in range(0,len(grad[0])):
            if img[i][j]==255:
                img = deal(img,i,j)
    return img



def canny(img):
    Gause_img = cv2.GaussianBlur(img,(3,3),0)
    grad_img, grad_x, grad_y = canny_operator(Gause_img)
    canny_img = get_max(grad_img,grad_x,grad_y,Gause_img)
    result_img = getresult(canny_img,Gause_img,20,10)
    return result_img




def main():
    img = read_img('1.jpg')
    img2 = canny(img)
    cv2.imshow('image',img2)
    cv2.waitKey(0)
    cv2.imwrite('1_other.jpg',img2)
#main()
img = read_img('3.jpg')
cv2.imwrite('3_canny.jpg',cv2.Canny(img,50,150))

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).