Beispiel #1
0
def undistortion(in_f, out_f, mapx, mapy, roi, crop=True):
    img = cv2.imread(in_f, 1)
    undist = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)
    if crop:
        x, y, w, h = roi
        undist = undist[y:y + h, x:x + w]
    cv2.imwrite(out_f, undist)
    file_message(out_f)
Beispiel #2
0
def detect_markers(in_f, out_f, aruco_dict, aruco_params, show=False):
    img = cv2.imread(in_f, cv2.IMREAD_COLOR)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    corners, ids, rejected = cv2.aruco.detectMarkers(gray,
                                                     aruco_dict,
                                                     parameters=aruco_params)
    with open(out_f, "wb") as out:
        pickle.dump([corners, ids, rejected], out)
    file_message(out_f)

    if show:
        img_with_aruco = cv2.aruco.drawDetectedMarkers(img, corners, ids,
                                                       (0, 255, 0))
        cv2.imshow("aruco", img_with_aruco)
        cv2.waitKey(0)
Beispiel #3
0
def threshold_segmentation(in_f, out_f, limits, colour_space="HSV"):
    img = cv2.imread(in_f, 1)
    if colour_space == "BGR":
        cvt = img.copy()
    elif colour_space == "LAB":
        cvt = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
    elif colour_space == "HSV":
        cvt = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    else:
        cvt = img.copy()

    limits = np.array(list(zip(*limits)))
    mask = cv2.inRange(cvt, limits[0], limits[1])
    mask = (mask / 255).astype(int)

    encode_segmentation(mask, out_f)
    file_message(out_f)
Beispiel #4
0
def prepare_annotator(img_dir=IMG_DIR,
                      ann_dir=ANN_DIR,
                      app_dir=APP_DIR,
                      classes=("background", "plant", "panicle")):
    img_out_dir = app_dir + "/data/images"
    for f in files(img_out_dir, ".jpg"):
        os.remove(f)
    for f in files(img_dir, ".jpg"):
        shutil.copy(f, img_out_dir)
        file_message(img_out_dir + "/" + os.path.basename(f))

    ann_out_dir = app_dir + "/data/annotations"
    for f in files(ann_out_dir, ".png"):
        os.remove(f)
    for f in files(ann_dir, ".png"):
        shutil.copy(f, ann_out_dir)
        file_message(ann_out_dir + "/" + os.path.basename(f))

    jsonfile = files(app_dir + "/data", ".json")[0]
    with open(jsonfile, "r") as read_file:
        data = json.load(read_file)
    data["labels"] = classes
    data["imageURLs"] = [
        "data/images/" + os.path.basename(f)
        for f in files(img_out_dir, ".jpg")
    ]
    data["annotationURLs"] = [
        "data/annotations/" + os.path.basename(f)
        for f in files(ann_out_dir, ".png")
    ]
    with open(jsonfile, "w") as write_file:
        json.dump(data, write_file)
    file_message(jsonfile)
Beispiel #5
0
def raw2jpg(in_f, out_f):
    with rawpy.imread(in_f) as raw:
        rgb = raw.postprocess()
    imageio.imsave(out_f, rgb)
    file_message(out_f)
Beispiel #6
0
def resize(in_f, out_f, ratio=.5):
    img = cv2.imread(in_f, 1)
    dim = (int(ratio * img.shape[1]), int(ratio * img.shape[0]))
    resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
    cv2.imwrite(out_f, resized)
    file_message(out_f)