Reputation: 172
I am using this code to print the intensity of OpenCV Mat pixels. However, I am getting character printed on the console instead of values between 0 and 250:
image1 = imread(nameJpg, 0);
image2 = imread(lastJpg, 0);
for (int j = 0; j < image1.rows-10; j += 1){
for (int i = 0; i < image1.cols-10; i += 1) {
std::cout << "" << image1.at<uchar>(i, j) << " " <<i mage1.at<uchar>(i, j)<<"\n";
}
}
How can I solve this problem?
Upvotes: 0
Views: 164
Reputation: 36
You want to cast it to int, such as:
(int)image.at<uchar>(i, j)
You can test this by referring to the ASCII table.
Edit
The source of the bug you're mentioning is from how you access the pixel. i should be for the rows, and j for the columns.
for (int i = 0; i < src.rows; i++) {
for (int j = 0; j < src.cols; j++)
cout << (int)src.at<uchar>(i, j) << " ";
cout << endl;
}
Upvotes: 2
Reputation: 20324
In gray image, the pixel intensity is between 0 and 255 not 250.
If you mean that you want to get the value of a certain pixel, you may use this:
auto image=cv::imread(path,CV_LOAD_IMAGE_GRAYSCALE);
auto pixel_value=image.at<uchar>(row_idx,col_idx);
After OP's edit:
If you want to get number instead of character while printing, use this instead:
auto pixel_value= static_cast<unsigned int>(image.at<uchar>(row_idx,col_idx));
Upvotes: 0