【问题标题】:Cannot cycle through all pixels of rectangular image matrix无法循环遍历矩形图像矩阵的所有像素
【发布时间】:2011-10-27 18:00:00
【问题描述】:

我正在尝试增加加载图像img 的亮度,但是为了循环像素,我使用了一个较小的矩阵[稍后我将使用它来应用高斯模糊]。这是我的功能:

void Dobright(cv::Mat &in,IplImage * img)
{   
    uchar* temp_ptr ;
    for( int row = 0; row < in.rows; row++) 
    {
            for ( int col = 0; col < in.cols; col++) 
            {
                CvPoint pt = {row,col};
                temp_ptr  = &((uchar*)(img->imageData + img->widthStep*pt.y))[pt.x*3];
                temp_ptr[0] += 100;
                temp_ptr[1] += 100;
                temp_ptr[2] += 100;
            }
    }
}

但是,如果原始图像是:

我得到的变亮图像为:

如您所见,有些部分比其他部分更亮,并且由于行和列不一样,因此无法访问整个图像的像素,如何解决这个问题?

【问题讨论】:

  • 你知道有用于应用高斯模糊的 OpenCV 函数,以及缩放矩阵的函数吗? (例如 cv::Mat::convert())

标签: c++ opencv


【解决方案1】:

从外观上看,您的宽度和高度混淆了,请尝试使用:
CvPoint pt = {col,row};

同样使用您当前的算法,当您的像素原始值 > 155 (156 + 100 = 1) 因为四舍五入时,您会遇到问题。尝试使用

tmp_ptr[0] = (tmp_ptr &gt; 155) ? 255 : tmp_ptr[0] + 100;

【讨论】:

    【解决方案2】:

    看起来您在这里翻转了 x 和 y。您希望 CvPoint 为 {col,row} 而不是 {row,col}

    这样想:第三行第五列是点(5,3),不是点(3,5)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多