109 lines
4.0 KiB
Python
109 lines
4.0 KiB
Python
|
import cv2
|
|||
|
import matplotlib.pyplot as plt
|
|||
|
import os
|
|||
|
|
|||
|
def plot_histogram_and_draw_lines(image_path, column_indices, y_range, results, image_index):
|
|||
|
# 讀取圖像
|
|||
|
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
|||
|
|
|||
|
# 檢查圖像是否成功讀取
|
|||
|
if image is None:
|
|||
|
print(f"Error: Unable to read the image '{image_path}'.")
|
|||
|
return
|
|||
|
|
|||
|
# 創建彩色圖像以便畫線
|
|||
|
image_color = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
|
|||
|
|
|||
|
for column_index in column_indices:
|
|||
|
# 檢查 column_index 是否在圖像範圍內
|
|||
|
if column_index >= image.shape[1]:
|
|||
|
continue
|
|||
|
|
|||
|
# 檢查 y_range 是否在圖像範圍內
|
|||
|
if max(y_range) >= image.shape[0]:
|
|||
|
continue
|
|||
|
|
|||
|
# 選擇特定的列和 y 坐標範圍
|
|||
|
column_data = image[y_range, column_index]
|
|||
|
|
|||
|
# 反轉 y 坐標範圍
|
|||
|
y_coordinates_reversed = list(y_range)[::-1]
|
|||
|
|
|||
|
# 查找從灰度值 250 開始並遵循規則的最高點
|
|||
|
start_index = next((i for i, value in enumerate(column_data) if value <= 250), None)
|
|||
|
if start_index is None:
|
|||
|
continue
|
|||
|
|
|||
|
start_y_coord = y_coordinates_reversed[start_index]
|
|||
|
|
|||
|
decreasing = True
|
|||
|
highest_point = None
|
|||
|
potential_highest = None
|
|||
|
|
|||
|
for i in range(start_index, len(column_data) - 40):
|
|||
|
current_value = column_data[i]
|
|||
|
next_values = column_data[i + 1:i + 5]
|
|||
|
|
|||
|
if decreasing:
|
|||
|
if all(next_value > current_value for next_value in next_values):
|
|||
|
decreasing = False
|
|||
|
potential_highest = (y_coordinates_reversed[i + 3], column_data[i + 3])
|
|||
|
else:
|
|||
|
if all(next_value > potential_highest[1] for next_value in next_values):
|
|||
|
potential_highest = (y_coordinates_reversed[i + 2], column_data[i + 2])
|
|||
|
elif all(next_value < current_value for next_value in next_values):
|
|||
|
highest_point = potential_highest
|
|||
|
break
|
|||
|
|
|||
|
if highest_point:
|
|||
|
highest_y_coord, highest_grayscale_value = highest_point
|
|||
|
|
|||
|
# 計算Y坐標的差值
|
|||
|
y_difference = start_y_coord - highest_y_coord
|
|||
|
|
|||
|
# 計算Y坐標差值乘以2.4/1000的結果
|
|||
|
result = y_difference * 2.4 / 1000
|
|||
|
|
|||
|
# 在圖像上畫線,從起點畫到最高點
|
|||
|
cv2.line(image_color, (column_index, image.shape[0] - start_y_coord),
|
|||
|
(column_index, image.shape[0] - highest_y_coord), (0, 0, 255), 1)
|
|||
|
|
|||
|
# 保存結果值和圖號到列表
|
|||
|
results.append((image_index, result))
|
|||
|
|
|||
|
# 顯示並保存帶有畫線的圖像
|
|||
|
plt.imshow(cv2.cvtColor(image_color, cv2.COLOR_BGR2RGB).transpose(1, 0, 2)) # 轉置圖像
|
|||
|
plt.title('Image with lines')
|
|||
|
plt.show()
|
|||
|
|
|||
|
# 獲取輸出圖像的文件名和路徑
|
|||
|
base_filename = os.path.splitext(os.path.basename(image_path))[0]
|
|||
|
output_filename = f'{base_filename}.png'
|
|||
|
output_path = os.path.join(os.path.dirname(image_path), output_filename)
|
|||
|
cv2.imwrite(output_path, image_color)
|
|||
|
|
|||
|
def process_folder(folder_path, column_indices, y_range):
|
|||
|
all_results = []
|
|||
|
image_index = 0
|
|||
|
for filename in os.listdir(folder_path):
|
|||
|
if filename.endswith(('.bmp', '.png', '.jpg', '.jpeg')):
|
|||
|
image_path = os.path.join(folder_path, filename)
|
|||
|
plot_histogram_and_draw_lines(image_path, column_indices, y_range, all_results, image_index)
|
|||
|
image_index += 1
|
|||
|
|
|||
|
# 打印所有結果值,每10個空一行
|
|||
|
for i, (image_index, result) in enumerate(all_results, start=1):
|
|||
|
print(f'Image {image_index + 1}: Result: {result:.4f}')
|
|||
|
if i % 10 == 0:
|
|||
|
print() # 插入空行
|
|||
|
|
|||
|
if __name__ == '__main__':
|
|||
|
folder_path = r"D:\Code\Project\Medeologix\Python\Size\TIS_test\joe\1092"
|
|||
|
x_range = 5472
|
|||
|
num_segments = 10
|
|||
|
|
|||
|
column_indices = [x * x_range // num_segments for x in range(num_segments)]
|
|||
|
y_range_partial = range(0, 3648) # 嘗試不同的範圍
|
|||
|
|
|||
|
process_folder(folder_path, column_indices, y_range_partial)
|