我正在制作一个项目来找出'网格点的像素强度值。在图像中。 '格点'被定义为位于图像中9×9网格点的顶点处的像素。该项目的目的是实现论文中提到的算法的第2步'任何类型的图像的图像签名' H.Chi Wong,Marshall Bern,David Goldberg?这是研究论文的链接:CiteSeerX
我正在使用OpenCV 2.4.11的Visual Studio 2012和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;
}
提前感谢您的帮助。解决这个问题对于这个项目非常重要和有用,我非常感谢您提供的任何帮助。
答案 0 :(得分:1)
我认为xgridpoint
一旦达到第7次迭代就会超出范围:
xgridpoint=i*floor(columns/10); // will be 350 for i == 7 in your example
会导致代码失败
Scalar gridpoint =img.at<uchar>(xgridpoint,ygridpoint);
注意:当您使用Mat
访问at
时,您应该提供(row_index, col_index)
。您可能想要写img.at<uchar>(ygridpoint, xgridpoint)
。