首页 > 解决方案 > 更有效的图像窗口/级别调整方式?C++

问题描述

我正在为我的图像处理课程编写这个程序,我需要创建一个大小为 30 的窗口(我假设为 30 像素,因为我的导师没有指定),我在其中对所有像素执行线性灰度转换窗框内。我编写了以下代码:

// create a window 30 image to modify
 Mat window30(relax.size(), relax.type());
 // traverse through the whole image with our 5x6 window and linearly transform the pixel values in the image
 for (int i = 0; i < window30.rows; i=i+5)
 {
      for (int j = 0; j < window30.cols; j=j+6)
      {
           for (int n = 0; n < 5; n++)
           {
                for (int m = 0; m < 6; m++)
                {
                     // if pixel value is below 50 set that value to 0
                     if (window30.at<uchar>(i+n,j+m) < 50)
                     {
                          window30.at<uchar>(i + n, j + m) = 0;
                     }
                     // if between 50 and 169 transform it to the value multiplied by 1.08875
                     else if (window30.at<uchar>(i + n, j + m) >= 50 and window30.at<int>(i + n, j + m) < 170)
                     {
                          window30.at<uchar>(i + n, j + m) = 1.08875 * window30.at<int>(i + n, j + m);
                     }
                     // else just set value to 255
                     else
                     {
                          window30.at<uchar>(i + n, j + m) = 255;
                     }
                }
           }
      }
 }
 imshow("window30", window30);
 waitKey(0);

这是我第一次使用 OpenCV 和 C++ 处理计算机视觉。我知道我的代码是垃圾,但我整天都被这个问题困扰,并且知道必须有办法做到这一点。

我正在使用 Visual Studio 2019 和最新版本的 OpenCV。

谢谢大家。

标签: c++opencvimage-processing

解决方案


推荐阅读