openCV look up table for 16CU1 - c++

I need to replace the value of a Mat 8UC1 [0,255] to values of a cv::Mat lookUpTable(1, 256, CV_16UC1); I check un this OpenCV tutorial an explanation which is the fastest method, however, when Im checking the assigned values of the LUT in each position im only sabing 8-bits so Im lossing the other 8-bits. This is the source code:
unsigned short int zDTableHexa[256]={0};
.... get the values...
cv::Mat lookUpTable(1, 256, CV_16UC1);
uchar* p = lookUpTable.data;
for( int i = 0; i < 256; i++){
p[i] = zDTableHexa[i];
cout<<(int)p[i]<<":"<<zDTableHexa[i]<<sizeof(p[i])<<":"<<sizeof(zDTableHexa[i])<<endl;
}
The printing result are:
104:872
101:869
97:865
93:861
90:858
86:854
83:851
80:848
76:844
73:841
70:838
66:834
63:831
When I check in binary is only the first 8-bits.
I understand that the pointer is UCHAR(8bits) but how I can assign the full value?

try
unsigned short* p = (unsigned short*) lookUpTable.data;

OpenCV LUT is working just with CV_8U, so what you can do is splitting the numbers in 3, that is 3x CV_8U, or CV_8UC3. But you cannot have more than 256 elements in the LUT and not other values than 0..255 as index.
In other words, you can parse uchars to uchars or uchars to floats: CV_8UC1 -> CV_8UC1 or CV_8UC1 -> CV_8UC3 (I did not tried it whit CV_8UC4, it could work).
For getting the elements of CV_8UC3 check cv::Vec3b
I found this, that might interest you

Related

OpenCV - RGB Channels in Float Data Type and Intensity Range within 0-255

How can I achieve the values of the RGB channels as
Float data type
Intensity range within 0-255
I used CV_32FC4 as the matrix type since I'll perform floating-point mathematical operations to implement Daltonization. I was expecting that the intensity range is the same with the intensity range of the RGB Channels in CV_8UC3, just having a different data type. But when I printed the matrix I noticed that the intensities of the channels are not within 0-255. I realized that it due to the range of the float matrix type.
Mat mFrame(height, width, CV_32FC4, (unsigned char *)pNV21FrameData);
for(int y = 0 ; y < height ; y++){
for(int x = 0 ; x < width ; x++){
Vec4f BGRA = mFrame.at<Vec4f>(y,x);
// Algorithm Implementation
mFrame.at<Vec4f>(y,x) = BGRA;
}
}
Mat mResult;
mFrame.convertTo(mResult, CV_8UC4, 1.0/255.0);
I need to manipulate the pixels like BGRA[0] = BGRA[0] * n; then assign it back to the matrix.
By your comments and the link in it I see that the data comes in BGRA. The data is in uchar.
I assume this from this line:
Mat mResult(height, width, CV_8UC4, (unsigned char *)poutPixels);
To solve this you can create the matrix and then convert it to float.
Mat mFrame(height, width, CV_8UC4, (unsigned char *)pNV21FrameData);
Mat mFloatFrame;
mFrame.convertTo(mFloatFrame, CV_32FC4);
Notice that this will keep the current ranges (0-255) if you need another one (like 0-1) you may put the scaling factor.
Finally you can convert back, but beware that this function does saturate_cast. If you have an specific way you want to manage the overflow or the decimals, you will have to do it before converting it.
Mat mResult;
mFloatFrame.convertTo(mResult, CV_8UC4);
Note that 1.0/255.0 is not there, since the data is already in the range of 0-255 (at least before the operations).
One final comment, the link in your comments use IplImage and other old C (deprecated) versions of OpenCv. If you are working in c++, stick to the c++ versions like Mat. This is not in the code you show here, but in the you linked. This comment is more for you to avoid future headaches.

grayscale image creation 16 bits

I am using openCV for the first time. I am using openCV3 and XCode to code it. I want to create a 16 bit grayscale image but I want to the data I have is defined such that 4000 is the pixel value for white and 0 for black. I have the information for these pixels in an array of type int. How can I create a Mat and assign the values in the array to the Mat?
short data[] = { 0,0,4000,4000,0,0,4000, ...};
Mat gray16 = Mat(h, w, CV_16S, data);
again, the types must match. for 16bit, you need CV_16S and a shortarray, for 8bit CV_8U and a uchar* array, for float CV_32S and a float* ....
You can create your Mat with
cv::Mat m(rows, cols, CV_16UC1);
but to my knowledge, there is no way to define a custom value for "white", you'll have to multiply m with std::numeric_limits::max / 4000. However, this is only necessary when displaying the image.
A lookup-table could do the same (potentially slower), see cv::LUT. However, it appearently only supports 8-bit images.
edit: OK, I missed the part about assigning existing array values; see berak's answer. I hope the answer is still useful.

Trying to implement a tiny part of matlab code in C++ using opencv

I am trying to convert an image to double precision using opencv. I am trying to imitate the im2double function available in MATLAB in c++. So, for this what i did was..
Mat IMG = imread("lena.bmp");
Size size(8,8);
Mat img,img_re,grey;
cvtColor( IMG, img, CV_BGR2GRAY );
resize(img,img_re,size);
img_re.convertTo( grey, CV_64FC3, 1.0/255.0 );
std::cout<<grey<<std::endl;
unsigned char *input = (unsigned char*)(grey.data);
grey: [0.3764705882352941, 0.5176470588235293, 0.4352941176470588, 0.8274509803921568;
0.392156862745098, 0.5254901960784314, 0.7372549019607844, 0.6431372549019607;
0.4431372549019608, 0.6431372549019607, 0.7176470588235294, 0.5607843137254902;
0.5333333333333333, 0.3254901960784314, 0.6862745098039216, 0.8431372549019608]
The data stored in grey is almost similar to the data obtained from matlab. the pixels have a range of [0,1]here. But ,my problem starts here. I want to now access the pixel values from 'grey' and save it to a boost matrix.
So for this i use..
for (unsigned i=0; i < height; ++i)
{
for (unsigned j=0; j < width; ++j )
{
image(i,j) = input[grey.step * j + i ];
}
}
image:: [4,4]((24,24,144,144),(24,24,144,144),(24,216,144,224),(24,63,144,63))
After this step all the values in the matrix have a range of [0,255]. grey scale images are between [0,255] but why do it get the values between [0,1] in the first case.
please stay away from accessing Mat's raw 'data' pointer, and use:
grey.at<double>(i,j);
instead.
also, if im_re is a 1 channel, grayscale image, your typeflag is wrong, should be:
img_re.convertTo( grey, CV_64F, 1.0/255.0 );

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.

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

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));