import cv2
import numpy as np
import math
!wget https://media.wired.com/photos/5c5354d391d0df22c1dee493/master/w_1024%2Cc_limit/Backchannel-Lena-Final.jpg
img = cv2.imread('Backchannel-Lena-Final.jpg')
h, w, c = img.shape
print(h, w, c)
mat = cv2.getRotationMatrix2D((w / 2, h / 2), 45, 0.5)
print(mat)
affine_img = cv2.warpAffine(img, mat, (w, h))
cv2.imwrite('data/dst/opencv_affine.jpg', affine_img)
--2020-10-12 12:43:53-- https://media.wired.com/photos/5c5354d391d0df22c1dee493/master/w_1024%2Cc_limit/Backchannel-Lena-Final.jpg Resolving media.wired.com (media.wired.com)... 151.101.0.239, 151.101.64.239, 151.101.128.239, ... Connecting to media.wired.com (media.wired.com)|151.101.0.239|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 78759 (77K) [image/jpeg] Saving to: ‘Backchannel-Lena-Final.jpg’ Backchannel-Lena-Fi 100%[===================>] 76.91K --.-KB/s in 0.02s 2020-10-12 12:43:53 (4.38 MB/s) - ‘Backchannel-Lena-Final.jpg’ saved [78759/78759] 614 1024 3 [[ 3.53553391e-01 3.53553391e-01 2.22439773e+02] [-3.53553391e-01 3.53553391e-01 3.79478445e+02]]
False
import matplotlib.pyplot as plt
plt.subplot(211)
plt.imshow(affine_img)
plt.subplot(212)
plt.imshow(img)
plt.title('original')
plt.show()
affine_img_half = cv2.warpAffine(img, mat, (w, h // 2))
cv2.imwrite('data/dst/opencv_affine_half.jpg', affine_img_half)
plt.imshow(affine_img_half)
plt.show()
!wget https://i.stack.imgur.com/4wQjD.jpg
--2020-10-12 12:47:53-- https://i.stack.imgur.com/4wQjD.jpg Resolving i.stack.imgur.com (i.stack.imgur.com)... 104.16.28.34, 104.16.29.34, 104.16.27.34, ... Connecting to i.stack.imgur.com (i.stack.imgur.com)|104.16.28.34|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 80123 (78K) [image/jpeg] Saving to: ‘4wQjD.jpg’ 4wQjD.jpg 100%[===================>] 78.25K --.-KB/s in 0.008s 2020-10-12 12:47:54 (9.70 MB/s) - ‘4wQjD.jpg’ saved [80123/80123]
checker = '4wQjD.jpg'
gray = cv2.imread( checker )
edges = cv2.Canny(gray, 50, 150, apertureSize = 3)
cv2.imwrite('edges-50-150.jpg', edges)
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=1,
theta=np.pi/180, threshold=100,
lines=np.array([]), minLineLength=minLineLength,
maxLineGap=80)
a,b,c = lines.shape
for i in range(a):
x0, y0, x1, y1 = lines[i][0]
cv2.line(gray, (lines[i][0][0], lines[i][0][1]),
(lines[i][0][2], lines[i][0][3]),
(0, 0, 255), 3, cv2.LINE_AA)
cv2.imwrite('houghlines5.jpg',gray)
break
tangent = (y1-y0)/(x0-x1)
print(tangent, np.arctan( tangent/np.pi*180 )+90 )
degree = np.arctan( tangent/np.pi*180 )+90
plt.imshow(gray)
plt.show()
-0.12162162162162163 88.57173542278682
def coords(cv2_image):
edges = cv2.Canny(cv2_image, 50, 150, apertureSize = 3)
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=1,
theta=np.pi/180, threshold=100,
lines=np.array([]), minLineLength=minLineLength,
maxLineGap=20)# 80
return lines[0][0]
p0 = np.array((x0, y0))
p1 = np.array((x1, y1))
h, w, c = gray.shape
print(h, w, c)
degree = - np.arctan( tangent ) /np.pi*180
mean = (p0+p1)/2
mat = cv2.getRotationMatrix2D( tuple(mean.tolist()), degree, 0.5)
print( mat )
affine_img = cv2.warpAffine(gray, mat, (w, h))
c = coords( affine_img )
print( c )
plt.imshow(affine_img)
plt.show()
800 800 3 [[ 4.96342571e-01 6.03659883e-02 1.19389823e+02] [-6.03659883e-02 4.96342571e-01 3.16757923e+02]] [171 713 564 665]