我有以下代码。当我运行程序时,屏幕上显示的是未知字符而不是像素值。我想要显示像素值。我该如何做?谢谢。
这是结果屏幕:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("/home/fd/baby.jpg");
for( int i = 0 ; i < image.rows ; i++)
{
for( int j = 0 ; j < image.cols ; j++ )
{
if(image.type() == CV_8UC1)
{
image.at<uchar>(i,j) = 255;
}
else if(image.type() == CV_8UC3)
{
cout << image.at<Vec3b>(i,j)[0] << " " << image.at<Vec3b>(i,j)[1] << " " << image.at<Vec3b>(i,j)[2] << endl;
image.at<Vec3b>(i,j)[0] = 255;
image.at<Vec3b>(i,j)[1] = 255;
image.at<Vec3b>(i,j)[2] = 255;
cout << image.at<Vec3b>(i,j)[0] << " " << image.at<Vec3b>(i,j)[1] << " " << image.at<Vec3b>(i,j)[2] << endl;
}
else
{
cout << "Anknown image format" << endl;
return 0;
}
}
}
imshow("Result İmage", image);
waitKey(0);
}
这是结果屏幕:
cout << img << endl;
。而不仅仅是cout << int(uchar_value);
。 - berakchar
默认情况下都会被打印为单个字符而不是数字。将其提升为更大的类型可以避免这种情况。(如果cout << 'A'
打印 65,那么会引起混淆。归咎于向后兼容性。) - Alan Stokes