当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Issue accessing/editing pixel location in Mat image

问题描述:

im trying to access and modify pixel values from an image I read into opencv. I read several posts on how to do this, however, they dont seem to work for me.

my code:

int main()

{

Mat src=imread("/home/jaysinh/Pictures/shapes.jpg");

cout<<"rows:"<<src.rows<<endl;

cout<<"cols:"<<src.cols<<endl;

cout<<src.at<cv::Vec3b>(10,10)[0]<<endl;

waitKey(0);

return 0;

}

gives me the result:

In this image im trying to see the pixel values at location (10,10) in the image. I tried to output all the values of the image but they still look like special characters.

I also tried Scalar and unchar types instead of Vec3b but nothing seems to give me the appropriate value (between 0-255). Type double gives me -NaN for every pixel location. I checked the src.type() of the image and that returned 16 which I figure is 16S type.

How can I modify the image or somehow access the image pixel values of this type and modify it?

Thanks in advance!

(here is the image im trying to access:

网友答案:

cout interprets your byte as a char, and tries to print something ascii ;) so just cast to int:

cout << int(src.at<Vec4b>(3,3)[0]) << endl;   
// 100

or print the whole pixel:

cout << src.at<Vec4b>(3,3) << endl;
// [100, 23, 90]

yes, you can manipulate it:

Vec3b & pixel = src.at<Vec4b>(3,3);
pixel[1] += 5;
cout << src.at<Vec4b>(3,3) << endl;
// [100, 28, 90]
分享给朋友:
您可能感兴趣的文章:
随机阅读: