I am trying to make an image which holds the ascii values of a array of characters. Each pixel must contain ascii value of each characters in the text. I wrote the image using imwrite
, but when i try to access the image using imread
the value of the pixels is different.
My program is:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace cv;
using namespace std;
void makeimage()
{
Mat img(100,100,CV_8UC3);
for(int i=0;i<100;++i)
{
for(int j=0;j<100;++j)
{
img.at<Vec3b>(i,j)[0]='a';
img.at<Vec3b>(i,j)[1]='b';
img.at<Vec3b>(i,j)[2]='c';
}
}
imwrite("image.jpg",img);
}
void readimage()
{
Mat image=imread("image.jpg",IMREAD_UNCHANGED);
for(int i=0;i<100;++i)
{
for(int j=0;j<100;++j)
{
cout<<(char)image.at<Vec3b>(i,j)[0 ];
cout<<(char)image.at<Vec3b>(i,j)[1 ];
cout<<(char)image.at<Vec3b>(i,j)[2 ]<<" ";
}
}
}
int main(int argc, char *argv[])
{
makeimage();
readimage();
waitKey(0);
return 0;
}
The output is shown in the image
You are saving your image in jpeg , a lossy format that compresses the image and thus modifies the values.
Save your image in a lossless format, like png , and it will work as expected.
...
imwrite("image.png",img);
...
Mat image=imread("image.png",IMREAD_UNCHANGED);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.