Skip to content
Snippets Groups Projects
Commit aafb3f38 authored by ivan.rigo's avatar ivan.rigo
Browse files

added OCR sample

parent 70047ece
Branches
No related tags found
No related merge requests found
Showing
with 166 additions and 0 deletions
File added
Hough/chess.png

460 KiB

import sys
from typing import List
from typing import Tuple
import numpy as np
import numpy.typing as npt
import math
from PIL import Image
from enum import Enum
from matplotlib import pyplot as plt
Img = npt.NDArray[np.uint8]
HoughAcc = npt.NDArray[np.uint8]
Line = Tuple[int,int]
import cv2 as cv
class Isotropy(Enum):
ISO_90 = 1
ISO_45 = 2
def load_img(path:str) -> Img:
with Image.open(path) as im:
return np.array(im)
def show_img(img: Img,axe:plt.axes) -> None:
if len(img.shape) == 2:
axe.imshow(img,cmap="gray",vmin=0,vmax=256)
else :
axe.imshow(img)
def normalize(img: Img, new_range: Tuple[int, int] = (0, 255)) -> Img:
fMax = np.max(img)
fMin = np.min(img)
for y in range(img.shape[0]):
for x in range(img.shape[1]):
img[y][x] = round((img[y][x] - fMin) * ((new_range[1] - new_range[0])/(fMax-fMin)) + new_range[0])
return img.astype(int8)
def rgb_to_gray(img: Img) -> Img:
res = np.zeros((img.shape[0],img.shape[1]))
iteNp = np.nditer(img,flags=["multi_index"])
for i in np.ndindex(img.shape[:2]):
res[i] = 0.2989*img[i][0] + 0.5870*img[i][1] + 0.1140*img[i][2]
return normalize(res)
def xcorr(img: Img, kernel: Img) -> Img:
kLen = int(kernel.shape[0]/2)
xMax = img.shape[0]
yMax = img.shape[1]
xcorrRes = np.zeros((img.shape[0],img.shape[1]))
iteNp = np.nditer(img,flags=["multi_index"])
while not iteNp.finished :
x,y = iteNp.multi_index
#+1 since the right part is always taking the value before the one set ( 1,3) return an array between 1,2 and not 1,3
regToApply = img[x - kLen : x + kLen + 1, y - kLen : y + kLen + 1]
if not regToApply.shape == kernel.shape:
#We use those to start at the top left of the array
xCorner = x-kLen
yCorner = y-kLen
regToApply = np.zeros((kernel.shape[0],kernel.shape[1]))
for xK in range(kernel.shape[0]):
for yK in range(kernel.shape[1]):
if xCorner + xK >= xMax or xCorner + xK < 0 or yCorner + yK < 0 or yCorner + yK >= yMax :
regToApply[xK,yK] = 123
else:
regToApply[xK,yK] = img[xCorner + xK, yCorner + yK]
xcorrRes[x][y] = np.sum(np.multiply(regToApply,kernel))
iteNp.iternext()
return normalize(xcorrRes)
def blur_avg(img: Img, blur_size: int) -> Img:
kernBlur = np.zeros((blur_size,blur_size),dtype=np.float64)
moy = math.pow(blur_size,2)
kernBlur[0:blur_size,0:blur_size] = 1/moy
return xcorr(img, kernBlur)
def normalize(img: Img, new_range: Tuple[int, int] = (0, 255)) -> Img:
fMax = np.max(img)
fMin = np.min(img)
for y in range(img.shape[0]):
for x in range(img.shape[1]):
img[y][x] = 0 if img[y][x] == 0 else round((img[y][x] - fMin) * ((new_range[1] - new_range[0])/(fMax-fMin)) + new_range[0])
return img.astype(np.uint8)
def laplace(img: Img, isotropy: Isotropy) -> Img:
kernLap = np.zeros((3,3))
if isotropy == Isotropy.ISO_45:
kernLap[1, 0:3] = -1
kernLap[0:3, 1] = -1
kernLap[1,1] = 4
else:
kernLap[0:3, 0:3] = -1
kernLap[1,1] = 8
return xcorr(img,kernLap)
def hough_space(img: Img, angle_step: int = 1) -> HoughAcc:
step = range(0,180,angle_step)
xShape = img.shape[0]
yShape = img.shape[1]
accXSize = 180
accYSize = int(math.sqrt(pow(xShape,2) + pow(yShape,2)))
arrD = np.zeros((accXSize,accYSize),np.uint8)
for x in range(xShape):
for y in range(yShape):
if(img[x][y] == 255):
for ang in step:
res = int(x * math.cos(math.radians(ang)) + y * math.sin(math.radians(ang)))
#resAng = int(ang/angle_step)
arrD[ang,res] += 1
return arrD
def hough_lines(acc: HoughAcc) -> List[Line]:
resLine = list(tuple())
for x in range(acc.shape[0]):
for y in range(acc.shape[1]):
if(acc[x,y] == 255):
resLine.append((x,y))
return resLine
def pretraitement(img: Img) -> Img:
kernOC = np.ones((3,3),np.uint8 )
if(len(img.shape) == 3):
img = rgb_to_gray(img)
img = blur_avg(img, 3)
#v = cv.Canny(originalImg,0,255)
img = laplace(img, Isotropy.ISO_90)
ret, img = cv.threshold(img,125,255, cv.THRESH_BINARY)
return img - cv.erode(img,kernOC)
if(len(sys.argv) <= 1 ):
print("Not enough parameters")
exit()
angWished = 1
fog, axe = plt.subplots(2,2)
originalImg = load_img(sys.argv[1])
show_img(originalImg, axe[0,0])
axe[0,0].set_title("Original")
preTrait = pretraitement(originalImg)
show_img(preTrait, axe[0,1])
axe[0,1].set_title("Pre-Traitement")
accumulator = hough_space(preTrait,angWished)
ret, accSeuil = cv.threshold(accumulator,80,255,cv.THRESH_BINARY)
show_img(accumulator, axe[1,0])
lines = hough_lines(accSeuil)
axe[1,0].set_title("Accumulateur \n (Pré-seuillage)")
plt.show()
#a = (- costheta) / sintheta
#b = x*cosTheta + y*sinTheta
\ No newline at end of file
Hough/triangle.png

5.57 KiB

OCR/ImageTest/0_1.png

376 B

OCR/ImageTest/0_10.png

473 B

OCR/ImageTest/0_11.png

421 B

OCR/ImageTest/0_12.png

402 B

OCR/ImageTest/0_13.png

436 B

OCR/ImageTest/0_14.png

405 B

OCR/ImageTest/0_15.png

388 B

OCR/ImageTest/0_16.png

350 B

OCR/ImageTest/0_17.png

417 B

OCR/ImageTest/0_18.png

472 B

OCR/ImageTest/0_19.png

357 B

OCR/ImageTest/0_2.png

348 B

OCR/ImageTest/0_20.png

382 B

OCR/ImageTest/0_3.png

394 B

OCR/ImageTest/0_4.png

379 B

OCR/ImageTest/0_5.png

401 B

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment