get color of each pixel from sl::zed::Mat - mat

i have a variable sl::zed::Mat and i want to get color of each pixel from that Mat. i was tried using getValue(x,y) to get 3 channels, but i can't get reach the value over 50.
here's my codes:
sl::zed::Mat color = data->_color;
sl::uchar3 bgrPixel = color.getValue(i, j);
i'm really confused, are they the right color or not? or you have another way to get color from each pixel of that variable type?

Related

can't access pixel intensities in image using cimg (returns 0)

I'm tries to access Cimg pixel values to print out the pixel intensity that my mouse is at, as well as calculating a histogram. However, I got all zeros from Cimg object.
The cimg image is initiated from memory buffer and it is 12 bit gray scale image, but padded to 16 bit to save in memory.
The code below is defined in a function that is called multiple times. I want to refresh the images in the current display and not to produce a new one every time the function is called. So the Cimgdisp is defined outside the function.
#include "include\CImg.h"
int main(){
CImg <unsigned short> image(width,height,1,1);
CImgDisplay disp(image);
//showImg() get called multiple times here
}
void showImg(){
unsigned short* imgPtr = (unsigned short*) (getImagePtr());
CImg <unsigned short> img(imgPtr,width,height);
img*=(65535/4095);//Renormalise from 12 bit input to 16bit for better display
//Display
disp->render(img);
disp->paint();
img*=(4095/65535);//Normalise back to get corect intensities
CImg <float> hist(img.histogram(100));
hist.display_graph(0,3);
//find mouse position and disp intensity
mouseX = disp->mouse_x()*width/disp->width();//Rescale the position of the mouse to true position of the image
mouseY = disp->mouse_y()*height/disp->height();
if (mouseX>0&mouseY>0){
PxIntensity = img(mouseX,mouseY,0,0);}
else {
PxIntensity = -1;}
}
All the intensities I retrieve are zero and the histogram is also zero.
img*=(4095/65535);//Normalise back to get corect intensities is incorrect, as (4095/65535)=0 in C/C++ (division of an integer by a larger one).
Maybe img*=(4095/65535.); ?
If you just want to scale between 12-bit and 16-bit and back then just using bit-shifts might be better.
img<<=4;//Renormalise from 12 bit input to 16bit for better display
//Display
disp->render(img);
disp->paint();
img>>=4;//Normalise back to get corect intensities

Vignettation with white color in opencv

im working on a vignette filter in openCV and i tried the code in this question ( Creating vignette filter in opencv? ), and it works perfectly.
But now I'm trying to modify it to create a white vignetting filter and I can't find a way to turn it so that it shows white color vignette instead of black.
ADDITIONALY TO ANSWER
After modifying the code there are some points I'd like to make clear for any future programmers/developers or people interested in the problem.
What is said in the answer is basically to do a weighted addition of pixels. Simple addition can be easily done using openCV's AddWeighted. This can be use to do blending with any color, not just black or white. However this is not simple addition since we do not have the same blending level everyuwhere, but instead level of blending is given by the gradient;
pseudocode looks like:
pixel[][] originalImage; //3 channel image
pixel[][] result; //3 channel image
pixel[][] gradient; //1 channel image
pixel color; //pixel for color definition of color to blend with
generateGradient(gradient); //generates the gradient as one channel image
for( x from 0 to originalImage.cols )
{
for( y from 0 to originalImage.rows )
{
pixel blendLevel = gradient[x][y];
pixel pixelImage = originalImage[x][y];
pixel blendcolor = color;
//this operation is called weighted addition
//you have to multiply the whole pixel (every channel value of the pixel)
//by the blendLevel, not just one channel
result[x][y] = (blendLevel * pixelImage) + ( ( 1 - blendLevel ) * blendColor );
}
}
Say, you darken your colour fore by a factor of x. Then to blend it with a different colour back, you take x * fore + (1 - x) * back. I don't remember the exact OpenCV syntax; looking at your link, I would write something like this:
cv::Mat result = maskImage * img + (1.0 - maskImage) * white;
If you convert your image to the CIE Lab colour space (as in the vignette code), which would be a good idea, don't forget to do the same for white.

