t4vvvvv

import cv2

import cv2
import numpy as np
from matplotlib import pyplot as plt
'''
使用霍夫变换检测直线

'''
# Load an image
img = cv2.imread('4682.png', 0)

# 高斯滤波
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_TOZERO)

# 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)

# Find contours in the binary image
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
    x,y,w,h=cv2.boundingRect(contour)
    if w>30 or h>30:
        print(x,y)
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)


# Display the result
cv2.imwrite('contours.png', img)
# cv2.imshow('Result', img)
# cv2.waitKey(0)

你可能感兴趣的:(python,opencv,开发语言)