Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用指针加载附近的像素_C++_Opencv - Fatal编程技术网

C++ 如何使用指针加载附近的像素

C++ 如何使用指针加载附近的像素,c++,opencv,C++,Opencv,假设我有一个图像矩阵,我在一个特定的像素上,比如说4: 0 1 2 3 `4` 5 6 7 8 int rows = image->height; int cols = image->width; uchar* temp_ptr = 0; CvScalar pixels[8]; for (int col = 0; col < image->height - 2; col++) { temp_ptr = image->imageDa

假设我有一个图像矩阵,我在一个特定的像素上,比如说4:

 0  1  2  
 3 `4` 5  
 6  7  8
int rows = image->height;
int cols = image->width;
uchar* temp_ptr = 0;

CvScalar pixels[8];

for (int col = 0; col < image->height - 2; col++) {
    temp_ptr = image->imageData + image->width * col + 1;
    for (int row = 0; row < image->width - 2; row++) {
        temp_ptr += row * 3;
        pixels[0].val = temp_ptr - width * 3 - 3;  // pixel 0 from your example
        pixels[1].val = temp_ptr - width * 3;      // 1
        pixels[2].val = temp_ptr - width * 3 + 3;  // 2
        pixels[3].val = temp_ptr - 3;              // 4
        pixels[4].val = temp_ptr + 3;              // etc...
        pixels[5].val = temp_ptr + width * 3 - 3;
        pixels[6].val = temp_ptr + width * 3;
        pixels[7].val = temp_ptr + width * 3 + 3;       

        // calculate averages here and store them somewhere (in a vector perhaps)
    }
}
我正在尝试循环遍历所有像素,并尝试访问0,1,2,3,5,6,7,8,其值存储在名为
Pixel
的数组中。。。。下面是我使用OpenCV的尝试,请告诉我哪里出了问题

我正在使用指针
temp\u ptr
访问IplImage
图像

    uchar* temp_ptr=0 ;
    CvScalar Pixel[3][3];
    int rows=image->height,cols=image->width,row,col;
    for( row = 0; row < rows-2; ++row) 
    {
        for ( col = 0; col < cols-2; ++col) 
            {               
                    temp_ptr  = &((uchar*)(image->imageData + (image->widthStep*row)))[col*3];
                    for (int krow = -1 ; krow <= 1; krow++)
                    {
                        for (int kcol = -1; kcol <= 1; kcol++)
                        {       
                        temp_ptr  = &((uchar*)(image->imageData + (image->widthStep*row+krow)))[(col+kcol)*3];
                            for(int i=0; i < 3; i++)
                            {
                                for(int j=0; j < 3; j++)
                                {
                                    for(int k=0; k < 3; k++)
                                    {
                                        Pixel[i][j].val[k]=temp_ptr[k];
                                    }
                                }
                            }

                        }
                    }
             }
    }
uchar*temp\u ptr=0;
cv标量像素[3][3];
int rows=图像->高度,cols=图像->宽度,行,列;
用于(行=0;行<行-2;++行)
{
对于(col=0;col图像数据+(图像->宽度步长*行))[col*3];
对于(整数克朗=-1;克朗宽度步长*行+克朗))[(列+kcol)*3];
对于(int i=0;i<3;i++)
{
对于(int j=0;j<3;j++)
{
对于(int k=0;k<3;k++)
{
像素[i][j].val[k]=temp_ptr[k];
}
}
}
}
}
}
}

我真的不知道如何使用
temp\u ptr
加载排序舍入像素,请帮助我。

假设
image->imageData
是RGB格式的,因此每个像素有3个字节,您可以这样做:

 0  1  2  
 3 `4` 5  
 6  7  8
int rows = image->height;
int cols = image->width;
uchar* temp_ptr = 0;

CvScalar pixels[8];

