pfc
pfc

Reputation: 1920

opencv c++ load image, get very large pixel values

I'm new to c++ opencv. I load an image and print its pixel values using the following code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/videoio.hpp>
cv::Mat cv_img;
cv_img = cv::imread("./input_image/000000.jpg", cv::IMREAD_COLOR);
std::cout << cv_img.rows << "," << cv_img.cols << "," << cv_img.channels() << std::endl;
for (int i = 0; i < 20; ++i)
{
    for (int j = 0; j < 20; ++j)
    {
        std::cout << cv_img.at<cv::Vec3w>(i, j)[0] << ", " << cv_img.at<cv::Vec3w>(i, j)[1] << ", " << cv_img.at<cv::Vec3w>(i, j)[2] << std::endl;
    }
}

for (int i = 0; i < 20; ++i)
{
    for (int j = 0; j < 20; ++j)
    {
        std::cout << cv_img.at<cv::Vec3i>(i, j)[0] << ", " << cv_img.at<cv::Vec3i>(i, j)[1] << ", " << cv_img.at<cv::Vec3i>(i, j)[2] << std::endl;
    }
}

But when I print its pixels, I got values like the following:

5139, 4882, 4628
5139, 4882, 4628
269488655, 336662803, 320147986
320017682, 370349078, 336990996

This is definitely wrong. I try to load the same image using opencv-python, like follows:

import cv2
img = cv2.imread("./input_image/000000.jpg", cv2.IMREAD_COLOR)
print(img[0:20,0:20,:])

I get the normal output like follows:

 [[58 59 55]
  [30 31 27]
  [28 29 25]
  ...
  [23 24 20]
  [22 23 19]
  [21 22 18]]

What's wrong with my c++ code?

I tried to replace with cv::Vec3b, but it did not work. The prints are as follows:

, <, 8
, ,

(many white spaces)

Thank you all for helping me!

Upvotes: 2

Views: 751

Answers (1)

ZdaR
ZdaR

Reputation: 22974

As per the docs. You should have used cv::vec3b to access the pixel values of uchar datatype.

Upvotes: 1

Related Questions