简体   繁体   English

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

[英]Showing image with changed pixel values openCV

I'm new to openCV and C++. 我是openCV和C ++的新手。 I would like to change the pixel values of an image I loaded and display that new image in another window to compare the results (just visually). 我想更改我加载的图像的像素值,并在另一个窗口中显示该新图像以比较结果(仅在视觉上)。 However, when I run the code, I get two original images. 但是,当我运行代码时,我得到了两个原始图像。 This means that either my for loop isn't doing what's it suppose to do (which i doubt since it makes sense to me) or the pixel value is lost and is not being saved to show the new image. 这意味着我的for循环没有执行应做的事情(我怀疑这对我来说是有意义的),或者像素值丢失并且没有保存以显示新图像。 I read a previous post that said I should include this statement after working with each pixel to set in to the altered image. 我读了一篇以前的文章,说我在处理每个像素以设置到更改后的图像后应该包含此语句。 The statement is: img.at(Point(x,y)) = color. 该语句是:img.at(Point(x,y))=颜色。

Could somebody please tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗?

Thank you 谢谢

    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) . (1) Firstly, 首先,

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

and

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

should be 应该

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

and

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

respectively. 分别。

Since, you never enter the loop because of this mistake, both images are same. 因为您永远不会因为这个错误而进入循环,所以两个图像都是相同的。

(2) . (2) Secondly, 其次,

Mat navigateImage(Mat)

should be 应该

Mat navigateImage(Mat img)

(3) . (3) Thirdly, put 第三,放

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

in main function. main功能上。

(4) . (4) Lastly, replace, 最后,更换

Mat newImage = navigateImage();

by 通过

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

else, both images will be same. 否则,两个图像将相同。

CORRECTED CODE - 正确的代码-

#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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM