传统图像技术的边缘提取

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cv2
import os
import numpy as np


def laplacian(img, ksize=3):
    laplacian = cv2.Laplacian(img, cv2.CV_16S, ksize=ksize)
    dst = cv2.convertScaleAbs(laplacian)
    cv2.imshow('laplacian', dst)


def scharr(img):
    x = cv2.Sobel(img, cv2.CV_16S, 1, 0, ksize=-1)
    y = cv2.Sobel(img, cv2.CV_16S, 0, 1, ksize=-1)
    Scharr_absX = cv2.convertScaleAbs(x)  # convert 转换  scale 缩放
    Scharr_absY = cv2.convertScaleAbs(y)
    result = cv2.addWeighted(Scharr_absX, 0.5, Scharr_absY, 0.5, 0)
    cv2.imshow('img', img)
    cv2.imshow('Scharr_absX', Scharr_absX)
    cv2.imshow('Scharr_absY', Scharr_absY)
    cv2.imshow('result', result)


def sobel(img):
    x = cv2.Sobel(img, cv2.CV_16S, 1, 0)  # 1,0代表只计算x方向计算边缘
    y = cv2.Sobel(img, cv2.CV_16S, 0, 1)  # 0,1代表只在y方向计算边缘
    absX = cv2.convertScaleAbs(x)
    absY = cv2.convertScaleAbs(y)
    dst = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
    cv2.imshow("absX", absX)
    cv2.imshow("absY", absY)
    cv2.imshow("Result", dst)


def canny(img):
    blur = cv2.GaussianBlur(img, (3, 3), 0)  # 用高斯滤波处理原图像降噪
    canny = cv2.Canny(blur, 50, 120)  # 50是最小阈值,150是最大阈值
    # cv2.imshow('canny', canny)
    return canny

img_folder = "./images_ganfeng/008"
def all_img():
    img_paths = os.listdir(img_folder)
    for img_path in img_paths:
        img_path = os.path.join(img_folder, img_path)
        print(img_path)
        img = cv2.imread(img_path, 0)

        # sobel(img)
        # scharr(img)
        # laplacian(img, 3)
        canny(img)

        cv2.imshow("src", img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

def get_mask(srcImg, points):
    mask = np.zeros(srcImg.shape, dtype=np.uint8)
    for point in points:
        point = np.array(point)
        roi_points = []
        roi_points.append(point.astype(np.int32))
        cv2.polylines(mask, roi_points, True, (0, 0, 0))
        cv2.fillPoly(mask, roi_points, (255, 255, 255))

    # cv2.imshow("mask", mask)
    # cv2.waitKey(0)
    return mask


img_path = "./images2/size1side1/size1side1_000180.png"  # size1side1_000158  size1side1_000119
img_path = "./images_ganfeng/008/008_000054.png"  # 001_000119.png
roi_points = [[[92, 495], [711, 177], [717, 266], [76, 568]],
              [[71, 570], [679, 608], [644, 684], [33, 612]],
              [[60, 784], [640, 1201], [471, 1196], [12, 899]]]
def single_img():
    img = cv2.imread(img_path, 0)
    color_img = cv2.imread(img_path)
    mask = get_mask(img, roi_points)

    # sobel(roi)
    # scharr(roi)
    # laplacian(roi, 3)
    edge = canny(img)
    roi = cv2.bitwise_and(edge, mask)

    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
    roi = cv2.morphologyEx(roi, cv2.MORPH_CLOSE, kernel)

    contours_area = []
    contours, hierarchy = cv2.findContours(roi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    for i, contour in enumerate(contours):
        area = cv2.contourArea(contour)
        contours_area.append([area, i])

    contours_area.sort(reverse=True)
    print(contours_area)

    ret_contours = contours_area[:8]
    print(ret_contours)
    ret_mask = np.zeros(img.shape, dtype=np.uint8)
    for ret_contour in ret_contours:
        cv2.fillPoly(ret_mask, [contours[ret_contour[1]]], (255, 255, 255))
        cv2.fillPoly(color_img, [contours[ret_contour[1]]], (0, 0, 255))

    cv2.imshow("roi_1", edge)
    cv2.imshow("roi_2", roi)
    cv2.imshow("color_img", color_img)
    cv2.imshow("ret_mask", ret_mask)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    cv2.imwrite("edge.png", color_img)


def main():
    # all_img()
    single_img()


if __name__ == "__main__":
    main()

 

猜你喜欢

转载自blog.csdn.net/qq_31112205/article/details/127536330