def scale1(img, width, height): log.add("Editing image") pts1 = np.float32([[158, 52], [553, 51], [111, 425], [594, 434]]) pts2 = np.float32([[0, 0], [width - 1, 0], [0, height - 1], [width - 1, height - 1]]) retval = cv2.getPerspectiveTransform(pts1, pts2) warp = cv2.warpPerspective(img, retval, (width, height)) log.add("Image transformed") # plt.subplot(131),plt.imshow(warp),plt.title('Output') # plt.show() return warp
def take(p): global capture if capture == "false": capture = cv2.VideoCapture(-1) log.add("Taking picture...") #capture = cv2.VideoCapture(-1) for i in range(50): ret, frame = capture.read() cv2.imwrite(p, frame) #capture.release() log.add("Picture saved!")
#!/usr/local/bin/python import os,sys,cv2 __homedir__ = os.path.dirname(os.path.realpath(__file__)) os.chdir(__homedir__) from lib import log,webcam,analyze,util log.add("Script succesfully started") util.state(__homedir__,"Script wordt gestart") util.state(__homedir__,"Maken van foto") #webcam.take(__homedir__ + "\\files\\bord.jpg") util.newboard(__homedir__) util.state(__homedir__,"Afbeelding bewerken en analyseren") analyze.start(__homedir__ + "\\files\\bord.jpg",8,0) #log.add(analyze.cell(__homedir__ + "\\files\\vak\\x1-y1.jpg")) util.state(__homedir__,"Controle op mogelijke zetten") bord = [[0 for x in xrange(8)] for x in xrange(8)] for ix in range(8): for iy in range(8): log.add(str(ix+1) + " , " + str(iy+1) + " = " + str(analyze.cell(__homedir__,ix+1,iy+1))) bord[ix][iy] = analyze.cell(__homedir__,ix+1,iy+1) log.add(bord) util.board2file(bord,__homedir__ + "\\files\\bord.txt")
def start(imgpath, size, border): log.add("Analyzing image...") log.add("Loading board image") img = cv2.imread(imgpath) log.add("Image loaded") log.add("Scaling image...") img = scale(img, 400, 400) log.add("Image scaled. Saving...") cv2.imwrite("files\\bord_scaled.jpg", img) log.add("Image saved") log.add("Chopping image...") chop(img, size, size, border) log.add("Image chopped")
def scale(img, width, height): log.add("Editing image") corners1 = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) corners2 = cv2.GaussianBlur(corners1, (5, 5), 0) # plt.subplot(133),plt.imshow(corners2),plt.title('Blur') corners = cv2.adaptiveThreshold(corners2, 255, 1, 1, 11, 2) log.add("Looking for contours") contours, hierarchy = cv2.findContours(corners, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) biggest = None max_area = 0 log.add("Comparing contours") for i in contours: area = cv2.contourArea(i) if area > 100: peri = cv2.arcLength(i, True) approx = cv2.approxPolyDP(i, 0.02 * peri, True) if area > max_area and len(approx) == 4: biggest = approx max_area = area log.add("Found contour") log.add("Transforming image") biggest = rectify(biggest) pts1 = np.float32(biggest) pts2 = np.float32([[0, 0], [width - 1, 0], [0, height - 1], [width - 1, height - 1]]) retval = cv2.getPerspectiveTransform(pts1, pts2) warp = cv2.warpPerspective(img, retval, (width, height)) log.add("Image transformed") # plt.subplot(131),plt.imshow(warp),plt.title('Output') # plt.subplot(132),plt.imshow(corners),plt.title('Input corners') # plt.show() return warp