FC2ブログ

Python::user-defined_class::img with opencv

# -*- coding: utf-8 -*-
from point2d import point2d
class img:
    def __init__ (self, width, height, dp):
        self.width = width
        self.height = height
        self.dp = dp
        import math
        self.pwidth = int(math.ceil(self.width / self.dp))
        self.pheight = int(math.ceil(self.height / self.dp))
        # convert XY-coordinate to IJ-coordinate
        rows    = self.pheight
        columns = self.pwidth
        import numpy
        self.img = numpy.full((rows, columns), 0, dtype=numpy.uint8)

    def save (self, target_file_fullpath):
        import cv2
        cv2.imwrite(target_file_fullpath, self.img)

    def draw (self, mark):
        for l in mark.lines:
            self.draw_line(l)

    def draw_line (self, line):
        import cv2
        cv2.line(self.img,
                 line.scaling(self.dp).start.to_int().to_tuple(),
                 line.scaling(self.dp).end.to_int().to_tuple(),
                 255,
                 thickness=int(line.scaling(self.dp).width),
                 lineType=cv2.LINE_AA)

    def flip_y (self):
        import cv2
        self.img = cv2.flip(self.img, 0)

    def print (self):
        print ("width, height = {0}, {1}".format(self.width, self.height))
        print ("pwidth, pheight = {0}, {1}".format(self.pwidth, self.pheight))
        print ("img.shape(rows, colums) = ({0}, {1})".format(self.img.shape[1], self.img.shape[0]))

関連記事
スポンサーサイト



コメント

非公開コメント