OpenCV cv::Mat 'ones' for multi-channel matrix? - c++

When working with 1-channel (e.g. CV_8UC1) Mat objects in OpenCV, this creates a Mat of all ones: cv::Mat img = cv::Mat::ones(x,y,CV_8UC1).
However, when I use 3-channel images (e.g. CV_8UC3), things get a little more complicated. Doing cv::Mat img = cv::Mat::ones(x,y,CV_8UC3) puts ones into channel 0, but channels 1 and 2 contain zeros. So, how do I use cv::Mat::ones() for multi-channel images?
Here's some code that might help you to see what I mean:
void testOnes() {
int x=2; int y=2; //arbitrary
// 1 channel
cv::Mat img_C1 = cv::Mat::ones(x,y,CV_8UC1);
uchar px1 = img_C1.at<uchar>(0,0); //not sure of correct data type for px in 1-channel img
printf("px of 1-channel img: %d \n", (int)px1); //prints 1
// 3 channels
cv::Mat img_C3 = cv::Mat::ones(x,y,CV_8UC3); //note 8UC3 instead of 8UC1
cv::Vec3b px3 = img_C3.at<cv::Vec3b>(0,0);
printf("px of 3-channel img: %d %d %d \n", (int)px3[0], (int)px3[1], (int)px3[2]); //prints 1 0 0
}
So, I would have expected to see this printout: px of 3-channel img: 1 1 1, but instead I see this: px of 3-channel img: 1 0 0.
P.S. I did a lot of searching before posting this. I wasn't able to resolve this by searching SO for "[opencv] Mat::ones" or "[opencv] +mat +ones".

I don't use OpenCV, but I believe I know what's going on here. You define a data-type, but you are requesting the value '1' for that. The Mat class appears not to pay attention to the fact that you have a multi-channel datatype, so it simply casts '1' as a 3-byte unsigned char.
So instead of using the ones function, just use the scalar constructor:
cv::Mat img_C3( x, y, CV_8UC3, CV_RGB(1,1,1) );

You can also initialize like this:
Mat img;
/// Lots of stuff here ...
// Need to initialize again for some reason:
img = Mat::Mat(Size(width, height), CV_8UC3, CV_RGB(255,255,255));

Related

convert image from BGR to ARGB using opencv

I want to convert BGR image to ABGR/ARGB.There are conversion BGR2RGBA in opencv but not BGR2ABGR or BGR2ARGB.
It is possible with opencv or using any other method?
The required operation can be accomplished by swapping the image channels using cv::mixChannels as follows:
cv::Mat bgr, bgra;
//bgr initialization code here...
//.
//.
//.
cv::cvtColor(bgr, bgra, cv::COLOR_BGR2BGRA);
cv::Mat abgr(bgra.size(), bgra.type());
int from_to[] = { 0,3, 1,1, 2,2, 3,0 };
cv::mixChannels(&bgra,1,&abgr,1,from_to,4);
from_to array is the mapping function which specifies which channels from source will be copied to which channels of the destination image. The pairs indicate that channel number 0 of the input will be copied to channel number 3 of the output, 1 to 1, 2 to 2, and channel number 3 will be copied to channel number 0 of the output.
Alternatively, we can split the image channels, swap the required channels and merge again. It can be done as follows:
cv::cvtColor(bgr, bgra, cv::COLOR_BGR2BGRA);
std::vector<cv::Mat> channels_bgra;
cv::split(bgra, channels_bgra);
std::vector<cv::Mat> channels_abgr = { channels_bgra[3], channels_bgra[1], channels_bgra[2], channels_bgra[0] };
cv::merge(channels_abgr, abgr);
OpenCV doesn't support ARGB or ABGR formats, so you will not be able to display it or use some of the functions on it... However, it is possible to create them with split and merge functions of OpenCV. Here is some code to explain what I mean.
cv::Mat src, final_image;
// fill src as you prefer
std::vector<cv::Mat> channels;
cv::split(src, channels); // this will put each channel in a mat in the vector
// swap or add channels in the vector
cv::Mat alpha(src.rows, src.cols, CV_8U, cv::Scalar(255));
channels.push_back(alpha);
std::reverse(channels.begin(), channels.end()); //needs <algorithm>
// merge the channels in one new image
cv::merge(channels, final_image);
This can be done faster (maybe it will be just shorter) with the function mixChannels, but I will say that this one is a little bit more confusing.

accessing pixel value of gray scale image in OpenCV

