Python
 Computer >> コンピューター >  >> プログラミング >> Python

Pythonでのフォトモザイクの実装


フォトモザイクは、画像を正方形のグリッドに分割できる手法です。各正方形は、他の画像や色に置き換えられます。したがって、実際の画像を特定の距離から見たい場合は実際の画像を見ることができますが、近づくと、さまざまな色のブロックのグリッドを見ることができます。

この場合、photomosaicと呼ばれるPythonモジュールを使用しています。このモジュールを使用すると、いくつかのフォトモザイクを簡単に作成できます。インストールするには、このリンクをたどってください。 scikit learnもダウンロードします モジュール。

sudo pip3 install photomosaic

このモジュールにはいくつかの機能があります。これらは以下にリストされています-

  • ここでは、さまざまなサイズのタイルを使用できます。
  • 画像の詳細部分に小さなタイルを設定できます。
  • フリッカーAPIを使用して、タイルとして使用する画像の大規模なコレクションを取得します

この記事では、このモジュールをフォトモザイク用に非常に簡単な方法で実装する方法を説明します。

skimageライブラリの画像を使用しています。

メイン画像

<中央> Pythonでのフォトモザイクの実装

フォトモザイクを作成する手順

  • 実際の画像(ここではskimageライブラリからの画像)を取得します
  • グリッドタイルのサイズを定義する
  • プールとしてカラフルなRGB画像ブロックを作成する場所を提供します
  • フォルダをフォトモザイクのプールとして設定します
  • プールとグリッドサイズを使用してフォトモザイクに変換します。
  • 画像を保存
  • 終了

サンプルコード

from skimage.io import *
import sys
import photomosaic asphmos
from skimage import data
image = data.coffee() #Get coffee image from skimage
#Get the mosaic size from the command line argument.
mos_size = (int(sys.argv[1]), int(sys.argv[2]))
#create all image squares and generate pool
phmos.rainbow_of_squares('square/')
square_pool = phmos.make_pool('square/*.png')
#Create the mosaic image and save
mosaic = phmos.basic_mosaic(image, square_pool, mos_size)
imsave('mosaic_op.png', mosaic)

出力(初回実行、グリッドサイズは100 x 100)

$ python3 225.Photomosaic.py 100 100
5832it [00:02, 2506.05it/s]
analyzing pool: 100%|| 5832/5832 [00:08<00:00, 717.90it/s]
/usr/local/lib/python3.6/dist-packages/skimage/transform/_warps.py:105: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
warn("The default mode, 'constant', will be changed to 'reflect' in "
/usr/local/lib/python3.6/dist-packages/skimage/transform/_warps.py:110: UserWarning: Anti-aliasing will be enabled by default in skimage 0.15 to avoid aliasing artifacts when down-sampling images.
warn("Anti-aliasing will be enabled by default in skimage 0.15 to "
partitioning: depth 0: 100%|| 10000/10000 [00:00<00:00, 852292.94it/s]
analyzing tiles: 100%|| 10000/10000 [00:00<00:00, 93084.50it/s]
matching: 100%|| 10000/10000 [00:00<00:00, 30864.50it/s]
drawing mosaic: 100%|| 10000/10000 [00:00<00:00, 13227.12it/s]
/usr/local/lib/python3.6/dist-packages/skimage/util/dtype.py:141: UserWarning: Possible precision loss when converting from float64 to uint8
.format(dtypeobj_in, dtypeobj_out))
<中央> Pythonでのフォトモザイクの実装

出力(2回目の実行、グリッドサイズは500 x 500)

$ python3 225.Photomosaic.py 500 500
5832it [00:02, 2634.16it/s]
analyzing pool: 100%|| 5832/5832 [00:08<00:00, 709.54it/s]
/usr/local/lib/python3.6/dist-packages/skimage/transform/_warps.py:105: UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
warn("The default mode, 'constant', will be changed to 'reflect' in "
/usr/local/lib/python3.6/dist-packages/skimage/transform/_warps.py:110: UserWarning: Anti-aliasing will be enabled by default in skimage 0.15 to avoid aliasing artifacts when down-sampling images.
warn("Anti-aliasing will be enabled by default in skimage 0.15 to "
partitioning: depth 0: 100%|| 250000/250000 [00:00<00:00, 456159.45it/s]
analyzing tiles: 100%|| 250000/250000 [00:02<00:00, 113937.01it/s]
matching: 100%|| 250000/250000 [00:07<00:00, 32591.43it/s]
drawing mosaic: 100%|| 250000/250000 [00:02<00:00, 104349.90it/s]
/usr/local/lib/python3.6/dist-packages/skimage/util/dtype.py:141: UserWarning: Possible precision loss when converting from float64 to uint8
.format(dtypeobj_in, dtypeobj_out))
<中央> Pythonでのフォトモザイクの実装
  1. Pythonを使用して画像を読む?

    OpenCVを使用した画像処理 OpenCV(オープンソースコンピュータービジョン)は、基本的に機械学習とコンピュータービジョンのために開発されたオープンソースプログラミングライブラリです。コンピュータビジョンアプリケーションで動作し、商用製品での機械学習の使用を高速化するための共通のインフラストラクチャを提供します。 コンピュータービジョンと機械学習の両方に最適化された2.5千を超えるアルゴリズムは、古典的で最先端のアルゴリズムです。非常に多くのアルゴリズムを使用して、顔の検出と認識、オブジェクトの識別、ビデオ内の人間の行動の分類、カメラの動きの追跡、画像の結合によるシーン全体の高解像度

  2. Pythonで太陽画像をプロットする

    Pythonでは、ソーラーイメージを作成するためのSunPyパッケージを提供しています。このパッケージには、さまざまな太陽観測所や太陽実験室からの陽子/電子フラックスの太陽データであるさまざまなファイルが含まれています。 pip install sunpyを使用する コマンド、sunpyパッケージをインストールできます。 ここでは、サンプルのAIA画像をプロットします。 AIAはAtmosphericImagingAssemblyです。これはSDOのもう1つの計器盤です。 ここでは、sunpy.Map()関数を使用して、サポートされているデータ製品の1つからマップを作成します。 サンプ