基于Python提取图片所有峰值(含小峰值)的代码优化咨询
图片峰值提取代码优化需求
需求为从图片中提取所有峰值,不得排除小峰值。我已编写相关实现代码,但认为当前代码冗余度高、实现方式不够合理。
相关素材
现有实现代码
from PIL import Image from PIL import ImageDraw import matplotlib.pyplot as plt import requests # im = Image.open(requests.get('https://images.squarespace-cdn.com/content/v1/58ed35be6b8f5b20a1da1e10/1492403492765-JYID3G6AGHHKFW5PW8BU/noun_335050_cc_%E5%89%AF%E6%9C%AC.png', stream=True).raw) import cv2 import numpy as np img_grey = cv2.imread('photo.jpg', cv2.IMREAD_GRAYSCALE) thresh = 128 img_binary = cv2.threshold(img_grey, thresh, 255, cv2.THRESH_BINARY)[1] cv2.imwrite('save.jpg', img_binary) image = cv2.imread('save.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # threshold_level = 50 # coords = np.column_stack(np.where(gray < threshold_level)) im = Image.open("save.jpg") im = im.convert('RGB') col_black = (0, 0, 0) col_white = (255, 255, 255) pixels_color_Black_array = [] for y in range(im.size[1]): for x in range(im.size[0]): if sum(im.getpixel((x, y))) < 20: im.putpixel((x, y), (0, 0, 0)) else: im.putpixel((x, y), (255, 255, 255)) for y in range(im.size[1]): for x in range(im.size[0]): if x == 462 or x == 461 or y == 367: continue; if (im.getpixel((x - 1, y - 1)) == col_white and im.getpixel((x + 1, y - 1)) == col_white and im.getpixel((x + 1, y - 1)) == col_white) and \ (im.getpixel((x - 1, y)) == col_white and im.getpixel((x + 1, y)) == col_white) and \ (im.getpixel((x - 1, y + 1)) == col_black and im.getpixel((x + 1, y + 1)) == col_black) and \ im.getpixel((x, y)) == col_black: pixels_color_Black_array.append({'x': x, 'y': y, 'color': im.getpixel((x, y))}) pixels_color_Black_array_2px = [] for y in range(im.size[1]): for x in range(im.size[0]): if x == 462 or x == 461 or y == 367: continue; if (im.getpixel((x - 1, y - 1)) == col_white and im.getpixel((x, y - 1)) == col_white and im.getpixel((x, y - 1)) == col_white and im.getpixel((x + 2, y - 1)) == col_white) and \ (im.getpixel((x - 1, y)) == col_white and im.getpixel((x + 2, y)) == col_white) and \ (im.getpixel((x - 1, y + 1)) == col_black and im.getpixel((x + 2, y + 1)) == col_black) and \ im.getpixel((x, y)) == col_black and im.getpixel((x + 1, y)) == col_black: pixels_color_Black_array_2px.append({'x': x, 'y': y, 'color': im.getpixel((x, y))}) pixels_color_Black_array_3px = [] for y in range(im.size[1]): for x in range(im.size[0]): if x == 462 or x == 461 or y == 367: continue; if (im.getpixel((x - 2, y - 1)) == col_white and im.getpixel((x - 1, y - 1)) == col_white and im.getpixel((x, y - 1)) == col_white and im.getpixel((x + 1, y - 1 == col_white)) and im.getpixel((x + 2, y - 1)) == col_white) and \ (im.getpixel((x - 2, y)) == col_white and im.getpixel((x + 2, y)) == col_white) and \ (im.getpixel((x - 2, y + 1)) == col_black and im.getpixel((x + 2, y + 1)) == col_black) and \ im.getpixel((x - 1, y)) == col_black and im.getpixel((x, y)) == col_black and im.getpixel((x + 1, y)) == col_black: pixels_color_Black_array_3px.append({'x': x, 'y': y, 'color': im.getpixel((x, y))}) pixels_color_Black_array_4px = [] for y in range(im.size[1]): for x in range(im.size[0]): if x == 462 or x == 461 or x == 460 or y == 367: continue; if (im.getpixel((x - 2, y - 1)) == col_white and im.getpixel((x - 1, y - 1)) == col_white and im.getpixel((x, y - 1)) == col_white and im.getpixel((x + 1, y - 1)) == col_white and im.getpixel((x + 2, y - 1)) == col_white and im.getpixel((x + 3, y - 1)) == col_white) and \ (im.getpixel((x - 2, y)) == col_white and im.getpixel((x + 3, y)) == col_white) and \ (im.getpixel((x - 2, y + 1)) == col_black and im.getpixel((x + 3, y + 1)) == col_black) and \ im.getpixel((x - 1, y)) == col_black and im.getpixel((x, y)) == col_black and im.getpixel((x + 1, y)) == col_black: pixels_color_Black_array_4px.append({'x': x, 'y': y, 'color': im.getpixel((x, y))}) print(len(pixels_color_Black_array)) print(len(pixels_color_Black_array_2px)) print(len(pixels_color_Black_array_3px)) print(len(pixels_color_Black_array_4px)) # print(pixels_color_Black_array) for pik in pixels_color_Black_array: cv2.circle(image, (pik['x'], pik['y']), 2, (0, 0, 255), -1) for pik in pixels_color_Black_array_2px: cv2.circle(image, (pik['x'], pik['y']), 2, (255, 0, 0), -1) for pik in pixels_color_Black_array_3px: cv2.circle(image, (pik['x'], pik['y']), 2, (0, 255, 0), -1) for pik in pixels_color_Black_array_4px: cv2.circle(image, (pik['x'], pik['y']), 2, (255, 255, 0), -1) cv2.imshow('image', image) cv2.waitKey()
内容的提问来源于stack exchange,提问作者Vasiliy
相关产品推荐
相关产品推荐

