【发布时间】:2020-05-02 15:19:01
【问题描述】:
彩色图像的像素值表示红、绿、蓝分量的总和 影响 。我想使用opencv提取每个组件的确切值,请建议!
【问题讨论】:
标签: opencv
彩色图像的像素值表示红、绿、蓝分量的总和 影响 。我想使用opencv提取每个组件的确切值,请建议!
【问题讨论】:
标签: opencv
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]
如果您打算使用 OpenCV 进行任何认真的工作,您可能还想获得 O'Reilly 的 Learning OpenCV 的副本并阅读它 - 这将在上述非常基本的问题上节省大量时间。
【讨论】:
我建议学习opencv c++ api。像素由 uchar 的向量表示。
如果这是一个彩色图像,那么我们有 3 个像素一个 uchar。
Opencv 定义typedef Vec<uchar, 3> Vec3b; 然后:
//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;
【讨论】:
此代码将打印像素 300、300 的红色和蓝色值:
img1 = cv2.imread('Image.png', cv2.IMREAD_UNCHANGED)
b,g,r = (img1[300, 300])
print (r)
print (b)
【讨论】: