Fill the background with a single color with OpenCV2 + Python

When initializing the screen with OpenCV, I wanted to color the background, but I struggled more than I expected, so I made a note.

What to do in this article

Points to struggle with

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.

I researched and tried various things

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.

Recommended

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

Fill the background with a single color with OpenCV2 + Python
Color extraction with Python + OpenCV solved the mystery of the green background
Search the maze with the python A * algorithm
I tried "smoothing" the image with Python + OpenCV
[Python] Get the files in a folder with Python
I tried "differentiating" the image with Python + OpenCV
Create a color bar with Python + Qt (PySide)
I tried "binarizing" the image with Python + OpenCV
The easiest way to use OpenCV with python
Binarization with OpenCV / Python
[Python] Created a class to play sin waves in the background with pyaudio
Make a breakpoint on the c layer with python
Write letters in the card illustration with OpenCV python
A memo that I touched the Datastore with python
Automate background removal for the latest portraits in a directory with Python and API
[Python] Using OpenCV with Python (Basic)
Fill JOI Difficulty 4 with Python
Make a fortune with Python
Face detection with Python + OpenCV
Call the API with python3.
Create a directory with python
Using OpenCV with Python @Mac
I replaced the Windows PowerShell cookbook with a python script.
I tried "gamma correction" of the image with Python + OpenCV
A note about hitting the Facebook API with the Python SDK
A memo when face is detected with Python + OpenCV quickly
Detect objects of a specific color and size with Python
Grayscale image is displayed as a color image in OpenCV / Python
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Create a simple video analysis tool with python wxpython + openCV
[Python] The first step to making a game with Pyxel
Estimate the attitude of AR markers with Python + OpenCV + drone
Shining life with Python and OpenCV
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
Extract the xz file with python
A python graphing manual with Matplotlib.
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
Save the result of the life game as a gif with python
Color the integration interval with matplotlib.pyplot
[Introduction to Python] How to split a character string with the split function
[Python] Using OpenCV with Python (Image transformation)
Let's make a GUI with python.
[Python] Using OpenCV with Python (Edge Detection)
Create a striped illusion with gamma correction for Python3 and openCV3
Make a cat detector with Google Colabratory (Part 2) [Python] ~ Use OpenCV ~
Get the weather with Python requests
Create a virtual environment with Python!
Get the weather with Python requests 2
How to make a surveillance camera (Security Camera) with Opencv and Python
I made a fortune with Python.
[Python] Adjusted the color map standard
Background subtraction, python, mog, gmg, opencv3.1
Find the Levenshtein Distance with python
The story of making a standard driver for db with python.
Building a virtual environment with Python 3
Easy Python + OpenCV programming with Canopy
Finding the simplest mistakes with OpenCV
Hit the Etherpad-lite API with Python
Install the Python plugin with Netbeans 8.0.2