I just want to get my concept clear that - is accessing all the matrix elements of cv::Mat means I am actually accessing all the pixel values of an image (grayscale - 1 channel and for colour - 3 channels)? Like suppose my code for printing the values of matrix of gray scale that is 1 channel image loaded and type CV_32FC1, is as shown below, then does that mean that I am accessing only the members of the cv::mat or I am accessing the pixel values of the image (with 1 channel - grayscale and type CV_32FC1) also?
cv::Mat img = cv::imread("lenna.png");
for(int j=0;j<img.rows;j++)
{
for (int i=0;i<img.cols;i++)
{
std::cout << "Matrix of image loaded is: " << img.at<uchar>(i,j);
}
}
I am quite new to image processing with OpenCV and want to clear my idea. If I am wrong, then how can I access each pixel value of an image?
You are accessing the elements of the matrix and you are accessing the image itself also. In your code, after you do this:
cv::Mat img = cv::imread("lenna.png");
the matrix img represents the image lenna.png. ( if it is successfully opened )
Why don't you experiment yourself by changing some of the pixel values:
cv::Mat img = cv::imread("lenna.png");
//Before changing
cv::imshow("Before",img);
//change some pixel value
for(int j=0;j<img.rows;j++)
{
for (int i=0;i<img.cols;i++)
{
if( i== j)
img.at<uchar>(j,i) = 255; //white
}
}
//After changing
cv::imshow("After",img);
Note: this only changes the image values in volatile memory, that is where the mat img is currently loaded. Modifying the values of the mat img, not going to change value in your actual image "lenna.png",which is stored in your disk, (unless you do imwrite)
But in case of 1-channel grayscale image it is CV_8UC1 not CV_32FC1
In order to get the pixel value of the grayscale image (an integer between 0 and 255), the answer also needs to be typecasted.
int pixelValue = (int)img.at<uchar>(i,j);

Using Mat::at(i,j) in opencv for a 2-D Mat object

I am using Ubuntu 12.04 and OpenCV 2
I have written the following code :
IplImage* img =0;
img = cvLoadImage("nature.jpg");
if(img != 0)
{
Mat Img_mat(img);
std::vector<Mat> RGB;
split(Img_mat, RGB);
int data = (RGB[0]).at<int>(i,j)); /*Where i, j are inside the bounds of the matrix size .. i have checked this*/
}
The problem is I am getting negative values and very large values in the data variable. I think I have made some mistake somewhere. Can you please point it out.
I have been reading the documentation (I have not finished it fully.. it is quite large. ) But from what I have read, this should work. But it isnt. What is going wrong here?
Img_mat is a 3 channeled image. Each channel consists of pixel values uchar in data type.
So with split(Img_mat, BGR) the Img_mat is split into 3 planes of blue, green and red which are collectively stored in a vector BGR. So BGR[0] is the first (blue) plane with uchar data type pixels...hence it will be
int dataB = (int)BGR[0].at<uchar>(i,j);
int dataG = (int)BGR[1].at<uchar>(i,j);
so on...
You have to specify the correct type for cv::Mat::at(i,j). You are accessing the pixel as int, while it should be a vector of uchar. Your code should look something like this:
IplImage* img = 0;
img = cvLoadImage("nature.jpg");
if(img != 0)
{
Mat Img_mat(img);
std::vector<Mat> BGR;
split(Img_mat, BGR);
Vec3b data = BGR[0].at<Vec3b>(i,j);
// data[0] -> blue
// data[1] -> green
// data[2] -> red
}
Why are you loading an IplImage first? You are mixing the C and C++ interfaces.
Loading a cv::Mat with imread directly would be more straight-forward.
This way you can also specify the type and use the according type in your at call.

How should I name my RGB channels, using cv::Mat_

