0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

画像関係命令まとめ

Last updated at Posted at 2020-05-25
import cv2
#画像読み込み
im = cv2.imread('data/src/lena.jpg')
#白黒画像読み込み
im_gray = cv2.imread('data/src/lena.jpg', cv2.IMREAD_GRAYSCALE)
im_gray = cv2.imread('data/src/lena.jpg', 0)

#黒い画像作成
import numpy as np
img = np.zeros((height, width, 3), np.uint8)

#色空間変換
imrgb = cv2.cvtColor(im_bgr, cv2.COLOR_BGR2RGB)
#そのほかの変換
#https://docs.opencv.org/3.4.0/d7/d1b/group__imgproc__misc.html#ga4e0972be5de079fed4e3a10e24ef5ef0

#画像表示
cv2.imshow('image', im)

#画像ファイルに保存
cv2.imwrite('data/dst/lena_opencv_red.jpg', im)
#保存画像品質指定
cv2.imwrite('data/dst/lena_opencv_red_low.jpg', im, [cv2.IMWRITE_JPEG_QUALITY, 50])

#画像サイズ取得
h,w,c = img.shape

#画像に図形を書く
cv2.rectangle(img, (50, 150), (125, 200), (0, 0, 255), thickness=-1)
cv2.line(img, (50, 60), (125, 10), (0, 255, 255), thickness=4, lineType=cv2.LINE_AA)
cv2.arrowedLine(img, pt1, pt2, color, thickness=1, lineType=cv2.LINE_8, shift=0, tipLength=0.1)
cv2.circle(img, center, radius, color, thickness=1, lineType=cv2.LINE_8, shift=0)
cv2.circle(img, (240, 35), 20, (0, 0, 0), thickness=3, lineType=cv2.LINE_AA)
cv2.ellipse(img, box, color, thickness=1, lineType=cv2.LINE_8)
cv2.ellipse(img, ((240, 105), (20, 50), 30), (0, 0, 0), thickness=-1)
cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness=1, lineType=cv2.LINE_8, shift=0)
cv2.ellipse(img, (240, 175), (10, 25), 30, 0, 270, (0, 0, 0), thickness=-1)
cv2.drawMarker(img, position, color, markerType=cv2.MARKER_CROSS, markerSize=20, thickness=1, line_type=cv2.LINE_8)
cv2.drawMarker(img, (337, 20), (0, 255, 0), markerType=cv2.MARKER_TILTED_CROSS, markerSize=15)
cv2.polylines(img, pts, isClosed, color, thickness=1, lineType=cv2.LINE_8, shift=0)
pts = np.array(((300, 80), (300, 130), (335, 130)))
cv2.polylines(img, [pts], True, (255, 255, 255), thickness=2)
cv2.fillPoly(img, pts, color, lineType=cv2.LINE_8, shift=0)
pts = np.array(((335, 80), (375, 80), (375, 130)))
cv2.fillPoly(img, [pts], (0, 0, 0))
cv2.fillConvexPoly(img, pts, (0, 0, 0))
cv.putText(img, text, org, fontFace, fontScale, color, thickness=1, lineType=cv2.LINE_8)
cv2.putText(img, 'test', (300, 170), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), thickness=2)
#日本語入力
import numpy as np
from PIL import ImageFont, ImageDraw, Image
# 背景が黒になるように、すべての要素を0とし、200*500でRGB値3つを格納でき、
# 要素のデータ型は8bit(1byte)の符号なし整数とする配列(背景画像)を作る。
img = np.zeros((200,500,3),np.uint8)
# 表示する色
b,g,r,a = 0,255,0,0 #B(青)・G(緑)・R(赤)・A(透明度)
## MSゴシック標準
fontpath ='C:\Windows\Fonts\msgothic.ttc' # Windows10 だと C:\Windows\Fonts\ 以下にフォントがあります。
font = ImageFont.truetype(fontpath, 32) # フォントサイズが32
img_pil = Image.fromarray(img) # 配列の各値を8bit(1byte)整数型(0~255)をPIL Imageに変換。
draw = ImageDraw.Draw(img_pil) # drawインスタンスを生成
position = (50, 100) # テキスト表示位置
draw.text(position, message, font = font , fill = (b, g, r, a) ) # drawにテキストを記載 fill:色 BGRA (RGB)
img = np.array(img_pil) # PIL を配列に変換

# ラベリング処理
label = cv2.connectedComponentsWithStats(mono_src)
# オブジェクト情報を項目別に抽出
n = label[0] - 1
data = np.delete(label[2], 0, 0)
center = np.delete(label[3], 0, 0)
# オブジェクト情報を利用してラベリング結果を画面に表示
for i in range(n):
    # 各オブジェクトの外接矩形の座標
    x0 = data[i][0]
    y0 = data[i][1]
    x1 = data[i][0] + data[i][2]
    y1 = data[i][1] + data[i][3]


0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?