how to fill color into image, by considering small matrix size 3*3 or 6*6? in opencv

How can i fill color to required image of type Mat,by considering the small matrix of 3*3,i searched the net,but couldn't find correct answer to implement,i hope u guys can help better,thanks in advance ,or else please upload some links if possible.
How can i fill color to required image of type Mat?
If you want to change the value of a Mat image, you can do as follows:
Grayscale (8UC1):
uchar intensity = img.at<uchar>(y, x) = value_you_want_to_fill;
Color image (BGR color ordering, the default format returned by imread):
img.at<Vec3b>(y, x) = Vec3b(B_value, G_value, R_value);

Rendering pixels from array of RGB values in SDL 1.2?

I'm working on a NES emulator right now and I'm having trouble figuring out how to render the pixels. I am using a 3 dimensional array to hold the RGB value of each pixel. The array definition looks like this for the 256 x 224 screen size:
byte screenData[224][256][3];
For example, [0][0][0] holds the blue value, [0][0][1] holds the green values and [0][0][2] holds the red value of the pixel at screen position [0][0].
When the vblank flag goes high, I need to render the screen. When SDL goes to render the screen, the screenData array will be full of the RGB values for each pixel. I was able to find a function named SDL_CreateRGBSurfaceFrom that looked like it may work for what I want to do. However, all of the examples I have seen use 1 dimensional arrays for the RGB values and not a 3 dimensional array.
What would be the best way for me to render my pixels? It would also be nice if the function allowed me to resize the surface somehow so I didn't have to use a 256 x 224 window size.
You need to store the data as an unidimensional char array:
int channels = 3; // for a RGB image
char* pixels = new char[img_width * img_height * channels];
// populate pixels with real data ...
SDL_Surface *surface = SDL_CreateRGBSurfaceFrom((void*)pixels,
img_width,
img_height,
channels * 8, // bits per pixel = 24
img_width * channels, // pitch
0x0000FF, // red mask
0x00FF00, // green mask
0xFF0000, // blue mask
0); // alpha mask (none)
In 2.0, use SDL_Texture + SDL_TEXTUREACCESS_STREAMING + SDL_RenderCopy, it's faster than SDL_RenderPoint.
See:
official example: http://hg.libsdl.org/SDL/file/e12c38730512/test/teststreaming.c
my derived example which does not require blob data and compares both methods: https://github.com/cirosantilli/cpp-cheat/blob/0607da1236030d2e1ec56256a0d12cadb6924a41/sdl/plot2d.c
Related: Why do I get bad performance with SDL2 and SDL_RenderCopy inside a double for loop over all pixels?

getting Y value[Ycbcr] of one Pixel in opencv

I'm trying to get the Y value of pixel from a frame that's in Ycbcr color mode.
here what I' wrote:
cv::Mat frame, Ycbcrframe, helpframe;
........
cvtColor(frame,yCbCrFrame,CV_RGB2YCrCb); // converting to Ycbcr
Vec3b intensity =yCbCrFrame.at<uchar>(YPoint);
uchar yv = intensity.val[0]; // I thought it's my Y value but its not, coz he gives me I think the Blue channel of RGB color space
any Idea how what the correct way to do that
what about the following code?
Vec3f Y_pix = YCbCrframe.at<Vec3f>(rows, cols);
int pixelval = Y_pix[0];
(P.S. I havent tried it yet)
You need to know both the depth (numerical format and precision of channel sample) as well as the channel count (typically 3, but can also be 1 (monochrome) or 4 (alpha-containing)), ahead of time.
For 3-channel, 8-bit unsigned integer (a.k.a. byte or uchar) pixel format, each pixel can be accessed with
mat8UC3.at<cv::Vec3b>(pt);