I want to access my matrix elements in the following manner:
frame[i][j].Red
, that is, the (i,j)-th pixe's red channel.
I have tried:
typedef struct{unsigned char Blue,Green,Red;}Pixel;
typedef cv::Mat_<Pixel> Image;
However when trying to imread(), imwrite() or whatever with the thus defined type, g++ greets me with:
OpenCV Error: Assertion failed (func != 0) in convertTo, file /home/users/mvitkov/projects/opencv-legacy/OpenCV-2.3.1/modules/core/src/convert.cpp, line 937
terminate called after throwing an instance of 'cv::Exception'
what(): /home/users/mvitkov/projects/opencv-legacy/OpenCV-2.3.1/modules/core/src/convert.cpp:937: error: (-215) func != 0 in function convertTo
Update: So no answer to my probably badly asked question. Too bad. The essence of the question is how to address the individual channels with sensible names (red, green, bkue), and not the c-era array indexing notation [2]. Duh!
Here's how you access each channel:
blue = frame.at<cv::Vec3b>(i,j)[0];
green = frame.at<cv::Vec3b>(i,j)[1];
red = frame.at<cv::Vec3b>(i,j)[2];
The above code assumes that you have a 3-channel image where each value is an 8-bit unsigned char (CV_8UC3). This type is used in many common image formats. However, if you have a different type of 3-channel image, here's what you do:
If the image type is 3-channel float (CV_32FC3), then replace cv::Vec3b with cv::Vec3f
If the image type is 3-channel double (CV_64FC3), then replace cv::Vec3b with cv::Vec3d
If the image type is 3-channel int (CV_32SC3), then replace cv::Vec3b with cv::Vec3i
If the image type is 3-channel short int (CV_16SC3) or 16-bit uchar (CV_16UC3), then replace cv::Vec3b with cv::Vec3s
Not sure what image format you're using? Try calling getImgType(frame) (see the code below).
string getImgType(cv::Mat frame)
{
int imgTypeInt = frame.type();
int numImgTypes = 28; // 7 base types, with 4 channel options each (C1, ..., C4)
int enum_ints[] = {CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4, CV_8SC1, CV_8SC2, CV_8SC3, CV_8SC4, CV_16UC1, CV_16UC2, CV_16UC3, CV_16UC4, CV_16SC1, CV_16SC2, CV_16SC3, CV_16SC4, CV_32SC1, CV_32SC2, CV_32SC3, CV_32SC4, CV_32FC1, CV_32FC2, CV_32FC3, CV_32FC4, CV_64FC1, CV_64FC2, CV_64FC3, CV_64FC4};
string enum_strings[] = {"CV_8U", "CV_8UC1", "CV_8UC2", "CV_8UC3", "CV_8UC4", "CV_8SC1", "CV_8SC2", "CV_8SC3", "CV_8SC4", "CV_16UC1", "CV_16UC2", "CV_16UC3", "CV_16UC4", "CV_16SC1", "CV_16SC2", "CV_16SC3", "CV_16SC4", "CV_32SC1", "CV_32SC2", "CV_32SC3", "CV_32SC4", "CV_32FC1", "CV_32FC2", "CV_32FC3", "CV_32FC4", "CV_64FC1", "CV_64FC2", "CV_64FC3", "CV_64FC4"};
for(int i=0; i<numImgTypes; i++)
{
if(imgTypeInt == enum_ints[i]) return enum_strings[i];
}
return "unknown image type";
}
If the image type is 3-channel short int (CV_16SC3) or 16-bit uchar (CV_16UC3), then replace cv::Vec3b with cv::Vec3s
this is simply not correct. I adressed a 3 channel 16 bit unsigned image once with < Vec3s>, and got a negative value G:29096 B:-21671 R:23413 returned.
If CV_16UC3, adress Mat with < Vec3w>. Using this I got G:29096 B:43865 R:23413
the "s" in CV_16SC3 stands for signed and not short.

Access to each separate channel in OpenCV

I have an image with 3 channels (img) and another one with a single channel (ch1).
Mat img(5,5,CV_64FC3);
Mat ch1 (5,5,CV_64FC1);
Is there any efficient way (not using for loop) to copy the first channel of img to ch1?
In fact, if you just want to copy one of the channels or split the color image in 3 different channels, CvSplit() is more appropriate (I mean simple to use).
Mat img(5,5,CV_64FC3);
Mat ch1, ch2, ch3;
// "channels" is a vector of 3 Mat arrays:
vector<Mat> channels(3);
// split img:
split(img, channels);
// get the channels (dont forget they follow BGR order in OpenCV)
ch1 = channels[0];
ch2 = channels[1];
ch3 = channels[2];
There is a function called cvMixChannels. You'll need to see implementation in the source code, but I bet it is well optimized.
You can use split function and then put zeros to the channels u want to ignore. This will result dispalying one channels out of three. See below..
For example:
Mat img, chans[3];
img = imread(.....); //make sure its loaded with an image
//split the channels in order to manipulate them
split(img, chans);
//by default opencv put channels in BGR order , so in your situation you want to copy the first channel which is blue. Set green and red channels elements to zero.
chans[1]=Mat::zeros(img.rows, img.cols, CV_8UC1); // green channel is set to 0
chans[2]=Mat::zeros(img.rows, img.cols, CV_8UC1);// red channel is set to 0
//then merge them back
merge(chans, 3, img);
//display
imshow("BLUE CHAN", img);
cvWaitKey();
You can access a specific channel, it works faster than the split operation
Mat img(5,5,CV_64FC3);
Mat ch1;
int channelIdx = 0;
extractChannel(img, ch1, channelIdx); // extract specific channel
// or extract them all
vector<Mat> channels(3);
split(img, channels);
cout << channels[0].size() << endl;
A simpler one if you have a RGB with 3 channels is cvSplit() if i'm not wrong, you have less to configure... (and i think it is also well optimized).
I would use cvMixChannel() for "harder" tasks... :p (i know i am lazy).
here is the documentation for cvSplit()