Opencv C++ Error, unable to obtain pixel intensity value for certain pixels

I am making a project to find out the pixel intensity values of 'gridpoints' in an image. 'Gridpoints' are defined as the pixels which are at the vertices of an 9x9 grid of points in an image. The intention of this project is to implement step 2 of the algorithm mentioned in the paper 'An Image signature for any kind of Image' by H.Chi Wong, Marshall Bern, David Goldberg? This is the link to the research paper: CiteSeerX

I am using Visual Studio 2012 and C++ using OpenCV 2.4.11. I have read answers in other posts (OpenCV Error Assertion failed on some Pixal Values) which suggest using cvtColor(Mask,Mask,CV_BGR2GRAY) but it results in an additional error.

I am able to calculate the values of five gridpoints. However, I get the following errors when the program is executed:

错误

This is the code:

#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;
}

Thanks in advance for your help. Solving this problem is very important and useful for this project and I highly appreciated any help that you may offer.


I think xgridpoint is going out of bounds as soon as you reach the 7th iteration:

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

which causes your code to fail on

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

Be careful: when you access a Mat with at , you're supposed to give (row_index, col_index) . You might want to write img.at<uchar>(ygridpoint, xgridpoint) .

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

上一篇: 设置彩色像素的Opencv最终会模糊相邻像素

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