opencv program-t3

import cv2
import numpy as np
from matplotlib import pyplot as plt

# Load an image
img = cv2.imread('4682.png', 0)
print(img.shape)
print(img.dtype)
#高斯滤波
gaussian = cv2.GaussianBlur(img, (5, 5), 0)
cv2.imwrite('gaussian.png',gaussian)
# Calculate histogram
hist = cv2.calcHist([gaussian], [0], None, [256], [0, 256])

# Plot histogram
plt.plot(hist)
plt.xlim([0, 256])
plt.show()
#二值化
# Apply Global Thresholding
ret, thresh = cv2.threshold(gaussian, 160, 255, cv2.THRESH_BINARY)

# Display the result
# cv2.imshow('Global Thresholding', thresh)
cv2.imwrite('Global Thresholding.png',thresh)

edges = cv2.Canny(thresh, 50, 150, apertureSize=3)
cv2.imwrite('canny.png',edges)

# Apply Hough Line Transform
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 40,minLineLength=30,maxLineGap=100)
print(lines)
# lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength=100, maxLineGap=10)

# 绘制直线
if lines is not None:
    for line in lines:
        x1, y1, x2, y2 = line[0]
        cv2.line(edges, (x1, y1), (x2, y2), (255, 0, 0), 5)
# Draw lines


# Display the result
# cv2.imshow('Hough Line Transform', img)
cv2.imwrite('Hough Line Transform.png',img)
cv2.waitKey(0)




你可能感兴趣的:(opencv,计算机视觉,python)