Skip to content

Instantly share code, notes, and snippets.

@daxanya2
Created June 22, 2014 11:29
Show Gist options
  • Save daxanya2/33d9f1ff5e5e8c0f5164 to your computer and use it in GitHub Desktop.
Save daxanya2/33d9f1ff5e5e8c0f5164 to your computer and use it in GitHub Desktop.
OpenCV2 + Pythonで背景を単色で埋める ref: http://qiita.com/daxanya1/items/85f5e17ecc1203f756ad
# coding: UTF-8
import numpy as np
import cv2
# 10 x 10 の3レイヤー(BGR)を定義
size = 10, 10, 3
# cv2.fillPolyで赤に埋める
red_img = np.zeros(size, dtype=np.uint8)
contours = np.array( [ [0,0], [0,10], [10, 10], [10,0] ] )
cv2.fillPoly(red_img, pts =[contours], color=(0,0,255))
# cv2.rectangleで青に埋める
blue_img = np.zeros(size, dtype=np.uint8)
cv2.rectangle(blue_img,(0,0),(10,10),(255,0,0),cv2.cv.CV_FILLED)
# np.fillで白に埋める
white_img = np.zeros(size, dtype=np.uint8)
white_img.fill(255)
# np.tileで緑に埋める
green_img = np.tile(np.uint8([0,255,0]), (10,10,1))
# 10 x 10 の単階調で灰色に埋める
gray_img = np.tile(np.uint8([127]), (10,10,1))
# リストの最初から最後までを紫に埋める
# 参考: http://stackoverflow.com/questions/4337902/how-to-fill-opencv-image-with-one-solid-color
purple_img = np.zeros(size, dtype=np.uint8)
purple_img[:] = (255, 0, 255)
# RGBの並びをBGRに変換して黄色に埋める
yellow_img = np.zeros(size, dtype=np.uint8)
rgb_color = (255,255,0);
yellow_img[:] = tuple(reversed(rgb_color))
cv2.namedWindow("yellow image", cv2.WINDOW_AUTOSIZE)
cv2.imshow("yellow image",yellow_img)
cv2.namedWindow("purple image", cv2.WINDOW_AUTOSIZE)
cv2.imshow("purple image",purple_img)
cv2.namedWindow("gray image", cv2.WINDOW_AUTOSIZE)
cv2.imshow("gray image",gray_img)
cv2.namedWindow("green image", cv2.WINDOW_AUTOSIZE)
cv2.imshow("green image",green_img)
cv2.namedWindow("white image", cv2.WINDOW_AUTOSIZE)
cv2.imshow("white image",white_img)
cv2.namedWindow("red image", cv2.WINDOW_AUTOSIZE)
cv2.imshow("red image",red_img)
cv2.namedWindow("blue image", cv2.WINDOW_AUTOSIZE)
cv2.imshow("blue image",blue_img)
cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment