此代码中的错误超出范围

error in this code out of range

本文关键字:范围 错误 代码      更新时间:2023-10-16

可能的重复项:
计数像素坐标 x 和 y

我正在尝试使用此代码来对像素坐标值进行求和

这是代码

#include<cv.h>
#include<cvaux.h>
#include<highgui.h>
#include<iostream>
#include<cxtypes.h> // for cvarr 
using namespace std;

void main()
{
    IplImage* image;
    int w,h;
    char* filename;
    filename="D:\Recognition\Image Crop\7.jpg";
    image=cvLoadImage(filename,0); //load image from file 
    // Get image attribute
    w=image->width; //image width
    h=image->height; //image height
    cout<<"1. image width "<<w<<"n2. image height "<<h<<"  n";
    int Sx,Sy;
    const CvArr* arr; // msh 3arf aih lzmtha
    CvScalar se; // to store the num
    //loop 3shan 23di 3ala kol pixel
    for(int x=0;x<image->width;x++)
    {
        for(int y=0;y<image->height;y++)
        {
            se=cvGet2D(image,x,y);
            Sx=se.val[y];
            Sx+=Sx;
        }
        Sy=se.val[x];
        Sy+=Sy;
    }
    cout<<"1. image width "<<w<<"n2. image height "<<h<<"  n";
    cout<<"3. sum x ="<<Sx<<"n4. sum y ="<<Sy<<" n";
}

此代码用于计数或获取图像中 x 和 y 的像素 P(x,y) 总和

这个输出是宽度和高度的值,但不要计算 x 和 y 的值并在输出屏幕中超出范围

任何帮助

提前谢谢.

Sx=se.val[y];
Sx+=Sx;

这不是在做你认为它正在做的事情。

a += b;a = a+b;相同:在每次迭代中,您都会覆盖Sx的值,然后将其添加到自身(加倍)并将该值存储回Sx变量中。 (在下一次迭代时覆盖它的位置。