1

我尝试使用此代码对图像中的像素坐标 (x,y) 求和

这是代码

#include<cv.h>
#include<cvaux.h>
#include<stdio.h>
#include<highgui.h>
#include<iostream>
#include<cxtypes.h> // for cvarr 
using namespace std;
// This program to count the pixel value in the gray image
void main()
{
    IplImage* image;
    int w,h;
    char* filename;
    filename="D:\\Recognition\\Image Crop\\7.jpg";
    image=cvLoadImage(filename,0); //for grayscal image
    // 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;
    CvScalar se; // to store the num
    for(int x=0;x>image->width;x++)
    {
        for(int y=0;image->height;y++)
        {
            se=cvGet2D(image,x,y);
            Sx=se.val[y];
            Sx+=Sx;
        }
        Sy=se.val[x];
        Sy+=Sy;
    }
    cout<<"3. sum x ="<<Sx<<"\n4. sum y ="<<Sy<<" \n";
}

我正在尝试计算像素坐标 x 和 y 的总和。

4

1 回答 1

0

这些循环是怎么回事?

for(int x=0;x>image->width;x++)
{
    for(int y=0;image->height;y++)

应该:

for(int x=0;x<image->width;x++)
{
    for(int y=0;y<image->height;y++)

对?

另外,这是怎么回事?:

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

您正在重置Sx每个循环迭代,然后将其加倍,但随后将该计算丢弃在下一次循环迭代中。类似的问题Sy

我鼓励您逐行查看您的程序,并在公开发布问题之前真正考虑一下它在做什么。

于 2012-05-26T14:49:48.773 回答