When initializing the screen with OpenCV, I wanted to color the background, but I struggled more than I expected, so I made a note.
Looking at Basic processing of cv :: Mat, OpenCV1 uses a structure called IplImage as image data, while OpenCV2 uses cv :: It has changed to use a class called Mat.
In OpenCV1, it is initialized by cvCreateImage, but the point is what to do with OpenCV2.
In the case of Python, the array structure of numpy can be used as it is, so I thought it would be easy once I got used to it, but I'm not used to it, so I don't know what to do with the C sample of OpenCV1.
When I went down the internet, there were various methods, so I tried arranging them. By the way, when displaying images with OpenCV, the order of colors is BGR.
example_colorfill.py
# coding: UTF-8
import numpy as np
import cv2
#10 x 10 3 layers(BGR)Define
size = 10, 10, 3
# cv2.Fill in red with 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.Fill in blue with 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 in white with fill
white_img = np.zeros(size, dtype=np.uint8)
white_img.fill(255)
# np.Fill in green with tile
green_img = np.tile(np.uint8([0,255,0]), (10,10,1))
#Fill in gray with 10 x 10 single gradation
gray_img = np.tile(np.uint8([127]), (10,10,1))
#Fill the list from beginning to end in purple
#reference: 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)
#Convert RGB sequence to BGR and fill in yellow
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)
When executed, the images filled with each color will be displayed.
I think it's either a method of filling with 0 first and filling with color by list operation, or a method of filling with color from the beginning with np.tile.
Either way, it seems that it is not compatible with C ++.
Recommended Posts