Python peaks from the picture.

Vasiliy12345

Новичок
Пользователь
Ноя 17, 2021
3
0
1
Вам нужно получить все пики с картинки, не исключены небольшие пики.
Я тут что-то писал, но костыль думаю переполнен.

Фото для кода (photo.jpg).
photo.jpg

My resultat.
33.png
```
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()
```

Помогите, пожалуйста.
 
Последнее редактирование:

regnor

Модератор
Команда форума
Модератор
Июл 7, 2020
2 672
478
83
without indentations, your code is not clear, insert the code as a code, observing the indentations, read more here how to ask a question

PS
ох уж эти англичане по имени Василий...
 

Vasiliy12345

Новичок
Пользователь
Ноя 17, 2021
3
0
1
Спасибо за совет, переделал
 

Форум IT Специалистов