Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a simple black image with opencv using cvcreateimage

Tags:

image

opencv

Very basic question coming from a newbie in OpenCV. I just want to create an image with every pixel set to 0 (black). I have used the following code in the main() function:

IplImage* imgScribble = cvCreateImage(cvSize(320, 240), 8, 3);

And what I get is a solid gray image, instead of the black one.

Thanks in advance !

like image 422
Kenneth Samuel Avatar asked Aug 01 '13 02:08

Kenneth Samuel


People also ask

How do I make an image black in Python?

We can use the zeros() function of NumPy to create a black image in Python. The zeros() function makes a matrix containing only zeros given the matrix's number of rows and columns.

How do I change a color image to black and white in OpenCV?

Step 1: Import OpenCV. Step 2: Read the original image using imread(). Step 3: Convert to grayscale using cv2. cvtcolor() function.


2 Answers

What version of opencv you are using? For Mat,

#include <opencv2/opencv.hpp>
cv::Mat image(320, 240, CV_8UC3, cv::Scalar(0, 0, 0));
like image 110
Jonah Avatar answered Oct 02 '22 19:10

Jonah


I can suggest two more altrnatives:

    IplImage* imgScribble = cvCreateImage(cvSize(320, 240), 8, 3);

    // Here you can set any color
    cvSet(imgScribble, cvScalar(0,0,0));


    // Here only black
    cvZero(imgScribble);
like image 33
Andrey Smorodov Avatar answered Oct 02 '22 18:10

Andrey Smorodov