FC2ブログ

Python::use-defined_module::mark drawing by Python::user-defined_class::img with opencv

# -*- coding: utf-8 -*-
from point2d import point2d
from mark import *
from img import img
if __name__ == '__main__':
    width, height, dp = 300, 300, 10
    center = point2d(width / 2, height / 2)
    mark__R.shift(center)

    start = point2d(0, -150)
    mark__R.shift(start)

    pmax = 300
    for p in range(0, pmax):
        print ("({0} / {1})".format(p + 1, pmax))
        # scan
        scan = point2d(0, 1)
        mark__R.shift(scan)
        #
        im = img(width, height, dp)
#        im.print()
        im.draw(mark__R)
        im.flip_y()
        im.save(str(p + 1) + ".png")
    
    pass

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



Python::user-defined_class::mark

# -*- coding: utf-8 -*-
import copy
class mark:
    def __init__ (self, name, center, lines):
        self.name = name
        self.center = copy.deepcopy(center)
        self.lines = lines
    
    def shift (self, delta):
        self.center = self.center - delta
        for i, l in enumerate(self.lines):
            self.lines[i] = self.lines[i].shift(-1 * delta)
        
    def print (self):
        print ("name = \"{0}\", center = ({1}, {2})".format(
                self.name, self.center.x, self.center.y))
        for l in self.lines:
            l.print()


from point2d import point2d
from line import line
lines__R = [
    line(*(point2d(-100, +100.0), point2d(+100, +100.0)), 1),
    line(*(point2d(-100, -100.0), point2d(+100, -100.0)), 1),
    line(*(point2d(-100, -100.0), point2d(-100, +100.0)), 1),
    line(*(point2d(+100, -100.0), point2d(+100, +100.0)), 1),
]

mark__R = mark("rectangle",   point2d(0, 0), lines__R)

関連記事

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]))

関連記事

Python::user-defined_class::line

# -*- coding: utf-8 -*-
from point2d import point2d
class line:
    def __init__ (self, start, end, width):
        self.start = point2d(start.x, start.y)
        self.end   = point2d(end.x, end.y)
        self.width = width

    def shift (self, delta):
        start = point2d((self.start.x - delta.x), (self.start.y - delta.y))
        end   = point2d((self.end.x   - delta.x), (self.end.y   - delta.y))
        width = self.width
        return line(start, end, width)

    def scaling (self, dp):
        start = point2d((self.start.x / dp), (self.start.y / dp))
        end   = point2d((self.end.x   / dp), (self.end.y   / dp))
        width = (self.width / dp)
        return line(start, end, width)
    
    def print (self):
        print ("line[width = {0}] = ({1:4.3e}, {2:4.3e}) -> ({3:4.3e}, {4:4.3e})".format(
                self.width, self.start.x, self.start.y, self.end.x, self.end.y))

関連記事

Python::user-defined_class::point2d

# -*- coding: utf-8 -*-
import copy
class point2d:
    def __init__ (self, x, y):
        self.x = x
        self.y = y
        
    def __add__ (self, other):
        x = self.x + other.x
        y = self.y + other.y
        return point2d(x, y)

    def __sub__ (self, other):
        x = self.x - other.x
        y = self.y - other.y
        return point2d(x, y)
    
    def __mul__ (self, other):
        x = self.x * other
        y = self.y * other
        return point2d(x, y)
    
    def __rmul__ (self, other):
        x = self.x * other
        y = self.y * other
        return point2d(x, y)
    
    def to_int (self):
        x = int(copy.copy(self.x))
        y = int(copy.copy(self.y))
        return point2d(x, y)

    def to_tuple (self):
        x = copy.deepcopy(self.x)
        y = copy.deepcopy(self.y)
        return (x, y)

    def print (self):
        print ("({0}, {1})".format(self.x, self.y))

関連記事