Ask Your Question
0

Single Pixel Colors

asked 2013-10-02 20:45:46 -0600

dmcenan gravatar image

Hi Folks,

I am just starting out with openCV and I understand how a 3 channel image uses a combination of RGB values to display a color image. The code below will produce a black and a white window but where do these values come from? Where is it specified that the values from 001 to 255 span the colors black to white with shades of grey in between? Is there a lookup chart like there is for color images with 3 values per pixel?

Also when I draw a circle, openCV let's me specify the color as '0' which is equal to black. When I use '0' or '000' in the code below, openCV produces an error. Why is that?

Mat blackImage(200, 200, CV_8U, 001);

Mat whiteImage(200, 200, CV_8U, 255);

Thanks.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2013-10-03 01:51:15 -0600

Moster gravatar image

If you want a 3 channel matrix which has the same value everywhere, you need to use a Scalar:

Mat (100, 100, CV_8UC3, Scalar(0,0,0));

or if you use a single channel Mat

Mat (100, 100, CV_8UC1, Scalar(0));

Also, you are questions are mixing stuff up. If you just have 1 single channel, its a grayscale image. So imagine the numbers from 0-255 to be the strength of the color gray. With 0 being black, weakest gray, and 255 being white, the strongest.

If you have 3 channels, its basically the same, just that each channel represents the strength of its respective color (red, green, blue). If all channels, have the same number (like (200, 200, 200)) its also just a grayscale image again.

edit flag offensive delete link more
0

answered 2013-10-04 21:25:51 -0600

dmcenan gravatar image

updated 2013-10-04 21:27:04 -0600

Hi guys thanks for your replies.

Michael, below is the exact code I am using (GCC 4.7.2, OpenCV 2.4.5) but when I change the last parameter in the Mat declaration from 001 to 0 or 000 etc to represent black, the program compiles fine but generates the following runtime error (Why is this?):

OpenCV Error: Null pointer (The matrix has NULL data pointer) in cvGetMat, file /home/dme/Documents/opencv-2.4.5/modules/core/src/array.cpp, line 2387 terminate called after throwing an instance of 'cv::Exception' what(): /home/dme/Documents/opencv-2.4.5/modules/core/src/array.cpp:2387: error: (-27) The matrix has NULL data pointer in function cvGetMat

#include <opencv2 core="" core.hpp=""> #include <opencv2 highgui="" highgui.hpp=""> #include <iostream>

using namespace std;
using namespace cv;

int main()
{
    Mat blackImage(200, 200, CV_8U, 001);

    namedWindow("Black");
    imshow("Black", blackImage);

waitKey(0);
}
edit flag offensive delete link more

Comments

Just read my post. Mat actually has a lot of constructors, but none that really fits the one you are using. You are trying to express a color with the numbers you put in there, but actually its using this one: Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP). This will fail, when your *data is 0, since it expresses a null pointer. So, just use the Scalar construct like I showed you. Its the correct way.

Moster gravatar imageMoster ( 2013-10-04 23:49:38 -0600 )edit

Question Tools

Stats

Asked: 2013-10-02 20:45:46 -0600

Seen: 809 times

Last updated: Oct 04 '13