カメラ画像を枠なし表示(Python, OpenCV を使用)
普通に表示すると、ウインドウに枠がつく. 枠を表示したくないときは、このページのプログラムを使用する.
【サイト内の OpenCV 関連ページ】
- OpenCV について [PDF] , [パワーポイント]
- OpenCV のインストール,画像表示を行う C++ プログラムの実行手順: 別ページ »で説明
- OpenCVとPythonを活用した画像・ビデオ処理プログラム: 別ページ »にまとめ
- OpenCV 4 の C/C++ プログラム: 別ページ »にまとめている.
【OpenCV の公式情報】
- OpenCV の公式ページ: https://opencv.org
- GitHub の OpenCV のページ: https://github.com/opencv/opencv/releases
前準備
Python のインストールと必要なPythonライブラリのインストール(Windows上)
- Python のインストール
注:既にPython(バージョン3.12を推奨)がインストール済みの場合は,この手順は不要である.
winget(Windowsパッケージマネージャー)を使用してインストールを行う
- Windowsで,コマンドプロンプトを管理者権限で起動する(手順:Windowsキーまたはスタートメニュー,「cmd」と入力,右クリックメニューなどで「管理者として実行」を選択)
- winget(Windowsパッケージマネージャー)が利用可能か確認する:
winget --version
- Pythonのインストール(下のコマンドにより Python 3.12 がインストールされる).
reg add "HKLM\SYSTEM\CurrentControlSet\Control\FileSystem" /v LongPathsEnabled /t REG_DWORD /d 1 /f REM Python をシステム領域にインストール winget install --scope machine --id Python.Python.3.12 --id Python.Launcher -e --silent REM Python のパス set "INSTALL_PATH=C:\Program Files\Python312" echo %PATH% | find /i "%INSTALL_PATH%" >nul if errorlevel 1 setx PATH "%PATH%;%INSTALL_PATH%" /M >nul echo %PATH% | find /i "%INSTALL_PATH%\Scripts" >nul if errorlevel 1 setx PATH "%PATH%;%INSTALL_PATH%\Scripts" /M >nul
- 必要なPythonライブラリのインストール
【関連する外部ページ】
【サイト内の関連ページ】
カメラ画像を枠なし表示(Python, OpenCV を使用)
USB接続できるビデオカメラを準備し,パソコンに接続しておく.
OpenCV による動画表示を行う.
import cv2
import numpy as np
import win32gui
import win32con
x = 0
y = 0
width = 640
height = 480
cv2.namedWindow("camera")
v = cv2.VideoCapture(0)
a = win32gui.FindWindow(None,"camera")
win32gui.SetWindowLong(a, win32con.GWL_STYLE, win32con.WS_POPUP)
print(a)
while(v.isOpened()):
r, f = v.read()
if ( r == False ):
break
cv2.imshow("camera", f)
win32gui.SetWindowPos(a, win32con.HWND_TOPMOST, x, y, width, height, win32con.SWP_SHOWWINDOW)
# Press Q to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
v.release()
cv2.destroyAllWindows()
* 止めたいとき,右上の「x」をクリックしない.画面の中をクリックしてから,「q」のキーを押して閉じる

拡大縮小、回転を行いたい場合
OpenCV による動画表示を行う.
import cv2
import numpy as np
import win32gui
import win32con
x = 0
y = 0
scale = 0.5
cv2.namedWindow("camera")
v = cv2.VideoCapture(0)
a = win32gui.FindWindow(None,"camera")
win32gui.SetWindowLong(a, win32con.GWL_STYLE, win32con.WS_POPUP)
print(a)
while(v.isOpened()):
r, f = v.read()
if ( r == False ):
break
f = cv2.rotate(f, cv2.ROTATE_90_COUNTERCLOCKWISE)
height = f.shape[0]
width = f.shape[1]
f = cv2.resize(f , (int(width*scale), int(height*scale)))
cv2.imshow("camera", f)
win32gui.SetWindowPos(a, win32con.HWND_TOPMOST, x, y, width, height, win32con.SWP_SHOWWINDOW)
# Press Q to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
v.release()
cv2.destroyAllWindows()
* 止めたいとき,右上の「x」をクリックしない.画面の中をクリックしてから,「q」のキーを押して閉じる