Pixel Intensity in opencv# - opencv

I am a beginner in both Image Processing and Opencv. I am trying to find out the individual pixel intensities of an image, using OPENCV#. There is assistance here: http://docs.opencv.org/doc/user_guide/ug_mat.html?highlight=pixel%20intensity for the same issue. But I am not sure how to use it in OPENCV#.
I know this is a very basic query. Please try to help out. Thanks in advance.

Pixel intensity is the same thing as that pixel's grayscale value. To get a grayscale (pixel intensity) version of an BGR image you can do this:
cv::cvtColor(bgr_mat,gray_mat,CV_BGR2GRAY);
Now the 3 channel BGR image has been converted to a 1 channel GRAYSCALE image. To find the intensity of pixel (x,y) in the gray image you can do this:
//NOTE: in OpenCV pixels are accessed in (row,col) format
int intensity = (int)gray_mat.at<uchar>(y,x);
Since each grayscale pixel is stored as uchar, the value of intensity will range from (0-255) where 255 is maximum intensity (seen as a completely white pixel).

in emgu cv, you can do it like this.
//Color
//Red
byte Red_val = My_Image.Data[y,x,0];
//Green
byte Green_val = My_Image.Data[y,x,1];
//Blue
byte Blue_val = My_Image.Data[y,x,2];
//Greyscale
byte Gray_val = My_Image.Data[y,x,0];

Related

Histogram equalization upon RGB images? RGB ouput possible?

I was writing code for histogram equalization upon RGB images?
It was suggested not performing equalization operation against R-G-B channels respectively.
So I first converted RGB to YUV color space and then performed equalization on Y channel (only), leaving U and V channel as what they were, converted altered Y channel with original U and V channels back to RGB color space.
The (RGB) resulting output was not ideal, while the gray scale ouput generated from Y channel only was quite acceptable.
My question is, Is it possible to get a full color RGB equalized ouput? And how? Should I perform equalization operation on U&V channel as well?

Convert image to grayscale with custom luminosity formula

I have images containing gray gradations and one another color. I'm trying to convert image to grayscale with opencv, also i want the colored pixels in the source image to become rather light in the output grayscale image, independently to the color itself.
The common luminosity formula is smth like 0.299R+0.587G+0.114B, according to opencv docs, so it gives very different luminosity to different colors.
I consider the solution is to set some custom weights in the luminosity formula.
Is it possible in opencv? Or maybe there is a better way to perform such selective desaturation?
I use python, but it doesnt matter
This is the perfect case for the transform() function. You can treat grayscale conversion as applying a 1x3 matrix transformation to each pixel of the input image. The elements in this matrix are the coefficients for the blue, green, and red components, respectively since OpenCV images are BGR by default.
im = cv2.imread(image_path)
coefficients = [1,0,0] # Gives blue channel all the weight
# for standard gray conversion, coefficients = [0.114, 0.587, 0.299]
m = np.array(coefficients).reshape((1,3))
blue = cv2.transform(im, m)
So you have custom formula,
Load source,
Mat src=imread(fileName,1);
Create gray image,
Mat gray(src.size(),CV_8UC1,Scalar(0));
Now in a loop, access BGR pixel of source like,
Vec3b bgrPixel=src.at<cv::Vec3b>(y,x); //gives you the BGR vector of type cv::Vec3band will be in row, column order
bgrPixel[0]= Blue//
bgrPixel[1]= Green//
bgrPixel[2]= Red//
Calculate new gray pixel value using your custom equation.
Finally set the pixel value on gray image,
gray.at<uchar>(y,x) = custom intensity value // will be in row, column order

Color descriptor OpenCV

I am using openCV to extract color information for each pixel in an image. I have found openCV has provided that through "OpponentColorDescriptorExtractor", but I have no idea how to use it. Can some one please provide me demo code that extracts color information pixel by pixel in an image? Thank you.
to access a pixel, use:
uchar v = img.at<uchar>(row,col); // grayscale, 1chan, 8bit
Vec3b v = img.at<Vec3b>(row,col); // rgb, 3chan, 3*8bit
// v[0]==b; v[1]==g; v[00]==r;

How can I know the red, blue and red component value of a pixel in a color image using openCV?

The pixel value of a colored image represents the total of the Red , green , blue component
effect . I want to extract the exact value for each component using opencv, Please suggest !
It's all in the OpenCV FAQ Wiki:
Suppose, we have 8-bit 3-channel image I (IplImage* img):
I(x,y)blue ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3]
I(x,y)green ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3+1]
I(x,y)red ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3+2]
You might also want to get a copy of O'Reilly's Learning OpenCV and read it if you're planning to do any serious work with OpenCV - it will save a lot of time on very basic questions such as the above.
I suggest to learn opencv c++ api. Pixels are represented by vector of uchar.
If this is a color image then we have 3 uchar by pixel.
Opencv defines typedef Vec<uchar, 3> Vec3b; then:
//load image
cv::Mat img = cv::imread("myimage.png",1); // 1 means color image
/* Here the cv::Mat can be seen as cv::Mat_<Vec3b>
* Matrix of uchar with 3 channels for BGR (warning this is not RGB)
*/
// access to pixel value
cv::Vec3b mypix = img.at<Vec3b>(i,j);
uchar bluevalue = mypix.x;
This code will print the red and blue value of pixel 300, 300:
img1 = cv2.imread('Image.png', cv2.IMREAD_UNCHANGED)
b,g,r = (img1[300, 300])
print (r)
print (b)

how to set hue value of some pixel with opencv

I need to change the hue of some pixels of my image, but i don't know how to set them!
I converted the image in HSV with CV_BGR2HSV and now i'm cycling with a for by rows and cols...
how can I access each pixel's hue?
for setting RGB i'm using this code...
CvScalar s;
s=cvGet2D(imgRGB,i,j); // get the (i,j) pixel value
printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]);
s.val[0]=240;
s.val[1]=100;
s.val[2]=100;
cvSet2D(imgRGB,i,j,s); // set the (i,j) pixel value
You already converted your image to HSV, so the 3 layers of the image now correspond to Hue, Saturation and Value:
s.val[0] is the hue.
s.val[1] is the saturation.
s.val[2] is the value.
So go ahead and use exactly the same method as for your RGB images to get and set the pixel values.
Yes, openCV uses 180° i.e., (0°-179°) cylinder of HSV; while normally its (0°-240°) in MS paint and ideally (0°-360°). So, openCV gives you result of hue from (0°-179°).

Resources