По мотивам, но не только это...
- Распознавалка QR-кодов (повёрнутых, искажённых перспективой и т.д.), которая позволяет не только распознать QR, но и детерминировать искажение:
Код: Выделить всё
# -*- coding: utf-8 -*-
import numpy as np
import argparse
import imutils
from pyzbar import pyzbar
import cv2
import sys
import math
from common import *
ap = argparse.ArgumentParser() # construct the argument parse and parse the arguments
ap.add_argument( "-i", "--image", required=True, help="path to the image file" )
args = vars(ap.parse_args())
image = cv2.imread( args[ "image" ], cv2.IMREAD_LOAD_GDAL ) # load the image from disk
if image.ndim > 2:
image = cv2.cvtColor( image, cv2.COLOR_BGR2GRAY ) # convert image into grayscale
#thresh, n = thresh_opt( image, 70, 150, 10 )
thresh, n = thresh_opt( image, 50, 120, 10 ) # find optimal p4qr1.jpg
#print( '{} => {}'.format( thresh, n ) )
image = cv2.threshold( image, thresh, 255, cv2.THRESH_BINARY )[ 1 ]
barcodes = pyzbar.decode( image ) # find the barcodes in the image
n = len( barcodes )
print( '{} => {}'.format( thresh, n ) )
#print( barcodes[ 0 ].polygon )
image = cv2.bitwise_not( image ) # convert to monochrome
show( image, 2000 )
def angles( bar ):
def get_angl( p1, p2 ):
x = p1.x - p2.x
y = p1.y - p2.y
a = math.atan2( y, x ) * 180 / math.pi
if abs( y ) > abs( x ): a -= 90
else: a -= 180
if a > 180: a -= 360
if a < -180: a += 360
return a
poly = bar.polygon
return ( get_angl( poly[ 0 ], poly[ 3 ] ), get_angl( poly[ 1 ], poly[ 2 ] ) )
for angle in range( -45, 46, 5 ): # loop over the rotation angles
# rotated = imutils.rotate( image, angle )
rotated = imutils.rotate_bound( image, angle )
rotated = cv2.bitwise_not( rotated )
barcodes = pyzbar.decode( rotated ) # find the barcodes in the image
for barcode in barcodes:
qa = angles( barcode )
print( '{:+04d}: {}\t<{:.2f}|{:.2f}>°'.format( angle, barcode.polygon, qa[ 0 ], qa[ 1 ] ) )
if 0 == len( barcodes ):
print( '{:+04d}: .................'.format( angle ) )
key = show( rotated, 2000 )
if key == 27 or key == ord( 'q' ):
sys.exit()
Всего то ...
Код: Выделить всё
[olej@xenix barcode-scanner]$ python2 rotate_doc.py -i ../QR1/qr1.gif
50 -> 1
60 -> 1
70 -> 1
80 -> 1
90 -> 1
100 -> 1
110 -> 1
80 => 1
-045: [Point(x=32, y=156), Point(x=155, y=280), Point(x=279, y=156), Point(x=156, y=33)] <-44.77|-45.00>°
-040: [Point(x=33, y=146), Point(x=145, y=279), Point(x=278, y=167), Point(x=166, y=32)] <-40.60|-40.10>°
-035: [Point(x=32, y=132), Point(x=132, y=275), Point(x=275, y=175), Point(x=175, y=32)] <-34.97|-34.97>°
-030: [Point(x=31, y=119), Point(x=119, y=270), Point(x=270, y=183), Point(x=183, y=31)] <-30.07|-29.95>°
-025: [Point(x=30, y=104), Point(x=105, y=263), Point(x=262, y=189), Point(x=189, y=31)] <-24.66|-25.24>°
-020: [Point(x=30, y=90), Point(x=89, y=254), Point(x=253, y=194), Point(x=194, y=30)] <-20.10|-20.10>°
-015: [Point(x=28, y=73), Point(x=73, y=242), Point(x=242, y=196), Point(x=196, y=28)] <-15.00|-15.23>°
-010: [Point(x=27, y=57), Point(x=57, y=229), Point(x=229, y=199), Point(x=199, y=27)] <-9.89|-9.89>°
-005: [Point(x=25, y=40), Point(x=40, y=215), Point(x=215, y=199), Point(x=199, y=25)] <-4.93|-5.22>°
+000: [Point(x=23, y=23), Point(x=23, y=198), Point(x=199, y=199), Point(x=198, y=23)] <0.00|0.33>°
+005: [Point(x=25, y=199), Point(x=199, y=215), Point(x=215, y=40), Point(x=40, y=25)] <4.93|5.22>°
+010: [Point(x=27, y=199), Point(x=198, y=228), Point(x=229, y=57), Point(x=57, y=27)] <9.89|10.28>°
+015: [Point(x=28, y=196), Point(x=196, y=242), Point(x=242, y=73), Point(x=73, y=28)] <15.00|15.23>°
+020: [Point(x=30, y=194), Point(x=192, y=251), Point(x=254, y=89), Point(x=90, y=29)] <19.98|20.94>°
+025: [Point(x=31, y=189), Point(x=189, y=263), Point(x=263, y=105), Point(x=105, y=30)] <24.96|25.10>°
+030: [Point(x=31, y=183), Point(x=183, y=270), Point(x=270, y=119), Point(x=119, y=31)] <30.07|29.95>°
+035: [Point(x=32, y=175), Point(x=176, y=275), Point(x=275, y=132), Point(x=132, y=32)] <34.97|34.70>°
+040: [Point(x=32, y=166), Point(x=167, y=278), Point(x=279, y=145), Point(x=146, y=33)] <40.60|40.10>°
+045: [Point(x=33, y=156), Point(x=156, y=279), Point(x=280, y=155), Point(x=156, y=32)] <44.77|-45.00>°