I am new to kinect project
And I am implementing a depth threshold when distance is greater than 400mm
for (UINT y = 0; y < pImg->rows; ++y)
{
// Get row pointers for Mats
const USHORT* pDepthRow = depth->ptr<USHORT>(y);
for (UINT x = 0; x < pImg->cols; ++x)
{
USHORT raw_depth = pDepthRow[x];
SHORT realDepth = NuiDepthPixelToDepth(raw_depth);
// If depth value is valid, convert and copy it
if (raw_depth != 65535)
{
if(realDepth >400 ) //greater than 400mm
{
pImg->at<Vec4b>(y,x)[0] = 255;
pImg->at<Vec4b>(y,x)[1] = 255;
pImg->at<Vec4b>(y,x)[2] = 255;
pImg->at<Vec4b>(y,x)[3] = 255;
}
else
{
pImg->at<Vec4b>(y,x)[0] = 0;
pImg->at<Vec4b>(y,x)[1] = 0;
pImg->at<Vec4b>(y,x)[2] = 0;
pImg->at<Vec4b>(y,x)[3] = 0;
}
}
}
It seems get the correct result but reduces the frame rate massively.
When I want to get rid of the loop by using the cv::inRange, but this function only support 8U1C when the raw depth is 16U.
So what else can I use to segment the depth according to the real distance?
Try to improve performance by storing a reference to the pixel.
Change this:
if (realDepth > 400) //greater than 400mm
{
pImg->at<Vec4b>(y,x)[0] = 255;
pImg->at<Vec4b>(y,x)[1] = 255;
pImg->at<Vec4b>(y,x)[2] = 255;
pImg->at<Vec4b>(y,x)[3] = 255;
}
else
{
pImg->at<Vec4b>(y,x)[0] = 0;
pImg->at<Vec4b>(y,x)[1] = 0;
pImg->at<Vec4b>(y,x)[2] = 0;
pImg->at<Vec4b>(y,x)[3] = 0;
}
To this:
(I donĀ“t know what T is because I dont know what pImg is.
T should be equal to the return value of the at method. I assume it is Vec4b.)
T& pixel = pImg->at<Vec4b>(y, x); // probably Vec4b& pixel = ..
if (realDepth > 400) //greater than 400mm
{
pixel[0] = 255;
pixel[1] = 255;
pixel[2] = 255;
pixel[3] = 255;
}
else
{
pixel[0] = 0;
pixel[1] = 0;
pixel[2] = 0;
pixel[3] = 0;
}
Related
I am trying to adjust the brightness and contrast of an RGB image but the output is not what I expect.
The function is callbacked from createTrackbar() function with values from 0 to 100.
Please check the image below. I would appreciate some help. Thanks.
void brightness_callback(int brightness, void *userdata)
{
int height = image_input.rows, width = image_input.cols;
image_output = Mat::zeros(image_input.size(), image_input.type());
int widthStep = image_input.step;
int nChannels = 3;
uchar *pDataInput = (uchar *)image_input.data;
uchar *pDataOutput = (uchar *)image_output.data;
for (int x = 0; x < height; x++, pDataInput += widthStep, pDataOutput += widthStep) {
uchar *pRowInput = pDataInput;
uchar *pRowOutput = pDataOutput;
for (int y = 0; y < width; y++, pRowInput += nChannels, pRowOutput += nChannels) {
uchar B = pRowInput[0];
uchar G = pRowInput[1];
uchar R = pRowInput[2];
pRowOutput[0] = truncate((uchar)(B + brightness));
pRowOutput[1] = truncate((uchar)(G + brightness));
pRowOutput[2] = truncate((uchar)(R + brightness));
}
}
imshow(window_original, image_output);
}
uchar truncate(uchar value) {
if (value < 0) return 0;
else if (value > 255) return 255;
return value;
}
I think that in previous versions FreeImage_ConvertTo24Bits(FIBITMAP*) work ok in any type of FIBITMAP* but in 3.18 it returns nullptr if the FIBITMAP* is a floating point texture.
Is there anything I missed? Anyone has noticed this change too? Should I make a intermediate transform?
Thank you in advance for any tip.
I didn't manage to make it work so I coded my own version I'll put this here for future readers:
inline BYTE LinToSRGB(float C)
{
C = std::fmin( std::fmax(C, 0.0f), 1.0f);
if (C > 0.0031308f) {
C = 1.055f * (pow(C, (1.0f / 2.4f))) - 0.055f;
} else {
C = 12.92f * C;
}
return static_cast<BYTE>(C*255.0f);
}
FIBITMAP* ConvertTo24Bits(FIBITMAP* origImage)
{
FIBITMAP* modImage = FreeImage_ConvertTo24Bits(origImage);
if(modImage!=nullptr)
{
return modImage;
}
const FREE_IMAGE_TYPE src_type = FreeImage_GetImageType(origImage);
const unsigned width = FreeImage_GetWidth(origImage);
const unsigned height = FreeImage_GetHeight(origImage);
modImage = FreeImage_Allocate(width, height,24);
const unsigned src_pitch = FreeImage_GetPitch(origImage);
const unsigned dst_pitch = FreeImage_GetPitch(modImage);
const BYTE *src_bits = (BYTE*)FreeImage_GetBits(origImage);
BYTE *dst_bits = (BYTE*)FreeImage_GetBits(modImage);
if(src_type == FIT_RGBAF)
{
for(unsigned y = 0; y < height; y++) {
const FIRGBAF *src_pixel = (FIRGBAF*) src_bits;
for(unsigned x = 0; x < width; x++) {
// convert and skip alpha channel
*dst_bits = LinToSRGB(src_pixel[x].blue);
dst_bits++;
*dst_bits = LinToSRGB(src_pixel[x].green);
dst_bits++;
*dst_bits = LinToSRGB(src_pixel[x].red);
dst_bits++;
}
src_bits += src_pitch;
}
}
else if(src_type == FIT_RGBF)
{
for(unsigned y = 0; y < height; y++) {
const FIRGBF *src_pixel = (FIRGBF*) src_bits;
for(unsigned x = 0; x < width; x++) {
// convert and skip alpha channel
*dst_bits = LinToSRGB(src_pixel[x].blue);
dst_bits++;
*dst_bits = LinToSRGB(src_pixel[x].green);
dst_bits++;
*dst_bits = LinToSRGB(src_pixel[x].red);
dst_bits++;
}
src_bits += src_pitch;
}
}
return modImage;
}
I am trying to implement the Matlab function imquantize using opencv. Which opencv thresholding function I should use to implement Matlab function multithresh? Once thresholding has been done how do I label the pixels according to the threshold? Is this the right way to implement imquantize ? Are there any other function's I should include in the code?
There is an implementation based on OpenCV here, where you should probably get the idea:
cv::Mat
imquantize(const cv::Mat& in, const arma::fvec& thresholds) {
BOOST_ASSERT_MSG(cv::DataType<float>::type == in.type(), "input is not of type float");
cv::Mat index(in.size(), in.type(), cv::Scalar::all(1));
for (int i = 0; i < thresholds.size() ; i++) {
cv::Mat temp = (in > thresholds(i)) / 255;
temp.convertTo(temp, cv::DataType<float>::type);
index += temp;
}
return index;
}
Updated: thresholds are the vector of the float threshold values (uniform distributed to # of levels that you want to quantize within [0, 1]). Check the code snippet of how it is used:
const float step = 1./levels[i];
arma::fvec thresh = arma::linspace<arma::fvec>(step, 1.-step, levels[i]-1);
channels[i] = imquantize(channels[i], thresh);
I suppose you are looking for something like this
/*function imquantize
* 'inputImage' is the input image.
* 'levels' is an array of threholds
* 'quantizedImage' is the reurned image
* with quantized levels.
*/
Mat imquantize(Mat inputImage, vector<vector<int> > levels)
{
//initialise output label matrix
Mat quantizedImage(inputImage.size(), inputImage.type(), Scalar::all(1));
//Apply labels to the pixels according to the thresholds
for (int i = 0; i < inputImage.cols; i++)
{
for (int j = 0; j < inputImage.rows; j++)
{
// Check if image is grayscale or BGR
if(levels.size() == 1)
{
for (int k = 0; k < levels[0].size(); k++)
{
// if pixel < lowest threshold , then assign 0
if(inputImage.at<uchar>(j,i) <= levels[0][0])
{
quantizedImage.at<uchar>(j,i) = 0;
}
// if pixel > highest threshold , then assign 255
else if(inputImage.at<uchar>(j,i) >= levels[0][levels[0].size()-1])
{
quantizedImage.at<uchar>(j,i) = 255;
}
// Check the level borders for pixel and assign the corresponding
// upper bound quanta to the pixel
else
{
if(levels[0][k] < inputImage.at<uchar>(j,i) && inputImage.at<uchar>(j,i) <= levels[0][k+1])
{
quantizedImage.at<uchar>(j,i) = (k+1)*255/(levels[0].size());
}
}
}
}
else
{
Vec3b pair = inputImage.at<Vec3b>(j,i);
// Processing the Blue Channel
for (int k = 0; k < levels[0].size(); k++)
{
if( pair.val[0] <= levels[0][0])
{
quantizedImage.at<Vec3b>(j,i)[0] = 0;
}
else if( pair.val[0] >= levels[0][levels.size()-1])
{
quantizedImage.at<Vec3b>(j,i)[0] = 255;
}
else
{
if(levels[0][k] < pair.val[0] && pair.val[0] <= levels[0][k+1])
{
quantizedImage.at<Vec3b>(j,i)[0] = (k+1)*255/(levels[0].size());
}
}
}
// Processing the Green Channel
for (int k = 0; k < levels[1].size(); k++)
{
if( pair.val[1] <= levels[1][0])
{
quantizedImage.at<Vec3b>(j,i)[1] = 0;
}
else if( pair.val[1] >= levels[1][levels.size()-1])
{
quantizedImage.at<Vec3b>(j,i)[1] = 255;
}
else
{
if(levels[1][k] < pair.val[1] && pair.val[1] <= levels[1][k+1])
{
quantizedImage.at<Vec3b>(j,i)[1] = (k+1)*255/(levels[1].size());
}
}
}
// Processing the Red Channel
for (int k = 0; k < levels[2].size(); k++)
{
if( pair.val[2] <= levels[2][0])
{
quantizedImage.at<Vec3b>(j,i)[2] = 0;
}
else if( pair.val[2] >= levels[2][levels.size()-1])
{
quantizedImage.at<Vec3b>(j,i)[2] = 255;
}
else
{
if(levels[2][k] < pair.val[2] && pair.val[2] <= levels[2][k+1])
{
quantizedImage.at<Vec3b>(j,i)[2] = (k+1)*255/(levels[2].size());
}
}
}
}
}
}
return quantizedImage;
}
In this function the input had to be an Mat::Image and a 2D vector which can have different levels for different channels.
I am acquiring images using a digital camera. At first, I was using a mono camera, but recently I upgraded to a color camera. With the mono camera I was having some palette issues until I found this bit of code to alter the palette to a grayscale palette:
for(int i=0; i<256; i++)
{
pbmi->bmiColors[i].rgbRed = BYTE(i);
pbmi->bmiColors[i].rgbGreen = BYTE(i);
pbmi->bmiColors[i].rgbBlue = BYTE(i);
pbmi->bmiColors[i].rgbReserved = BYTE(0);
}
where pbmi is a BITMAPINFO*.
This worked just fine for the mono camera. But now with the color camera I obviously don't want to make the images grayscale. However, if I remove that chunk of code I get the same palette issues that I was getting before with the mono camera. So it seems to me like I need to do something similar as I did before and create a palette, only this time a color palette.
For reference, here is the rest of the pbmi's settings:
//// INFO ////
BITMAPINFO* pbmi = (BITMAPINFO*)alloca( sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD)*256);
pbmi->bmiHeader.biSize = sizeof (pbmi->bmiHeader);
pbmi->bmiHeader.biWidth = 2752;
pbmi->bmiHeader.biHeight = -2200;
pbmi->bmiHeader.biPlanes = 1;
pbmi->bmiHeader.biBitCount = 8;
pbmi->bmiHeader.biCompression = BI_RGB;
pbmi->bmiHeader.biSizeImage = 0;
pbmi->bmiHeader.biXPelsPerMeter = 14173;
pbmi->bmiHeader.biYPelsPerMeter = 14173;
pbmi->bmiHeader.biClrUsed = 0;
pbmi->bmiHeader.biClrImportant = 0;
So far, I have tried the following:
for(int i=0,a = 0; i < 64; i++)
{
pbmi->bmiColors[i].rgbRed = BYTE(a);
pbmi->bmiColors[i+64].rgbGreen = BYTE(a);
pbmi->bmiColors[i+64+64].rgbBlue = BYTE(a);
pbmi->bmiColors[i+64+64+64].rgbReserved = BYTE(0);
a += 4;
}
//This created a palette consisting only of cyan, yellow, and magenta colors.
//Didn't work.
for(int i=0,r=0,g=0,b=0; b <= 255; i++)
{
if(r >= 256)
{
r = 0;
g++;
}
if(g >= 256)
{
g = 0;
b++;
}
pbmi->bmiColors[i].rgbRed = BYTE(r);
pbmi->bmiColors[i].rgbGreen = BYTE(g);
pbmi->bmiColors[i].rgbBlue = BYTE(b);
pbmi->bmiColors[i].rgbReserved = BYTE(0);
r++;
}
//Here I was trying to basically count up hexadecimally from 000000 to FFFFFF.
//Caused an access violation error.
I've also tried each of those after changing pbmi->bmiHeader.biBitCount to 16, 24, and 32, none of which worked.
So my question is: How do I create a color palette based on the BITMAPINFO settings I have provided?
If you are trying to create a simple RGB pallete, you just need to change the R, G e B values from 0 to 255:
const int MaxIndex = 255;
for(int r=0; r <= MaxIndex; r++)
for(int g=0; g <= MaxIndex; g++)
for(int b=0; b <= MaxIndex; b++)
{
i = r * MaxIndex * MaxIndex + g * MaxIndex + b
pbmi->bmiColors[i].rgbRed = BYTE(r);
pbmi->bmiColors[i].rgbGreen = BYTE(g);
pbmi->bmiColors[i].rgbBlue = BYTE(b);
pbmi->bmiColors[i].rgbReserved = BYTE(0);
}
I have to use glDrawPixels to implement a raster algorithm.
Right now I'm only trying to get a simple example of glDrawPixels working but having an issue.
GLint height, width, size = 0;
GLbyte *image = NULL;
int i,j=0;
width = 512;
height = 512;
size = width*height;
image = (GLbyte*)malloc(sizeof(GLbyte)*size*3);
for(i = 0; i < size*3; i=i+width*3){
for(j = i; j < width*3; j=j+3){
image[j] = 0xFF;
image[j+1] = 0x00;
image[j+2] = 0x00;
}
}
glDrawPixels(width, height, GL_RGB, GL_BYTE, image);
free(image);
gluSwapBuffers();
Above is the code that I'm trying to get to work, from my understanding it should simply draw a 512x512 red square.
However what I get is one red row at the bottom and everything else is grey.
Your second for() loop is broken -- you're starting at i, but only going up to width * 3, so it doesn't run at all when i > 0.
Here's a simpler approach:
GLbyte *p = image;
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
*p++ = 0xFF;
*p++ = 0x00;
*p++ = 0x00;
}
}
Your loop conditions look off to me. (After the first row, the condition on j will always be true, and the inner loop won't execute.) An easier way to do it would be to do something like this:
for (y = 0; y < height; y++)
{
// Go to the start of the next row
GLbyte* rowStart = image + (width * 3) * y;
GLbyte* row = rowStart;
for (x = 0; x < width; x++)
{
row [ x * 3 ] = 0xFF;
row [ (x * 3) + 1 ] = 0x00;
row [ (x * 3) + 2 ] = 0x00;
}
}