我有以下代码。当我运行程序时,屏幕上出现未知字符而不是像素值。我想显示像素值。我该怎么做呢?谢谢你。
#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);
}
Run Code Online (Sandbox Code Playgroud)
这是结果屏幕:

将每个输出转换为整数
<< image.at<Vec3b>(i,j)[0] ...
Run Code Online (Sandbox Code Playgroud)
改成
<< (int)image.at<Vec3b>(i,j)[0] ...
Run Code Online (Sandbox Code Playgroud)
您正在打印一个char(或可能unsigned char),它通过流打印为单个字符(在 255 时看起来像您所看到的)。Castint强制它显示值的数字表示。
其他答案改变了image.at<type>原始数据的解释方式;不要那样做。必须正确解释它们。
| 归档时间: |
|
| 查看次数: |
8900 次 |
| 最近记录: |