Opencv C ++错误,无法获取某些像素的像素强度值

我正在制作一个项目来查找图像中“网格点”的像素强度值。 '网格点'被定义为位于图像中9×9点网格顶点的像素。 这个项目的目的是实现H.Chi Wong,Marshall Bern,David Goldberg在论文'An Image signature for any kind image'中提到的算法的步骤2。 这是研究论文的链接:CiteSeerX

我正在使用OpenCV 2.4.11使用Visual Studio 2012和C ++。 我已经阅读了其他帖子(OpenCV Error Assertion在某些Pixal值上失败)中的答案,它建议使用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<<"nn";

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就会出界:

xgridpoint=i*floor(columns/10); // will be 350 for i == 7 in your example

这会导致你的代码失败

Scalar gridpoint =img.at<uchar>(xgridpoint,ygridpoint);

小心:当你用at访问Mat at ,你应该给(row_index, col_index) 。 您可能想要编写img.at<uchar>(ygridpoint, xgridpoint)

链接地址: http://www.djcxy.com/p/15877.html

上一篇: Opencv C++ Error, unable to obtain pixel intensity value for certain pixels

下一篇: OpenCV: Normalizing pixel values of an image