C++:openCV error -
hi trying rgb pixel in image using c++ opencv 2.4.5
but error when compile .
it loads image exception arise when trying rgb pixel
can 1 me please ?
the following code loads image , find rgb pixel @ index 25,4
code :
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv/cv.h> #include <opencv/highgui.h> #include <iostream> using namespace std; using namespace cv; int main() { int x; mat input = imread("c:/red.jpg"); if (input.empty()) { std::cout << "!!! failed imread(): image not found" << std::endl; cin>>x; return 0; // don't let execution continue, else imshow() crash. } imshow("input", input); vec3f pixel = input.at<vec3f>(25, 40); if( !pixel[0]) { std::cout << "!!! failed pixel(): image not found" << std::endl; cin>>x; return 0; } int b = pixel[0]; int g = pixel[1]; int r = pixel[2]; cout<<b; cout <<" "; cout<<r; cout <<" "; cout <<g; cin>>b; /* // detect squares after filtering... */ return 0; }
you image of type cv_8uc3, means value of pixel stored 3-channel 8-bit uchar. in
vec3f pixel = input.at<vec3f>(25, 40);
you trying access pixel values floats, since in opencv vec3f
defined typedef vec<float, 3> vec3f;
. causes program crush. instead should be:
vec3b pixel = input.at<vec3b>(25, 40);
in opencv vec3b
defined typedef vec<uchar, 3> vec3b;
, want.
here's documentation cv:: vec data type.
edit can output pixel data with
cout << pixel << endl;
or this:
printf("[%u, %u, %u]", pixel[0], pixel[1], pixel[2]);
or this:
int b = static_cast<int>(pixel[0]); int g = static_cast<int>(pixel[1]); int r = static_cast<int>(pixel[2]); cout<< "[" << b <<", " << g << ", " << r << "]" << endl;
Comments
Post a Comment