Skip to content

Commit 2d345bd

Browse files
Add files via upload
1 parent 77e5209 commit 2d345bd

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

Intermediate/Object Measurement/1.jpg

83.3 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import cv2
2+
import utlis
3+
4+
###################################
5+
webcam = True
6+
path = '1.jpg'
7+
cap = cv2.VideoCapture(0)
8+
cap.set(10,160)
9+
cap.set(3,1920)
10+
cap.set(4,1080)
11+
scale = 3
12+
wP = 210 *scale
13+
hP= 297 *scale
14+
###################################
15+
16+
while True:
17+
if webcam:success,img = cap.read()
18+
else: img = cv2.imread(path)
19+
20+
imgContours , conts = utlis.getContours(img,minArea=50000,filter=4)
21+
if len(conts) != 0:
22+
biggest = conts[0][2]
23+
#print(biggest)
24+
imgWarp = utlis.warpImg(img, biggest, wP,hP)
25+
imgContours2, conts2 = utlis.getContours(imgWarp,
26+
minArea=2000, filter=4,
27+
cThr=[50,50],draw = False)
28+
if len(conts) != 0:
29+
for obj in conts2:
30+
cv2.polylines(imgContours2,[obj[2]],True,(0,255,0),2)
31+
nPoints = utlis.reorder(obj[2])
32+
nW = round((utlis.findDis(nPoints[0][0]//scale,nPoints[1][0]//scale)/10),1)
33+
nH = round((utlis.findDis(nPoints[0][0]//scale,nPoints[2][0]//scale)/10),1)
34+
cv2.arrowedLine(imgContours2, (nPoints[0][0][0], nPoints[0][0][1]), (nPoints[1][0][0], nPoints[1][0][1]),
35+
(255, 0, 255), 3, 8, 0, 0.05)
36+
cv2.arrowedLine(imgContours2, (nPoints[0][0][0], nPoints[0][0][1]), (nPoints[2][0][0], nPoints[2][0][1]),
37+
(255, 0, 255), 3, 8, 0, 0.05)
38+
x, y, w, h = obj[3]
39+
cv2.putText(imgContours2, '{}cm'.format(nW), (x + 30, y - 10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
40+
(255, 0, 255), 2)
41+
cv2.putText(imgContours2, '{}cm'.format(nH), (x - 70, y + h // 2), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
42+
(255, 0, 255), 2)
43+
cv2.imshow('A4', imgContours2)
44+
45+
img = cv2.resize(img,(0,0),None,0.5,0.5)
46+
cv2.imshow('Original',img)
47+
cv2.waitKey(1)
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import cv2
2+
import numpy as np
3+
4+
def getContours(img,cThr=[100,100],showCanny=False,minArea=1000,filter=0,draw =False):
5+
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
6+
imgBlur = cv2.GaussianBlur(imgGray,(5,5),1)
7+
imgCanny = cv2.Canny(imgBlur,cThr[0],cThr[1])
8+
kernel = np.ones((5,5))
9+
imgDial = cv2.dilate(imgCanny,kernel,iterations=3)
10+
imgThre = cv2.erode(imgDial,kernel,iterations=2)
11+
if showCanny:cv2.imshow('Canny',imgThre)
12+
contours,hiearchy = cv2.findContours(imgThre,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
13+
finalCountours = []
14+
for i in contours:
15+
area = cv2.contourArea(i)
16+
if area > minArea:
17+
peri = cv2.arcLength(i,True)
18+
approx = cv2.approxPolyDP(i,0.02*peri,True)
19+
bbox = cv2.boundingRect(approx)
20+
if filter > 0:
21+
if len(approx) == filter:
22+
finalCountours.append([len(approx),area,approx,bbox,i])
23+
else:
24+
finalCountours.append([len(approx),area,approx,bbox,i])
25+
finalCountours = sorted(finalCountours,key = lambda x:x[1] ,reverse= True)
26+
if draw:
27+
for con in finalCountours:
28+
cv2.drawContours(img,con[4],-1,(0,0,255),3)
29+
return img, finalCountours
30+
31+
def reorder(myPoints):
32+
#print(myPoints.shape)
33+
myPointsNew = np.zeros_like(myPoints)
34+
myPoints = myPoints.reshape((4,2))
35+
add = myPoints.sum(1)
36+
myPointsNew[0] = myPoints[np.argmin(add)]
37+
myPointsNew[3] = myPoints[np.argmax(add)]
38+
diff = np.diff(myPoints,axis=1)
39+
myPointsNew[1]= myPoints[np.argmin(diff)]
40+
myPointsNew[2] = myPoints[np.argmax(diff)]
41+
return myPointsNew
42+
43+
def warpImg (img,points,w,h,pad=20):
44+
# print(points)
45+
points =reorder(points)
46+
pts1 = np.float32(points)
47+
pts2 = np.float32([[0,0],[w,0],[0,h],[w,h]])
48+
matrix = cv2.getPerspectiveTransform(pts1,pts2)
49+
imgWarp = cv2.warpPerspective(img,matrix,(w,h))
50+
imgWarp = imgWarp[pad:imgWarp.shape[0]-pad,pad:imgWarp.shape[1]-pad]
51+
return imgWarp
52+
53+
def findDis(pts1,pts2):
54+
return ((pts2[0]-pts1[0])**2 + (pts2[1]-pts1[1])**2)**0.5
55+
56+

0 commit comments

Comments
 (0)