How to fill OpenCV image with one solid color?
9 Answers
Using the OpenCV C API with IplImage* img
:
Use cvSet(): cvSet(img, CV_RGB(redVal,greenVal,blueVal));
Using the OpenCV C++ API with cv::Mat img
, then use either:
cv::Mat::operator=(const Scalar& s)
as in:
img = cv::Scalar(redVal,greenVal,blueVal);
or the more general, mask supporting, cv::Mat::setTo()
:
img.setTo(cv::Scalar(redVal,greenVal,blueVal));

- 16,743
- 5
- 67
- 137
-
4Should it be red, green, blue OR blue, green, red? Isn't it blue, green, red in opencv? – P.C. Sep 26 '13 at 15:52
-
18It's Blue, Green, Red. Confrmed – TheUnexpected Sep 27 '13 at 15:26
-
4Actually, OpenCV is channel-order agnostic. You are correct that the most common channel order (on PCs) is `BGR` though this is not very pertinent to this answer. It is generally the user's responsibility to keep track of channel order. – Adi Shavit Sep 27 '13 at 18:52
-
@AdiShavit can we remove the set color from the picture? – Shwetabh Shekhar Jan 20 '16 at 13:36
-
2@ShwetabhShekhar: That question has so many different meanings and answers that it is way beyond the scope of a comment... Try asking it as a full question and provide more context to what you want to do. But answering simplistically, what do you mean by *"removing"* a triplet of values from an existing C array? – Adi Shavit Jan 20 '16 at 14:48
Here's how to do with cv2 in Python:
# Create a blank 300x300 black image
image = np.zeros((300, 300, 3), np.uint8)
# Fill image with red color(set each pixel to red)
image[:] = (0, 0, 255)
Here's more complete example how to create new blank image filled with a certain RGB color
import cv2
import numpy as np
def create_blank(width, height, rgb_color=(0, 0, 0)):
"""Create new image(numpy array) filled with certain color in RGB"""
# Create black blank image
image = np.zeros((height, width, 3), np.uint8)
# Since OpenCV uses BGR, convert the color first
color = tuple(reversed(rgb_color))
# Fill image with color
image[:] = color
return image
# Create new blank 300x300 red image
width, height = 300, 300
red = (255, 0, 0)
image = create_blank(width, height, rgb_color=red)
cv2.imwrite('red.jpg', image)

- 1,886
- 1
- 21
- 28
Create a new 640x480 image and fill it with purple (red+blue):
cv::Mat mat(480, 640, CV_8UC3, cv::Scalar(255,0,255));
Note:
- height before width
- type CV_8UC3 means 8-bit unsigned int, 3 channels
- colour format is BGR

- 19,459
- 24
- 95
- 136
The simplest is using the OpenCV Mat class:
img=cv::Scalar(blue_value, green_value, red_value);
where img
was defined as a cv::Mat
.

- 11,846
- 12
- 66
- 91
Use numpy.full
. Here's a Python that creates a gray, blue, green and red image and shows in a 2x2 grid.
import cv2
import numpy as np
gray_img = np.full((100, 100, 3), 127, np.uint8)
blue_img = np.full((100, 100, 3), 0, np.uint8)
green_img = np.full((100, 100, 3), 0, np.uint8)
red_img = np.full((100, 100, 3), 0, np.uint8)
full_layer = np.full((100, 100), 255, np.uint8)
# OpenCV goes in blue, green, red order
blue_img[:, :, 0] = full_layer
green_img[:, :, 1] = full_layer
red_img[:, :, 2] = full_layer
cv2.imshow('2x2_grid', np.vstack([
np.hstack([gray_img, blue_img]),
np.hstack([green_img, red_img])
]))
cv2.waitKey(0)
cv2.destroyWindow('2x2_grid')

- 9,891
- 6
- 33
- 57
-
-
-
@orangepips, It's a nice answer. By the way, could you tell me if there is any function or operator in android (opencv) for something like [:,0]=1 in python? It is also used on your python code posted. – Gary Chen Apr 21 '21 at 15:18
color=(200, 100, 255) # sample of a color
img = np.full((100, 100, 3), color, np.uint8)

- 340
- 4
- 9
-
5@Amir Dora disagree completely. This is the best answer here by far, answers the question in the most clear, concise, and direct manner. – cdahms Mar 21 '21 at 21:00
-
@AmirDora, I completely disagree as well. The answer is concise and answers the question precisely. – Neckster Sep 30 '21 at 11:07
-
1It's a community guideline, always better to follow. But i have removed my comment. – Amir Dora. Sep 30 '21 at 12:03
For an 8-bit (CV_8U) OpenCV image, the syntax is:
Mat img(Mat(nHeight, nWidth, CV_8U);
img = cv::Scalar(50); // or the desired uint8_t value from 0-255

- 5,413
- 6
- 45
- 99
If you are using Java for OpenCV, then you can use the following code.
Mat img = src.clone(); //Clone from the original image
img.setTo(new Scalar(255,255,255)); //This sets the whole image to white, it is R,G,B value

- 2,111
- 1
- 19
- 20
I personally made this python code to change the color of a whole image opened or created with openCV . I am sorry if it's not good enough , I am beginner .
def OpenCvImgColorChanger(img,blue = 0,green = 0,red = 0):
line = 1
ImgColumn = int(img.shape[0])-2
ImgRaw = int(img.shape[1])-2
for j in range(ImgColumn):
for i in range(ImgRaw):
if i == ImgRaw-1:
line +=1
img[line][i][2] = int(red)
img[line][i][1] = int(green)
img[line][i][0] = int(blue)

- 39
- 1
- 5
-
1Welcome to SO! When you are about to answer an old question (this one is almost 10 years old) that already has an accepted answer (that is the case here) please ask yourself: Do I really have a substantial improvement to offer? If not, please consider refraining from answering. – Timus Oct 23 '20 at 21:35