源码如下:

#include
int main(int argc, char** argv)
{
cv::Mat img_rgb, img_gry, img_cny;
cv::namedWindow(“Example Gray”,cv::WINDOW_AUTOSIZE);
cv::namedWindow(“Example Canny”, cv::WINDOW_AUTOSIZE);
img_rgb = cv::imread(argv[1]);
cv::cvtColor(img_rgb, img_gry, cv::COLOR_BGR2GRAY); //生成一个和原图一样大小,但只有一个通道的图像,从而将BGR图像转换为灰度值
cv::imshow(“Example Gray”, img_gry);
cv::Canny(img_gry, img_cny, 10, 100, 3, true);
cv::imshow(“Example Canny”, img_cny);
cv::waitKey(0);

int x = 16, y = 32;
cv::Vec3b intensity = img_rgb.at(y, x);
uchar blue = intensity[0];//读写像素值 blue
uchar green = intensity[1]; //读写像素值 green
uchar red = intensity[2]; //读写像素值 red
std::cout << "At(x,y)=(" << x << "," << y << "):(blue,green,red)=(" << (unsigned int)blue <<
“,” << (unsigned int)green << "," << (unsigned int)red << ")" << std::endl;
std::cout <<"Gray pixel there is:" << (unsigned int)img_gry.at(y, x) << std::endl;
x /= 4; y /= 4;
}