画素へのアクセス

V2.4.11, V3.0.0
In [2]:
# -*- coding: utf-8 -*-
import cv2
import numpy as np

#Ipythonで表示用の設定
import matplotlib.pyplot as plt
%matplotlib inline

#画像読込
in_img = cv2.imread("lena.jpg")

#画像座標(100,90)へアクセス
px = in_img[90,100]
print "(100, 90)=" , px

#青の画素にアクセス(OpenCVは、BGRで格納に注意)
blue = in_img[90,100,0]
print "(100, 90, Blue)=" ,blue

#画素値の変更
in_img[90,100]=[255,255,255]
print "(100, 90)=" , in_img[90,100]

#赤の画素値にアクセス
print "(10, 10, Red)=", in_img.item(10,10,2)

#赤の画素値の変更
in_img.itemset((10,10,2),100)
print  "(10, 10, Red)=", in_img.item(10,10,2)
(100, 90)= [ 81  67 173]
(100, 90, Blue)= 81
(100, 90)= [255 255 255]
(10, 10, Red)= 228
(10, 10, Red)= 100

inserted by FC2 system