I am a beginner for opencv. Yesterday I wrote a simple code in c++, which is really puzzled me. Here is my code.
#include <stdio.h>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
void cvtColor_cv()
{
Mat image;
Mat image2;
image=imread("/home/shz/alltest/c++/640.jpg",1);
namedWindow("Display Image",WINDOW_AUTOSIZE);
namedWindow("Display Image2",WINDOW_AUTOSIZE);
imshow("Display Image",image);
cvtColor(image,image2,COLOR_BGR2Luv);
imshow("Display Image2",image2);
waitKey(0);
}
void cvtColor_cal()
{
Mat image;
Mat image2;
double a[3][3];
image=imread("/home/shz/alltest/c++/640.jpg",1);
int height=image.rows;
int width=image.cols;
cout << height <<endl;
cout << width <<endl;
//Vec3b bgr=image.at<Vec3b>(0,0);
cout << image.at<Vec3b>(0,0)[0] << endl;
//namedWindow("Display Image",WINDOW_AUTOSIZE);
//namedWindow("Display Image2",WINDOW_AUTOSIZE);
//imshow("Display Image",image);
for (int i=1;i<height;i++)
{
for (int j=1;j<width;j++)
{
//cout << (int) data[j] <<endl;
//Vec3b bgr=image.at<Vec3b>(i,j);
cout << image.at<Vec3b>(i,j)[0] <<endl;
}
}
//image=1/0.17697*a*image;
//imshow("Display Image2",image);
waitKey(0);
}
int main()
{
cvtColor_cal();
return 0;
}
As we can see ,the output is:
746
1080
�
Segmentation fault (core dumped)
After I use GDB to debug this program and it prompts me:
program terminated with signal sigsegv, segmentation fault. #0 0x000000000040199e in ?? ()
Obviously, image.rows and image.cols worked well. im.at<Vec3b>(0,0)[0]
also worked but the output is a little strange.The for loops that I have tested can work well done(I used it to output a lot number “1”).
But when I write im.at<Vec3b>(i,j)[0]
into for loops. It has reported the above error.
This is really a hard question for me. The version of OpencV I use is 4.5.5. The version of gcc I use is 5.4.0.
The GDB result is here:
(gdb) r
Starting program: /home/shz/alltest/test_2022.4.9/Display
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
746
1080
�
Breakpoint 1, cvtColor_cal () at Display.cpp:49
49 Vec3b bgr=image.at<Vec3b>(i,j);
(gdb) n
Breakpoint 2, cv::Matx<unsigned char, 3, 1>::Matx (this=0x7fffffffe4b0,
values=0x3fd51eb851eb91ca <error: Cannot access memory at address 0x3fd51eb851eb91ca>) at /usr/local/include/opencv2/core/matx.hpp:673
673 for( int i = 0; i < channels; i++ ) val[i] = values[i];
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401a58 in cv::Matx<unsigned char, 3, 1>::Matx (
this=0x7fffffffe4b0,
values=0x3fd51eb851eb91ca <error: Cannot access memory at address 0x3fd51eb851eb91ca>) at /usr/local/include/opencv2/core/matx.hpp:673
673 for( int i = 0; i < channels; i++ ) val[i] = values[i];
(gdb) n
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
47
48 //cout << (int) data[j] <<endl;
49 Vec3b bgr=image.at<Vec3b>(i,j);
50 cout << image.at<Vec3b>(i,j)[0] <<endl;
51
52 }
53 }
54 //image=1/0.17697*a*image;
55 //imshow("Display Image2",image);
56