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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值