Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from __future__ import print_function
import cv2
import numpy as np
import align_images as align
import findGabarito as gabarito
import contornosInternos
import bubble as bubble
import imutils
from imutils.perspective import four_point_transform
from imutils import contours
import bitwiseImage as bt
import roi as r
from matplotlib import pyplot as plt
import h5py
import numpy as np
from skimage import morphology
#importação da imagem
im = '../template/templatePreenchido.png'
#alinhamento da imagem
imagemAlinhada = align.alignImages(im)
cv2.imwrite('../processadas/processadaAlinhada.png', imagemAlinhada)
gabarito = gabarito.findGabarito(imagemAlinhada)
dim = (1595,556)
gabarito = cv2.resize(gabarito, dim, interpolation=cv2.INTER_CUBIC )
cv2.imwrite('../processadas/gabarito.png', gabarito)
gabaritoInteresse = cv2.imread('../processadas/gabarito.png')
bolhas, umArrayDePontos, contornosCirculos = bubble.bolhas(gabaritoInteresse)
nomalizacao = np.ones(dim)
gabaritoInteresse = cv2.addWeighted(gabaritoInteresse, 1.07, np.zeros(gabaritoInteresse.shape, gabaritoInteresse.dtype),0,0)
gabaritoInteresse = cv2.normalize(gabaritoInteresse,nomalizacao,150,255, cv2.NORM_MINMAX)
gabaritoInteresse = cv2.cvtColor(gabaritoInteresse,cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gabaritoInteresse, (17,17),1)
thresh = cv2.adaptiveThreshold(gabaritoInteresse, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
edged = cv2.Canny(blurred, 100,200)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)
heirarchy = cnts[1][0]
cnts = cnts[0] if imutils.is_cv4() else cnts[1]
questions = []
for c in cnts:
(x, y, w, h) = cv2.boundingRect(c)
ar = w / float(h)
if (w >= 20 and h >= 20) and (w <= 25 and h <= 25) and ar >= 0.7 and ar <= 1.3:
box = [(x//5)*5, y]
questions.append([c, box])
#print(x, y)
#cv2.rectangle(gabarito, (x, y), (x+w, y+h), (255, 0, 0), 1)
questions = sorted(questions, key=lambda q: q[1][1])
questionCnts = []
'''
Agora estamos classificando da esquerda para a direita tomando um lote de 30 contornos
que são basicamente uma linha inteira e, em seguida, classificá-los a partir da ordem crescente de x
'''
boxes = []
for i in np.arange(0, len(questions), 30):
# take a row of bubbles
q = list(questions[i: i+30])
for o in q:
boxes.append(o[1])
q = sorted(q, key=lambda k: k[1][0])
for o in q:
questionCnts.append(o[0])
posicaoRespostas = np.empty(0,int)
for (q, i) in enumerate(np.arange(0, len(questionCnts), 30)):
cnts = contours.sort_contours(questionCnts[i:i+30])[0]
for (l ,k )in enumerate(cnts):
(x, y, w, h) = cv2.boundingRect(k)
if (w >= 20 and h >= 20) and (w <= 25 and h <= 25) and ar >= 0.7 and ar <= 1.3:
box = [(x//5)*5, y]
#print(x, y)
posicaoRespostas = np.append(posicaoRespostas,(l))
cv2.rectangle(bolhas, (x, y), (x+w, y+h), (0, 0, 255), 1)
cv2.imshow("Bolhas",bolhas)
cv2.waitKey(0)
cv2.destroyAllWindows()
exit()
@andreemidio

This comment has been minimized.

Copy link
Owner Author

commented Jul 23, 2019

Test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.