【问题标题】:How can I access each pixel in OpenCV, perform an operation and return the new information to the image?如何访问 OpenCV 中的每个像素,执行操作并将新信息返回到图像?
【发布时间】:2018-05-01 07:34:47
【问题描述】:

我正在尝试访问图像的每个像素以执行操作并将其返回到图像,有人知道如何执行此操作,因为我已经这样做了,但是编译时标记我错了。

 #include <opencv2/opencv.hpp>
 #include <opencv2/highgui/highgui.hpp> 
 #include <stdio.h>

 using namespace cv;
 using namespace std;
 int main( int argc, char** argv ){

    char* source_window = "Source image";
    Mat nueva;
    Mat img = imread("img.jpg",CV_LOAD_IMAGE_COLOR);
    unsigned char *input = (unsigned char*)(img.data);

    int i,j,b;
    for(int i = 0;i < img.cols;i++){
        for(int j = 0;j < img.rows;j++){
           b = input[i+1][j+1] ;
           nueva[i][j] = b;
         }
     }

     imshow( source_window, nueva );

 }

【问题讨论】:

  • 你的实际目标是什么?您似乎暗示没有if 条件,您需要操纵所有像素吗?在这种情况下,您可以创建一个新的Mat
  • 猜你的问题不是关于 OpenCV,而是关于你得到的 C++ 编译器错误消息。我建议阅读错误消息,将其放在谷歌上并尝试了解您实际上被卡住的原因
  • 一般来说,当您发现自己手动迭代cv::Mat [执行相同操作] 的像素时,是时候停下来思考并参考文档了。在绝大多数情况下,已经存在合适的高级抽象,可以让您用更少的代码和通常更好的性能实现相同的目标。学习使用图书馆提供的工具。 (但是,从显示的代码来看,您应该首先花时间学习 C++ 的基础知识)

标签: image opencv pixel


【解决方案1】:

我认为你可以使用这段代码

 int nl= image.rows; 
 int nc= image.cols * image.channels();
 for (int j=0; j<nl; j++) {
    uchar* data= image.ptr<uchar>(j);
    for (int i=0; i<nc; i++) {
       data[i]= data[i]/div*div + div/2;
    }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-21
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    相关资源
    最近更新 更多