import cv2
import cv2
import numpy as np
from matplotlib import pyplot as plt
'''
'''
print(cv2.__version__)
# Load an image
img = cv2.imread('4682.png', 0)
# 平移 3种旋转,使用cv2.ROTATE_xxx进行选择
# # rotated_img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
# # rotated_img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
# rotated_img = cv2.rotate(img, cv2.ROTATE_180)
# cv2.imwrite('rotate.png', rotated_img)
#
# # 翻转 0表示上下,正数表示左右,负数表示上下左右都翻转
# # flip_img = cv2.flip(img, 0)
# # flip_img = cv2.flip(img, 1)
# flip_img = cv2.flip(img, -1)
# cv2.imwrite('flip_img.png', flip_img)
# 高斯滤波
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()
# 二值化
ret, thresh = cv2.threshold(gaussian, 160, 255, cv2.THRESH_TOZERO)
# 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 and h<10:
crop_img = 'cropped_image' + str(x)+'_'+str(y) + '.png'
print(x,y,w,h)
##裁剪矩形图像
cropped_image = img[ y-10:y+h+10,x-10:x+w+10]
# cv2.imwrite(crop_img, cropped_image)
gaussian = cv2.GaussianBlur(cropped_image, (3, 3), 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()
# 二值化
ret, thresh = cv2.threshold(cropped_image, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
plt.scatter(range(thresh.shape[0] * thresh.shape[1]), thresh.reshape(-1), s=1)
plt.title('Pixel Coordinates and Gray Values Scatter Plot')
plt.show()
##腐蚀与膨胀
# # Create a structuring element for morphology operations
# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (8, 8))
#
# # Dilate binary image to fill holes and gaps
# dilated = cv2.dilate(thresh, kernel)
#
# # Erode the dilated image to remove small isolated holes
# eroded = cv2.erode(dilated, kernel)
#
# # Show the filled and repaired image
# cv2.imshow('Repaired Image', eroded)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# cv2.putText(thresh,'best threshold: '+str(ret),(0,30),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,0),2)
# cv2.imshow('thresh', thresh)
# cv2.waitKey(0)
# cv2.destroyALLwindows()
cv2.imwrite('thresh.png', thresh)
k = np.ones((2,2),np.uint8)
dst = cv2.erode(thresh,k)
dst = cv2.dilate(dst, k)
# cv2.imshow('erode', dst)
# cv2.waitKey(0)
# cv2.destroyALLwindows()
edges = cv2.Canny(dst, 200, 250, apertureSize=3)
cv2.imwrite('canny.png', edges)
# Apply Hough Line Transform
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 40, minLineLength=10, maxLineGap=5)
print(lines)
# Draw the detected lines
line_img = np.zeros_like(cropped_image)
for line in lines:
rho, theta = line[0]
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
cv2.line(line_img, (x1, y1), (x2, y2), 255, 1)
linenum = 'HoughLinesP'+str(x1)+'_'+str(y1)+'.png'
cv2.imwrite(linenum, cropped_image)
print(lines)
##画矩形
cv2.rectangle(cropped_image, (x, y), (x + w, y + h), (0, 255, 0), 1)
# Display the result
cv2.imwrite('contours.png', img)
# cv2.imshow('Result', img)
# cv2.waitKey(0)