zoukankan      html  css  js  c++  java
  • Python OpenCV 图像处理初级使用

    # -*- coding: utf-8 -*-
    """
    Created on Thu Apr 25 08:11:32 2019

    @author: jiangshan
    """

    import cv2
    import numpy as np

    dataSetDir = 'D:\Workspace\Spyder\'
    #filename = dataSetDir+'Mask_10228690_15.tif'
    filename = dataSetDir+'Input_10228690_15.tiff'

    # 加载彩图
    img = cv2.imread(filename)
    # 灰度图模式加载一副彩图
    #img = cv2.imread(filename,0)
    # 0 - 1 二值化
    #Grayimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #ret, thresh = cv2.threshold(Grayimg, 0, 1,cv2.THRESH_BINARY)

    # 显示图像
    #cv2.imshow('image',img)
    #cv2.waitKey(0) #cv2.waitKey() 是键盘绑定函数。如果没有键盘输入,返回值为-1,如果这个参数为0,将会无限期的等待键盘输入。
    """
    cv2.destroyAllWindows() 删除任何建立的窗口。如果想删除特定的窗口可以使用cv2.destroyWindow(),在函数内输入想删除的窗口名。
    """
    # 保存图像
    cv2.imwrite('Input-test.png',img)
    #cv2.imwrite('Target.png',thresh)

    image = cv2.imread('Input-test.png',cv2.COLOR_RGB2GRAY)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gaus = cv2.GaussianBlur(gray,(3,3),0)#高斯模糊预处理
    edges = cv2.Canny(gaus, 50, 150, apertureSize=3)#Canny算子
    minLineLength = 300
    maxLineGap = 10
    lines = cv2.HoughLinesP(edges,1.0,np.pi/180,10,minLineLength,maxLineGap)#HoughLinesP直线特征提取
    line = lines[:,0,:]#提取为二维
    for x1, y1, x2, y2 in line:
    cv2.line(image, (x1, y1), (x2, y2), (0,0,255), 2)#绘线
    cv2.imshow('Detected houghline', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    #HoughCircles圆形特征提取
    circles1 = cv2.HoughCircles(edges,cv2.HOUGH_GRADIENT,1,
    100,param1=100,param2=30,minRadius=200,maxRadius=300)
    circles = circles1[0,:,:]#提取为二维
    circles = np.uint16(np.around(circles))#四舍五入,取整
    for i in circles[:]:
    cv2.circle(image,(i[0],i[1]),i[2],(255,0,0),5)#画圆
    cv2.circle(image,(i[0],i[1]),2,(255,0,255),10)#画圆心
    cv2.imshow('Detected houghCircles', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    image = cv2.imread('Input-test.png')
    image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)#将图像转化为灰度图像
    cv2.imshow("Original",image)
    cv2.waitKey()
    #拉普拉斯边缘检测
    lap = cv2.Laplacian(image,cv2.CV_64F)#拉普拉斯边缘检测
    lap = np.uint8(np.absolute(lap))##对lap去绝对值
    cv2.imshow("Laplacian",lap)
    cv2.waitKey()

    #Sobel边缘检测
    sobelX = cv2.Sobel(image,cv2.CV_64F,1,0)#x方向的梯度
    sobelY = cv2.Sobel(image,cv2.CV_64F,0,1)#y方向的梯度
    sobelX = np.uint8(np.absolute(sobelX))#x方向梯度的绝对值
    sobelY = np.uint8(np.absolute(sobelY))#y方向梯度的绝对值
    sobelCombined = cv2.bitwise_or(sobelX,sobelY)#
    cv2.imshow("Sobel X", sobelX)
    cv2.waitKey()
    cv2.imshow("Sobel Y", sobelY)
    cv2.waitKey()
    cv2.imshow("Sobel Combined", sobelCombined)
    cv2.waitKey()

    #Canny边缘检测
    canny = cv2.Canny(image,30,150)
    cv2.imshow("Canny",canny)
    cv2.waitKey()

    #Canny边缘提取
    import cv2 as cv
    def edge_demo(image):
    blurred = cv.GaussianBlur(image, (3, 3), 0)
    gray = cv.cvtColor(blurred, cv.COLOR_RGB2GRAY)
    # xgrad = cv.Sobel(gray, cv.CV_16SC1, 1, 0) #x方向梯度
    # ygrad = cv.Sobel(gray, cv.CV_16SC1, 0, 1) #y方向梯度
    # edge_output = cv.Canny(xgrad, ygrad, 50, 150)
    edge_output = cv.Canny(gray, 50, 150)
    cv.imshow("Canny Edge", edge_output)
    dst = cv.bitwise_and(image, image, mask= edge_output)
    cv.imshow("Color Edge", dst)
    src = cv.imread('Input-test.png')
    cv.namedWindow('input_image', cv.WINDOW_NORMAL) #设置为WINDOW_NORMAL可以任意缩放
    cv.imshow('input_image', src)
    edge_demo(src)
    cv.waitKey(0)
    cv.destroyAllWindows()

  • 相关阅读:
    HttpResponse对象、JsonResponse、StreamingHttpResponse、FileResponse
    for循环
    字符串功能
    (二)第一个Spring Boot工程
    (一)安装Docker和Kubernetes
    如何解决CHM打开乱码的问题
    [译]PyUnit—Python单元测试框架(1)
    正则表达式
    Java开发心得
    Shell获取文件后缀名
  • 原文地址:https://www.cnblogs.com/jeshy/p/10787155.html
Copyright © 2011-2022 走看看