Advertisement
Guest User

threshold

a guest
Jan 24th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. # o código espera que a imagem esteja no mesmo diretório que ele
  6. try:
  7.   img = plt.imread('image.jpg')  # leitura da imagem como um numpy array
  8. except:
  9.   print("An exception occurred")
  10.   exit(0)
  11.  
  12. nrows, ncols = img.shape[:2]  # obtendo as dimensões da imagem linhas x colunas
  13. img_copy = img.copy()  # cópia da imagem(array) para ser alterado
  14. threshold = 150  # threshold predefinido
  15.  
  16. for i in range(nrows):
  17.     for j in range(ncols):
  18.         lum = img[i, j][0]*0.2126 + img[i, j][1]*0.7152 + img[i, j][2]*0.1722  # luminosidade
  19.         if lum >= threshold:
  20.             img_copy[i,j][:3] = 255  # definindo o pixel branco
  21.         else:
  22.             img_copy[i,j][:3] = 0  # definindo o pixel preto
  23.  
  24. plt.imsave('image_threshold.jpg', img_copy)
  25. plt.imshow(img_copy)
  26. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement