|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on Wed Jun 9 14:22:21 2021 |
| 4 | +
|
| 5 | +@author: 이병화 |
| 6 | +""" |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import cv2 |
| 10 | +import glob |
| 11 | +import sys |
| 12 | +import os |
| 13 | +import cv2.aruco |
| 14 | + |
| 15 | + |
| 16 | +''' |
| 17 | +criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) |
| 18 | +
|
| 19 | +objp = np.zeros((6*7,3), np.float32) |
| 20 | +objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2) |
| 21 | +
|
| 22 | +objpoints = [] |
| 23 | +imgpoints = [] |
| 24 | +
|
| 25 | +images = glob.glob('./chess/*.jpg') |
| 26 | +#images = glob.glob('./calibration/*.png') |
| 27 | +
|
| 28 | +
|
| 29 | +for fname in images: |
| 30 | + img = cv2.imread(fname) |
| 31 | + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
| 32 | + ret, corners = cv2.findChessboardCorners(gray, (7,6), None) |
| 33 | + |
| 34 | + if ret == True: |
| 35 | + objpoints.append(objp) |
| 36 | + corners2 = cv2.cornerSubPix(gray,corners, (11,11), (-1,-1), criteria) |
| 37 | + imgpoints.append(corners) |
| 38 | + |
| 39 | + img = cv2.drawChessboardCorners(img, (7,6), corners2, ret) |
| 40 | + cv2.imshow('img', img) |
| 41 | + cv2.waitKey(500) |
| 42 | + |
| 43 | +cv2.destroyAllWindows() |
| 44 | +
|
| 45 | +ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None) |
| 46 | +
|
| 47 | +img = cv2.imread('./chess/KakaoTalk_20210512_224651717_15.jpg') |
| 48 | +#img = cv2.imread('./calibration/00010.png') |
| 49 | +h, w = img.shape[:2] |
| 50 | +newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h)) |
| 51 | +
|
| 52 | +dst = cv2.undistort(img, mtx, dist, None, newcameramtx) |
| 53 | +
|
| 54 | +x, y, w, h = roi |
| 55 | +dst = dst[y:y+h, x:x+w] |
| 56 | +cv2.imwrite('calibresult.png', dst) |
| 57 | +
|
| 58 | +tot_error = 0 |
| 59 | +for i in range(len(objpoints)): |
| 60 | + imgpoints2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist) |
| 61 | + error = cv2.norm(imgpoints[i],imgpoints2, cv2.NORM_L2)/len(imgpoints2) |
| 62 | + tot_error += error |
| 63 | +print("total error: ", tot_error/len(objpoints)) |
| 64 | +''' |
| 65 | + |
| 66 | +# linedetection and following |
| 67 | + |
| 68 | +# edge검출 ROI처리 |
| 69 | +# 카메라 사용 |
| 70 | +#cap1 = cv2.VideoCapture(0) |
| 71 | + |
| 72 | +#if not cap1.isOpened(): |
| 73 | + #print('Camera open failed!') |
| 74 | + #sys.exit() |
| 75 | + |
| 76 | +#mtx = np.load('mtx.npy') |
| 77 | +#dist = np.load('dist.npy') |
| 78 | + |
| 79 | +#img = cv2.imread('./Inteld435i/00006_image.png') |
| 80 | +#h, w = img.shape[:2] |
| 81 | +#newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w,h), 1, (w,h)) |
| 82 | + |
| 83 | +#dst = cv2.undistort(img, mtx, dist, None, newcameramtx) |
| 84 | + |
| 85 | +#x, y, w, h = roi |
| 86 | +#dst = dst[y:y+h, x:x+w] |
| 87 | +#cap1 = cv2.imwrite('calibresult.png', dst) |
| 88 | + |
| 89 | + |
| 90 | +cap1 = cv2.imread('./Inteld435i/00070_image.png') |
| 91 | +#cap1 = cv2.imread('./image/image_018.jpg') |
| 92 | + |
| 93 | +caphsv = cv2.cvtColor(cap1, cv2.COLOR_BGR2HSV) |
| 94 | +h,s,v = cv2.split(caphsv) |
| 95 | + |
| 96 | +edges = cv2.Canny(s,100,200) |
| 97 | + |
| 98 | +roi_h = edges.shape[0] |
| 99 | +roi_w = edges.shape[1] |
| 100 | + |
| 101 | +region = np.array([ |
| 102 | + [[60, roi_h], [285, 240],[330, 230], [roi_w, 430]] |
| 103 | + ], dtype = np.int32) |
| 104 | + |
| 105 | +mask = np.zeros_like(s) |
| 106 | +cv2.fillPoly(mask, region, 255) |
| 107 | +region = np.array([ |
| 108 | + [[140, roi_h], [290, 230],[350, 230], [600, 480]] |
| 109 | + ], dtype = np.int32) |
| 110 | +cv2.fillPoly(mask,region, 0) |
| 111 | + |
| 112 | +roimg = cv2.bitwise_and(edges, mask) |
| 113 | + |
| 114 | +cv2.imshow('Canny',edges) |
| 115 | +cv2.imshow('ROI',roimg) |
| 116 | + |
| 117 | +#hought transform 차선검출 |
| 118 | +lines = cv2.HoughLines(roimg, 1, np.pi/180, 180) |
| 119 | +#lines = cv2.HoughLinesP(roimg, 1,np.pi/180,50,100,10) |
| 120 | +cap2 = cap1 |
| 121 | + |
| 122 | +if lines is not None: |
| 123 | + for line in lines : |
| 124 | + #x1,y1,x2,y2 = line[0] |
| 125 | + r, theta = line[0] |
| 126 | + tx, ty = np.cos(theta), np.sin(theta) |
| 127 | + x0, y0 = tx*r, ty*r |
| 128 | + x1, y1 = int(x0 + 1000*(-ty)), int(y0 + 1000 * tx) |
| 129 | + x2, y2 = int(x0 - 1000*(-ty)), int(y0 - 1000 * tx) |
| 130 | + print(theta*(180/3.14)) |
| 131 | + cap2 = cv2.line(cap2, (x1,y1), (x2,y2), (0,0,255), 1) |
| 132 | + |
| 133 | + |
| 134 | +#cv2.circle(cap1,(310,210),5,(0,0,255),-1) |
| 135 | +cv2.imshow('roimg',cap2) |
| 136 | + |
| 137 | + |
| 138 | +#object detection and localization |
| 139 | + |
| 140 | +#hog = cv2.HOGDescriptor((48,96),(16,16),(8,8),(8,8),9) |
| 141 | + |
| 142 | + |
| 143 | +#hog=cv2.HOGDescriptor() |
| 144 | +#hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) |
| 145 | + |
| 146 | +#detected, _ = hog.detectMultiScale(cap1) |
| 147 | + |
| 148 | +#for (x,y,w,h) in detected: |
| 149 | +# cv2.rectangle(cap1, (x,y),(x+w,y+h), (50,200,50),3) |
| 150 | +#cv2.imshow('HOG', cap2) |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg") |
| 155 | +classes = [] |
| 156 | +with open("coco.names", "r") as f: |
| 157 | + classes = [line.strip() for line in f.readlines()] |
| 158 | +layer_names = net.getLayerNames() |
| 159 | +output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] |
| 160 | +colors = np.random.uniform(0, 255, size=(len(classes), 3)) |
| 161 | + |
| 162 | +cap3 = cv2.resize(cap1, None, fx=0.4, fy=0.4) |
| 163 | +height, width, channels = cap3.shape |
| 164 | + |
| 165 | +blob = cv2.dnn.blobFromImage(cap3, 0.00392, (416, 416), (0, 0, 0), True, crop=False) |
| 166 | +net.setInput(blob) |
| 167 | +outs = net.forward(output_layers) |
| 168 | + |
| 169 | +class_ids = [] |
| 170 | +confidences = [] |
| 171 | +boxes = [] |
| 172 | +for out in outs: |
| 173 | + for detection in out: |
| 174 | + scores = detection[5:] |
| 175 | + class_id = np.argmax(scores) |
| 176 | + confidence = scores[class_id] |
| 177 | + if confidence > 0.5: |
| 178 | + # Object detected |
| 179 | + center_x = int(detection[0] * width) |
| 180 | + center_y = int(detection[1] * height) |
| 181 | + w = int(detection[2] * width) |
| 182 | + h = int(detection[3] * height) |
| 183 | + # Rectangle coordinates |
| 184 | + x = int(center_x - w / 2) |
| 185 | + y = int(center_y - h / 2) |
| 186 | + boxes.append([x, y, w, h]) |
| 187 | + confidences.append(float(confidence)) |
| 188 | + class_ids.append(class_id) |
| 189 | + |
| 190 | +indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) |
| 191 | + |
| 192 | +font = cv2.FONT_HERSHEY_PLAIN |
| 193 | +for i in range(len(boxes)): |
| 194 | + if i in indexes: |
| 195 | + x, y, w, h = boxes[i] |
| 196 | + label = str(classes[class_ids[i]]) |
| 197 | + #color = colors[i] |
| 198 | + color = colors[10] |
| 199 | + x = round(x*2.5) |
| 200 | + y = round(y*2.5) |
| 201 | + w = round(w*2.5) |
| 202 | + h = round(h*2.5) |
| 203 | + cv2.rectangle(cap2, (x, y), ((x + w), (y + h)), color, 2) |
| 204 | + |
| 205 | + |
| 206 | +cv2.imshow("Image", cap2) |
| 207 | +# Markerdetection |
| 208 | + |
| 209 | +#독자기능 |
| 210 | + |
| 211 | +pts1 = np.float32([[285, 240],[380, 260],[60, 480], [640, 450]]) |
| 212 | +pts2 = np.float32([[160,0],[480,0],[160,480],[480,480]]) |
| 213 | + |
| 214 | +M = cv2.getPerspectiveTransform(pts1,pts2) |
| 215 | + |
| 216 | +dst = cv2.warpPerspective(cap1,M,(640,480)) |
| 217 | + |
| 218 | +cv2.imshow("birdeye",dst) |
| 219 | + |
| 220 | +cv2.waitKey() |
| 221 | +cv2.destroyAllWindows() |
0 commit comments