显示像素值已更改的图像openCV

Showing image with changed pixel values openCV

本文关键字:图像 openCV 像素 显示      更新时间:2023-10-16

我是openCV和C++的新手。我想更改我加载的图像的像素值,并在另一个窗口中显示新图像,以比较结果(只是视觉上)。然而,当我运行代码时,我会得到两个原始图像。这意味着要么我的for循环没有做它应该做的事情(我对此表示怀疑,因为这对我来说是有意义的),要么像素值丢失,无法保存以显示新图像。我读过之前的一篇帖子,说我应该在处理每个像素以设置到修改后的图像后包含这一声明。语句为:img.at(Point(x,y))=color。

有人能告诉我我做错了什么吗?

谢谢

    cv::Mat img = cv::imread("12.jpg", CV_LOAD_IMAGE_COLOR);
    // start of pixel navigation
    Mat navigateImage(Mat) {
        for(int x = 0; x > img.rows; x++) 
        {
            for(int y = 0; y > img.cols; y++){
                Vec3b color = img.at<Vec3b>(Point(x,y));
                if ( color[0] > 10 && color [1] > 10 && color[2]> 10 )
                {
                    color[0] = 0 ;
                    color[1] = 0;
                    color[2] = 0;
                    img.at<Vec3b>(Point(x,y)) = color;
                }
                else
                {
                    color.val[0] = 255 ;
                    color.val[1] = 255;
                    color.val[2] = 255;
                    img.at<Vec3b>(Point(x,y)) = color;
                }
            }
        }
        return img;
    }
    // end of pixel navigation

    int main( int argc, char** argv )
    {
    if(! img.data){
        cout << "could not open or find the image" << endl;
        return -1;}
        Mat newImage = navigateImage(img);
        cv::imshow( " Original", img);
        cv::imshow( " Altered ", newImage);

        cv::waitKey(0);
        return 0;
    }

(1)。首先,

for(int x = 0; x > img.rows; x++)

for(int y = 0; y > img.cols; y++)

应该是

for(int x = 0; x < img.cols; x++)

for(int y = 0; y < img.rows; y++)

分别。

因为,你永远不会因为这个错误而进入循环,所以两个图像是相同的。

(2)。其次,

Mat navigateImage(Mat)

应该是

Mat navigateImage(Mat img)

(3)。第三,投放

cv::Mat img = cv::imread("12.jpg", CV_LOAD_IMAGE_COLOR);

在CCD_ 1函数中。

(4)。最后一点更换,

Mat newImage = navigateImage();

通过

Mat newImage = navigateImage(img.clone());

否则,两个图像将相同。

更正代码-

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
// start of pixel navigation
Mat navigateImage(Mat img) {
    for(int x = 0; x < img.cols; x++)
    {
        for(int y = 0; y < img.rows; y++){
            Vec3b color = img.at<Vec3b>(Point(x,y));
            if ( color[0] > 10 && color [1] > 10 && color[2]> 10 )
            {
                color[0] = 0 ;
                color[1] = 0;
                color[2] = 0;
                img.at<Vec3b>(Point(x,y)) = color;
            }
            else
            {
                color.val[0] = 255 ;
                color.val[1] = 255;
                color.val[2] = 255;
                img.at<Vec3b>(Point(x,y)) = color;
            }
        }
    }
    return img;
}
// end of pixel navigation

int main( int argc, char** argv )
{
    Mat img = cv::imread("12.png", CV_LOAD_IMAGE_COLOR);
    if(! img.data){
        cout << "could not open or find the image" << endl;
        return -1;
    }
    Mat newImage = navigateImage(img.clone());
    cv::imshow( " Original", img);
    cv::imshow( " Altered ", newImage);
    cv::waitKey(0);
    return 0;
}