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;
Related
I am doing a project on image encryption and decryption. So I need to extract RGB Color components in a color image. Please clear my doubt in open cv using c++. Thanks in advance.
Deepak Chiradoni
first load your image as
Mat img=imread(file_name);
It returns img in BGR format.
Now use following code snippet to access color components in BGR image. here y-row index and x-column index
Vec3b intensity = img.at<Vec3b>(y, x);
uchar blue = intensity.val[0];
uchar green = intensity.val[1];
uchar red = intensity.val[2];
Reference: http://docs.opencv.org/doc/user_guide/ug_mat.html
I am using BGR to HSV conversion of image using OpenCV. Since I am new to this field and software, I may sound incorrect so please let me know if I am wrong.
Now, in my school project i want to work with HSV image, which is easily converted using
cvtColor(src, dst, CV_BGR2HSV);
I suppose, that imread() function reads image in a uchar format which is 8bit unsigned image (i suppose). In that case imshow() is also uchar.
But to work with HSV image, I am not sure but I feel i need to convert use Mat3b perhaps for the distinctive H, S and V channels of the image.
Incase if I am wrong, that I want to work with H channel of the HSV image only so how can i print, or modify this channel information.
Many thanks
Perhaps you can use cv::split to devide the HSV to 3 one-channel Mat. I think this topic OpenCV:split HSV image and scan through H channel may solve your problem.
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];
i'm using that guide to detect blobs of a certain color.
On that guide, it check for orange's blob and it use this values:
int orange[3] = {200, 250, 10};
On the guide it says that this values represents the orange in HSV.
I don't know how they calculate this values but i try to check for blue value converting from rgb to hsv but it doesn't work. What i have to detect is this color:
Any ideas on how to determinate HSV values to detect colors? thanks!!
For blue, it can be used as HSV values : [120,255,255]
I calculated it from GIMP, an open-source image processing tool like Photoshop. Similarly you can use any tools to do so.
But remember in OpenCV, Hue ranges from 0-180, S = 0-255, V = 0-255
But in GIMP, it is H = 0-360, S = 0-100, V = 0-100. So you have to apply scaling according to that.
A lot of interactive sites are available on googling. You can try that.
To know how they convert these values, check its wikipedia page.
Or you can check out the OpenCV docs to see how it works in OpenCV :
cvtColor()
I just pasted the relation for you here:
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)