-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkivy_opencv.py
81 lines (51 loc) · 3.36 KB
/
kivy_opencv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*
import numpy as np
import cv2
from PIL import Image
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics.texture import Texture
from kivy.graphics import Rectangle
class MyApp(App):
title = "opencv on kivy"
def build(self):
img = cv2.imread('kai_058Kazukiya17103_TP_V.jpg',1)
# 画像をグレイスケールに変換
gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
#cv2.imshow('opencv_normal', img)
cv2.imshow('opencv_gray', gray_img)
if img is None:
print('load image')
sys.exit(1)
widget = Widget()
''' pattern1 (openCVの機能で表示) '''
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # openCVの色の並びはBGRなのでRGBに直す
img2 = cv2.flip(img2, 0) # Kivyの座標の原点は左下なので上下反転する
# OpenCVの座標shapeは「高さ」、「幅」、「チャンネル」の順番 Kivyのsizeは(幅、高さ)なので逆にする必要がある
#texture = Texture.create(size=(img2.shape[1], img2.shape[0]))
#texture.blit_buffer(img2.tostring())
#with widget.canvas: # 描画
# Rectangle(texture=texture ,pos=(0, 0), size=(img2.shape[1], img2.shape[0]))
''' pattern2 (kivyの機能のみで表示) '''
#texture = Texture.create(size=(img.shape[1], img.shape[0]), colorfmt='bgr', bufferfmt='ubyte') # BGRモードで用意,ubyteはデフォルト引数なので指定なくてもよい
#texture.blit_buffer(img.tostring(),colorfmt='bgr', bufferfmt='ubyte') # ★ここもBGRで指定しないとRGBになって色の表示がおかしくなる
#texture.flip_vertical() # 画像を上下反転させる
#with widget.canvas:
# Rectangle(texture=texture ,pos=(0, 0), size=(img.shape[1], img.shape[0]))
''' pattern3(モノクロ) '''
texture = Texture.create(size=(gray_img.shape[1], gray_img.shape[0]), colorfmt='bgr', bufferfmt='ubyte') # BGRモードで用意,ubyteはデフォルト引数なので指定なくてもよい
gray_to_rgb_img = cv2.cvtColor(gray_img, cv2.COLOR_GRAY2RGB) # bilt_bufferでは1チェンネルのグレイ画像は表示できないので3チャンネルの画像に変換する
texture.blit_buffer(gray_to_rgb_img.tostring(),colorfmt='bgr', bufferfmt='ubyte') # ★ここもBGRで指定しないとRGBになって色の表示がおかしくなる
texture.flip_vertical() #
with widget.canvas:
Rectangle(texture=texture ,pos=(0, 0), size=(gray_img.shape[1], gray_img.shape[0]))
''' Pillowで画像を表示する '''
#pillow_img = Image.open('kai_058Kazukiya17103_TP_V.jpg', 'r')
#texture = Texture.create(size=pillow_img.size)
#texture.blit_buffer(pillow_img.tobytes())
#texture.flip_vertical() #
#with widget.canvas:
# Rectangle(texture=texture ,pos=(0, 0), size=pillow_img.size)
return widget
if __name__ == '__main__':
MyApp().run()