2020年3月25日水曜日

JupyterLab で画像の穴のずれ具合を確認

from PIL import Image, ImageDraw
import numpy as np

水平になるように画像を回転

im = Image.open('IMG_20200302_210756-2.png')

im = im.rotate(-1.34)

draw = ImageDraw.Draw(im)
size = im.size
y = 2245
draw.line((0, y, size[0], y), fill=(255, 0, 0), width=10)
im

必要な部分の画像を切り取り

im_crop = im.crop((1050, 1350, 1600, 1900))
im_crop

丸の位置とサイズ確認

im_crop = im.crop((1050, 1350, 1600, 1900))
draw_crop = ImageDraw.Draw(im_crop)

width = 5
diff = 10

# 外丸
outer_center = np.array([292, 275])
outer_rad = 213

outer_color = (255, 0, 0)
draw_crop.ellipse([tuple(outer_center - outer_rad), tuple(outer_center + outer_rad)], outline=outer_color, width=width)
draw_crop.line(
    [
        tuple(outer_center - diff),
        tuple(outer_center + diff)
    ],
    fill=outer_color,
    width=width
)
draw_crop.line(
    [
        (
            outer_center[0] + diff,
            outer_center[1] - diff
        ),
        (
            outer_center[0] - diff,
            outer_center[1] + diff
        )
    ],
    fill=outer_color,
    width=width
)
im_crop

# 内丸
inner_center = np.array([315, 245])
inner_rad = 83

inner_color = (0, 0, 255)
draw_crop.ellipse([tuple(inner_center - inner_rad), tuple(inner_center + inner_rad)], outline=inner_color, width=width)
draw_crop.line(
    [
        tuple(inner_center - diff),
        tuple(inner_center + diff)
    ],
    fill=inner_color,
    width=width
)
draw_crop.line(
    [
        (
            inner_center[0] + diff,
            inner_center[1] - diff
        ),
        (
            inner_center[0] - diff,
            inner_center[1] + diff
        )
    ],
    fill=inner_color,
    width=width
)
im_crop

中心のずれ (ピクセル)

center_diff_pix = outer_center - inner_center
center_diff_pix
array([-23,  30])

中心のずれ (mm) => 内円の半径 1.5mm で計算

center_diff_mm = center_diff_pix * 1.5 / inner_rad
center_diff_mm
array([-0.41566265,  0.54216867])

0 件のコメント:

コメントを投稿