89

How to fill OpenCV image with one solid color?

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
Rella
  • 65,003
  • 109
  • 363
  • 636

9 Answers9

112

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));
Adi Shavit
  • 16,743
  • 5
  • 67
  • 137
  • 4
    Should 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
  • 18
    It's Blue, Green, Red. Confrmed – TheUnexpected Sep 27 '13 at 15:26
  • 4
    Actually, 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
43

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)
Kimmo
  • 1,886
  • 1
  • 21
  • 28
14

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
Stéphane
  • 19,459
  • 24
  • 95
  • 136
11

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.

Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91
8

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')
orangepips
  • 9,891
  • 6
  • 33
  • 57
7
color=(200, 100, 255) # sample of a color 
img = np.full((100, 100, 3), color, np.uint8)
Leox
  • 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
  • 1
    It's a community guideline, always better to follow. But i have removed my comment. – Amir Dora. Sep 30 '21 at 12:03
6

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
AlainD
  • 5,413
  • 6
  • 45
  • 99
4

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
Karthik N G
  • 2,111
  • 1
  • 19
  • 20
0

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)
KAVYA SHARMA
  • 39
  • 1
  • 5
  • 1
    Welcome 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