for (int col = 0; col < image->height - 2; col++) {
    temp_ptr = image->imageData + image->width * col + 1;
    for (int row = 0; row < image->width - 2; row++) {
        temp_ptr += row * 3;
        pixels[0].val = temp_ptr - width * 3 - 3;  // pixel 0 from your example
        pixels[1].val = temp_ptr - width * 3;      // 1
        pixels[2].val = temp_ptr - width * 3 + 3;  // 2
        pixels[3].val = temp_ptr - 3;              // 4
        pixels[4].val = temp_ptr + 3;              // etc...
        pixels[5].val = temp_ptr + width * 3 - 3;
        pixels[6].val = temp_ptr + width * 3;
        pixels[7].val = temp_ptr + width * 3 + 3;       

        // calculate averages here and store them somewhere (in a vector perhaps)
    }
}
int rows=image->height;
int cols=图像->宽度;
uchar*temp_ptr=0;
标量像素[8];
对于(int col=0;colheight-2;col++){
温度ptr=图像->图像数据+图像->宽度*col+1;
对于(int row=0;rowwidth-2;row++){
温度ptr+=第3行;
像素[0].val=temp_ptr-width*3-3;//示例中的像素0
像素[1]。val=temp_ptr-宽度*3;//1
像素[2]。val=temp_ptr-宽度*3+3;//2
像素[3]。val=temp_ptr-3;//4
像素[4].val=temp\u ptr+3;//等。。。
像素[5]。val=temp_ptr+宽度*3-3;
像素[6]。val=temp_ptr+宽度*3;
像素[7]。val=temp_ptr+宽度*3+3;
//在此计算平均值并将其存储在某个位置(可能是向量中)
}
}

注意,我没有测试这段代码。

首先,您必须开始学习一些编程。你的完整代码乱七八糟。 我可以很快发现一些主要问题:

  • 首先,你必须从1开始你的前两个for循环(因为当你应用这个窗口时,你减少了-1),你最终会读取一些没有分配的内存地址
  • 第二,第一个temp_ptr=&((uchar*)(image->imageData+(image->widthStep*row))[col*3]是无用的,因此您可以删除它
  • 另一个

    temp_ptr  = &((uchar*)(image->imageData + (image->widthStep*row+krow)))[(col+kcol)*3];
    
如果出现小问题,则运算符优先级应为:

  temp_ptr  = &((uchar*)(image->imageData + image->widthStep*(row+krow))[(col+kcol)*3];
  • 您不需要其他3个内部循环

也不清楚你想做什么,你想得到一个特定像素的邻域(然后你不需要循环),或者你想对图像中的每个像素应用一个内核。

先生,听起来你想做卷积,当你的指尖有OpenCV时,这样做有点像用开罐器敲击你的意大利面,用钝器把它砸开

事实上,你所做的几乎完全是图像的输出,除了它还包括平均值中的中心像素

如果要排除中心像素,则可以创建自定义内核-仅创建具有适当权重的矩阵:

[.125 .125 .125
 .125  0   .125
 .125 .125 .125 ]

将这一点应用于带有。

的图像,使用六个嵌套for循环几乎肯定会出错。你需要在所有像素之间循环还是只需要那些能拥有八个“邻居”的像素?只需要那些能拥有八个邻居的像素。。。。同样,我以同样的方式访问所有像素,通过计算所有像素的平均值并将新值从颜色[RBG]数组
Pixel
…存储到中心,即
4
Pixel,来存储值。您已经获得了很多有用的提示。最重要的是,你永远不会把结果写回图像中。而且,我不想听起来很刺耳,但正如有人说的“开始学习编程-你的代码是一团乱麻”。每个人的代码一开始都是一团糟,我认为你应该试着自己解决这个问题,试着理解你在做什么。你不必为OpenCV而烦恼。你写算法有困难。一旦你准备好了,你就没事了!:-)@danbystrom-我认为你错了,我的算法很好,是的,我知道我应该把值写回像素,我只是把大括号放错了…我不知道opencv。例如,我只是猜测CvScalar.val属于uchar[3]类型。我只是想给你一个想法,不是可编译的现成代码:)alinoz你的答案错了,我不知道是谁投了赞成票。。。应该知道数据类型CvScalar是一个结构,它包含一个名为
val
的数组,您可以在其中存储
RBG
值!在这方面你是对的。我会编辑那部分,但其余部分我是对的。