Opencv C++错误,无法获取某些像素的像素强度值
Opencv C++ Error, unable to obtain pixel intensity value for certain pixels
我正在做一个项目来找出图像中 'gridpoints' 的像素强度值。 'Gridpoints' 被定义为图像中 9x9 网格点的顶点处的像素。该项目的目的是实现 H.Chi Wong、Marshall Bern、David Goldberg 的论文 'An Image signature for any kind of Image' 中提到的算法的第 2 步?这是研究论文的link:CiteSeerX
我正在使用 Visual Studio 2012 和使用 OpenCV 2.4.11 的 C++。我在其他帖子 (OpenCV Error Assertion failed on some Pixal Values) 中阅读了建议使用 cvtColor(Mask,Mask,CV_BGR2GRAY)
的答案,但它会导致额外的错误。
我能够计算五个网格点的值。但是,在执行程序时出现以下错误:
这是代码:
#include <iostream>
#include <stdio.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include <math.h>
using namespace cv;
using namespace std;
int main( int argc, const char** argv )
{
Mat img = imread("Mountain_8-Bit_Grayscale.jpg", CV_LOAD_IMAGE_GRAYSCALE); //read the image data in the file "MyPic.JPG" and store it in 'img'
cout<<"Step 1: Image is converted to grayscale image \n";
cout<<"Step 2: This step is skipped and the image is not cropped currently, assuming that there will not be significant change in the image signature. \n";
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window
//cvtColor(img,img,CV_BGR2GRAY);
int rows=img.rows;
cout<<"No.of rows "<<rows<<"\n";
int columns= img.cols;
cout<<"No. of columns "<<columns<<"\n";
int x,y;
x=450;
y=34;
Scalar intensity = img.at<uchar>(y, x);
cout<<"The intensity at "<<x<<" and " <<y<<" is "<<intensity.val[0]<<"\n";// gives the intensity of the pixel which is the first element of the array
Scalar pintensity;
long colintensitysum=0;
int rowno=0;
int colno=0;
long totalcolintsum=0;
/* for(rowno;rowno<=rows;rowno++)
{
//pintensity= img.at<uchar>(colno,rowno);
pintensity= img.at<uchar>(1,rowno);
colintensitysum=colintensitysum + pintensity.val[0];
if(rowno==rows-1)//incorrect block of statements
{
cout<<colintensitysum<<" ";
totalcolintsum=totalcolintsum+colintensitysum;
colintensitysum=0;
colno++;
}
if (colno==columns-1)
{break;}
} *
cout<<"The sum of the pixel intensities of the given column is "<<colintensitysum<<"\n";
cout<<"The sum of the pixel intensities of all the pixels in the image is "<<totalcolintsum<<"\n"; */
int x1cord= floor(columns/10);
int y1cord=floor(rows/10);
Scalar centergridpoint= img.at<uchar>(y1cord,x1cord);
cout<<"The intensity of the first grid point is "<<centergridpoint<<"\n\n";
int xgridpoint;
int ygridpoint;
xgridpoint=0;
ygridpoint=floor(rows/10);
int gridpointintensity=0;
int columnnumber=0;
Scalar gridpoint =img.at<uchar>(xgridpoint,ygridpoint);
int count=0;
for(int i=1;i<=10;i++)
{
//columnnumber=i*floor(columns/10);
xgridpoint=i*floor(columns/10);
if (xgridpoint==10*floor(columns/10))
{
count++;
ygridpoint=count*floor(rows/10);
cout<<"\n";
i=1;
xgridpoint=0;
cout<<"\nI'm in the if condition !!!! \n";
}
Scalar gridpoint =img.at<uchar>(xgridpoint,ygridpoint);
gridpointintensity=gridpoint.val[0];
cout<<gridpointintensity<<" ";
}
float side;
side = (2,floor(0.5+min(columns,rows)/20));
cout<<"\n \n Step 3: The side of the square centered at the grid point is "<<side;
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
return 0;
}
在此先感谢您的帮助。解决这个问题对这个项目非常重要和有用,我非常感谢您提供的任何帮助。
我认为 xgridpoint
在您到达第 7 次迭代时就越界了:
xgridpoint=i*floor(columns/10); // will be 350 for i == 7 in your example
这会导致您的代码在
上失败
Scalar gridpoint =img.at<uchar>(xgridpoint,ygridpoint);
注意:当您使用 at
访问 Mat
时,您应该提供 (row_index, col_index)
。你可能想写 img.at<uchar>(ygridpoint, xgridpoint)
.
我正在做一个项目来找出图像中 'gridpoints' 的像素强度值。 'Gridpoints' 被定义为图像中 9x9 网格点的顶点处的像素。该项目的目的是实现 H.Chi Wong、Marshall Bern、David Goldberg 的论文 'An Image signature for any kind of Image' 中提到的算法的第 2 步?这是研究论文的link:CiteSeerX
我正在使用 Visual Studio 2012 和使用 OpenCV 2.4.11 的 C++。我在其他帖子 (OpenCV Error Assertion failed on some Pixal Values) 中阅读了建议使用 cvtColor(Mask,Mask,CV_BGR2GRAY)
的答案,但它会导致额外的错误。
我能够计算五个网格点的值。但是,在执行程序时出现以下错误:
这是代码:
#include <iostream>
#include <stdio.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include <math.h>
using namespace cv;
using namespace std;
int main( int argc, const char** argv )
{
Mat img = imread("Mountain_8-Bit_Grayscale.jpg", CV_LOAD_IMAGE_GRAYSCALE); //read the image data in the file "MyPic.JPG" and store it in 'img'
cout<<"Step 1: Image is converted to grayscale image \n";
cout<<"Step 2: This step is skipped and the image is not cropped currently, assuming that there will not be significant change in the image signature. \n";
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window
//cvtColor(img,img,CV_BGR2GRAY);
int rows=img.rows;
cout<<"No.of rows "<<rows<<"\n";
int columns= img.cols;
cout<<"No. of columns "<<columns<<"\n";
int x,y;
x=450;
y=34;
Scalar intensity = img.at<uchar>(y, x);
cout<<"The intensity at "<<x<<" and " <<y<<" is "<<intensity.val[0]<<"\n";// gives the intensity of the pixel which is the first element of the array
Scalar pintensity;
long colintensitysum=0;
int rowno=0;
int colno=0;
long totalcolintsum=0;
/* for(rowno;rowno<=rows;rowno++)
{
//pintensity= img.at<uchar>(colno,rowno);
pintensity= img.at<uchar>(1,rowno);
colintensitysum=colintensitysum + pintensity.val[0];
if(rowno==rows-1)//incorrect block of statements
{
cout<<colintensitysum<<" ";
totalcolintsum=totalcolintsum+colintensitysum;
colintensitysum=0;
colno++;
}
if (colno==columns-1)
{break;}
} *
cout<<"The sum of the pixel intensities of the given column is "<<colintensitysum<<"\n";
cout<<"The sum of the pixel intensities of all the pixels in the image is "<<totalcolintsum<<"\n"; */
int x1cord= floor(columns/10);
int y1cord=floor(rows/10);
Scalar centergridpoint= img.at<uchar>(y1cord,x1cord);
cout<<"The intensity of the first grid point is "<<centergridpoint<<"\n\n";
int xgridpoint;
int ygridpoint;
xgridpoint=0;
ygridpoint=floor(rows/10);
int gridpointintensity=0;
int columnnumber=0;
Scalar gridpoint =img.at<uchar>(xgridpoint,ygridpoint);
int count=0;
for(int i=1;i<=10;i++)
{
//columnnumber=i*floor(columns/10);
xgridpoint=i*floor(columns/10);
if (xgridpoint==10*floor(columns/10))
{
count++;
ygridpoint=count*floor(rows/10);
cout<<"\n";
i=1;
xgridpoint=0;
cout<<"\nI'm in the if condition !!!! \n";
}
Scalar gridpoint =img.at<uchar>(xgridpoint,ygridpoint);
gridpointintensity=gridpoint.val[0];
cout<<gridpointintensity<<" ";
}
float side;
side = (2,floor(0.5+min(columns,rows)/20));
cout<<"\n \n Step 3: The side of the square centered at the grid point is "<<side;
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
return 0;
}
在此先感谢您的帮助。解决这个问题对这个项目非常重要和有用,我非常感谢您提供的任何帮助。
我认为 xgridpoint
在您到达第 7 次迭代时就越界了:
xgridpoint=i*floor(columns/10); // will be 350 for i == 7 in your example
这会导致您的代码在
上失败Scalar gridpoint =img.at<uchar>(xgridpoint,ygridpoint);
注意:当您使用 at
访问 Mat
时,您应该提供 (row_index, col_index)
。你可能想写 img.at<uchar>(ygridpoint, xgridpoint)
.