python目標定位(借鑑csdn上大神)

寫博客是爲了記錄下來,畢竟好多東西記不住,看過就忘了,收藏又太多,還不如搬運到本身博客下面,隨時可翻~~~函數

近期再學目標識別與定位,看着原理都很簡單,可是真本身作,又以爲困難重重。spa

csdn上一個大神發了一個蟲子的定位切割程序,跑了一下效果不錯,所以記錄下來,能夠在此基礎上改進。.net

import cv2
import numpy as np


def get_image(path):
    #獲取圖片
    img=cv2.imread(path)
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    return img, gray

def Gaussian_Blur(gray):
    # 高斯去噪
    blurred = cv2.GaussianBlur(gray, (9, 9),0)

    return blurred

def Sobel_gradient(blurred):
    # 索比爾算子來計算x、y方向梯度
    gradX = cv2.Sobel(blurred, ddepth=cv2.CV_32F, dx=1, dy=0)
    gradY = cv2.Sobel(blurred, ddepth=cv2.CV_32F, dx=0, dy=1)

    gradient = cv2.subtract(gradX, gradY)
    gradient = cv2.convertScaleAbs(gradient)

    return gradX, gradY, gradient

def Thresh_and_blur(gradient):  #濾波,二值化

    blurred = cv2.GaussianBlur(gradient, (9, 9),0)
    (_, thresh) = cv2.threshold(blurred, 90, 255, cv2.THRESH_BINARY)

    return thresh

def image_morphology(thresh):   #形態學,補齊邊緣
    # 創建一個橢圓核函數
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (25, 25))    #返回指定形狀和尺寸的結構元素,
    # 定義一個25x25的橢圓形內核
    # 執行圖像形態學, 細節直接查文檔,很簡單
    closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)    #開運算,開運算即爲先腐蝕再膨脹,目的是消除白色點點
    closed = cv2.erode(closed, None, iterations=4)    #腐蝕
    closed = cv2.dilate(closed, None, iterations=4)   #膨脹

    return closed

def findcnts_and_box_point(closed):  #尋找目標輪廓和中心點
    # 這裏opencv3返回的是三個參數
    (_, cnts, _) = cv2.findContours(closed.copy(),
        cv2.RETR_LIST,
        cv2.CHAIN_APPROX_SIMPLE)
    c = sorted(cnts, key=cv2.contourArea, reverse=True)[0]    #倒序排列
#cv2.contourArea是輪廓的面積,所以是按照面積由大到小的順序排序
    # compute the rotated bounding box of the largest contour
    rect = cv2.minAreaRect(c)
    #旋轉的邊界矩形,面積最小,返回值爲Box2D結構,分別爲左上角座標、寬和高,旋轉角度
    box = np.int0(cv2.boxPoints(rect))
#搭配cv2.minAreaRect函數,用於繪製旋轉邊界矩形
    return box

def drawcnts_and_cut(original_img, box):   #畫輪廓
    # 由於這個函數有極強的破壞性,全部須要在img.copy()上畫
    # draw a bounding box arounded the detected barcode and display the image
    draw_img = cv2.drawContours(original_img.copy(), [box], -1, (0, 0, 255), 3)    #繪製全部輪廓

    Xs = [i[0] for i in box]
    Ys = [i[1] for i in box]
    x1 = min(Xs)
    x2 = max(Xs)
    y1 = min(Ys)
    y2 = max(Ys)
    hight = y2 - y1
    width = x2 - x1
    crop_img = original_img[y1:y1+hight, x1:x1+width]

    return draw_img, crop_img

def walk():

    img_path = r'F:\pycharm\test\iterable\cz.png'
    save_path = r'F:\pycharm\test\iterable\cz_save.png'
    original_img, gray = get_image(img_path)
    blurred = Gaussian_Blur(gray)
    gradX, gradY, gradient = Sobel_gradient(blurred)
    thresh = Thresh_and_blur(gradient)
    closed = image_morphology(thresh)
    box = findcnts_and_box_point(closed)
    draw_img, crop_img = drawcnts_and_cut(original_img,box)
    # 暴力一點,把它們都顯示出來看看
    cv2.imshow('original_img', original_img)
    cv2.imshow('blurred', blurred)
    cv2.imshow('gradX', gradX)
    cv2.imshow('gradY', gradY)
    cv2.imshow('final', gradient)
    cv2.imshow('thresh', thresh)
    cv2.imshow('closed', closed)
    cv2.imshow('draw_img', draw_img)
    cv2.imshow('crop_img', crop_img)
    cv2.waitKey(20171219)
    cv2.imwrite(save_path, crop_img)

walk()

但願本身早日寫出實現功能的代碼。code

原做網址爲:https://blog.csdn.net/sinat_36458870/article/details/78825571。blog

感謝。排序

相關文章
相關標籤/搜索