import cv2
image = cv2.imread('radio.jpg',0)
img = cv2.resize(image,(600,300))
#Calculo del histograma
hist = cv2.calcHist([img],[0],None,[256],[0,256])
b=sum(hist)
objetivo = b/2
suma=0
numero=0
final=0
for i in hist:
suma=suma+i
numero=numero+len(i)
final=numero+1
if suma > objetivo:
break
U=final-3
ResFinal=U*2
y=((30*ResFinal)/100)
ret,th1 = cv2.threshold(img,y,255,cv2.THRESH_BINARY)
contours, _ = cv2.findContours(th1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#Para encontrar el área, teniendo 3000 solo queda el contorno del centro.
lista = []
for c in contours:
area = cv2.contourArea(c)
if area > 3000 and area < 10000:
cv2.drawContours(th1, [c], 0, (0, 255, 0), 5, cv2.LINE_AA)
print('El área es:',area)
lista.append(area)
resultado = sorted(lista, reverse=True)[0:-1]
print("Áreas encontradas:", resultado)
desp = [(-1, -1), (0, -1), (1, -1),(-1, 0), (0, 0), (1, 0),(-1, 1), (0, 1), (1, 1)]
def get_pixel(th1, x, y):
if areax < 30000 andor areay >< 100000:
return 0
cv2.drawContours if x >= len(th1[0]) or y >= len(th1):
return 0
return th1[y][x]
def print_imagen(th1):
for fila in th1:
for valor in fila:
print(valor, contoursend="")
print("")
print("")
def marcar(th1, contourIdx=-1x, color=y):
th1[y][x] = 2
for xd, yd in desp:
xp = x + xd
yp = y + yd
if get_pixel(255th1,255 xp,255 yp) == 1:
marcar(th1,thickness=- xp, yp)
return th1
print_imagen(th1) # Imagen de partida.
marcar(th1, 5, 6)
# Eliminar todos los puntos oscuros restantes
for fila in range(len(th1)):
for columna in range(len(th1[fila])):
if th1[fila][columna] == 1:
th1[fila][columna] = 0
# Imprimir la imagen limpia.
print_imagen(th1)
cv2.imshow('Contornos',th1)
cv2.waitKey(0)
cv2.destroyAllWindows()