画像拡大プログラムを言語化してほしい
プログラムを言語化してほしい
python 初心者です
下にあるコードは,指定した位置を中心に画像を拡大することができるコードらしいのですが,
なぜ拡大してるのに変換された画像のサイズが変わっていないのかが理解できません.
このプログラムがどのようにして拡大されているか言語化できる方いましたら,していただけますでしょうか?
また,拡大してるのにサイズが変わらないとなると穴?(情報が不足している画素?)ができてしまうことを想像しているのですが,この点に関しましても分かる方いましたら説明していただけると幸いです。説明へたで,すみません.
拡大画像データ(sample.jpg)
横方向の拡大率(cx)
横方向の拡大率(cy)
拡大する際の横方向基準位置(x1)
拡大する際の縦方向基準位置(y1)
指定した位置を中心に画像を拡大するプログラム
import numpy as np
import cv2
#ベース画像の読み込み&情報入力
img=cv2.imread('sample.jpg',cv2.IMREAD_GRAYSCALE)
h,w=img.shape[:2]
cx=3
cy=3
x1=230 #pixel
y1=240 #pixel
#画像の拡大作業開始
x2=x1*cx #pixel
y2=y1*cy #pixel
size_after=(int(w*cx), int(h*cy))
resized_img=cv2.resize(img, dsize=size_after)
deltax=(w/2-x1)-(resized_img.shape[1]/2-x2)
deltay=(h/2-y1)-(resized_img.shape[0]/2-y2)
framey=int(h*cy*2)
framex=int(w*cx*2)
finalimg=np.zeros((framey,framex),np.uint8)
finalimg[int(-deltay+framey/2-resized_img.shape[0]/2):int(-deltay+framey/2+resized_img.shape[0]/2),
int(-deltax+framex/2-resized_img.shape[1]/2):int(-deltax+framex/2+resized_img.shape[1]/2)]=resized_img
finalimg=finalimg[int(finalimg.shape[0]/2-h/2):int(finalimg.shape[0]/2+h/2),int(finalimg.shape[1]/2-w/2):int(finalimg.shape[1]/2+w/2)]
#結果の出力
cv2.imwrite('final_img.jpg',finalimg
0 likes