简体   繁体   English

OpenCV BGR值修改

[英]OpenCV BGR value modification

The following code is supposed to change all the values of BGR to 128. 以下代码应将BGR的所有值更改为128。

#include<stdio.h>
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc,char** argv)
{

    Mat input_image=imread(argv[1],1);
    if(!input_image.data)
    {
        printf("No image data\n");
        return -1;
    }
    long nRow = input_image.rows;
    long nCol = input_image.cols;

    if(input_image.isContinuous())
    {
        nCol *= nRow;
        nRow = 1;
    }

    long i,j;
    uchar* p;
    for(i=0;i<nRow;i++)
    {
        p=input_image.ptr<uchar>(i);
        for(j=0;j<nCol;j++)
        {
            p[j]=128;
        }
    }
    namedWindow("Changed color",CV_WINDOW_AUTOSIZE);
    imshow("Changed color",input_image);

    waitKey(0);

    return 0;
}

The image name is given as a command line argument. 图像名称作为命令行参数给出。 This code is compiled using openCV 3.0. 此代码是使用openCV 3.0编译的。 When the input image is supplied, only near to 35%(top half) of the image is colored gray(128,128,128). 提供输入图像时,仅接近图像的35%(上半部分)显示为灰色(128,128,128)。 The rest of the image pixels still contain the original values. 其余图像像素仍包含原始值。 I tried various pictures of various resolutions, all yield the same result. 我尝试了各种分辨率的各种图片,所有结果都相同。

Only the top part of the image has been edited. 仅图像的顶部已被编辑。 The rest is still the original image.Resolution:1280x768 A sample output image 其余部分仍是原始图像。分辨率 :1280x768 示例输出图像

The behavior you're seeing is because you're not taking into account that BGR images have 3 channels. 您看到的行为是因为您没有考虑到BGR图像具有3个通道。 So for each pixel, there are actually 3 bytes that you would need to set to 128. One way to correct it would be to modify your inner loop to the following: 因此,对于每个像素,实际上需要将3个字节设置为128。一种纠正方法是将内部循环修改为以下内容:

for (j = 0; j < (nCol * input_image.channels()); ++j)

Hope that helps. 希望能有所帮助。

Given only the top 35% or so gets changed, I'd say there's something wrong with your for loops which means you don't access the latter 65%. 如果只更改最高的35%左右,我想说您的for循环出了点问题,这意味着您无法访问后面的65%。 (Your image having multiple channels isn't the issue here, as changing only one of the channels would simply affect the colour balance, not only colour a certain portion of the image). (这里的图像没有多个通道,因为仅更改一个通道将仅影响色彩平衡,而不仅是对图像的特定部分进行着色)。 However, there's really no need to recolour the image row by row. 但是,实际上不需要逐行重新着色图像。

Miki's suggestion in the comments of using setTo to change the values is good. Miki在使用setTo更改值的注释中的建议很好。 Here's the documentation for more information. 这是文档,以获取更多信息。 Repeating the code snippet anyway: 无论如何重复代码片段:

input_image.setTo(Scalar(128,128,128))

If you don't want to lose the original image, because loading an image just to make it grey does seem kinda strange, make a new grey matrix of the same size, using something similar to the following (John Wiggles did have a point): 如果您不想丢失原始图像,因为仅加载图像使其变成灰色似乎有点奇怪,请使用与以下类似的方法制作一个相同大小的新灰色矩阵(John Wiggles确实有一点) :

Mat grey(input_image.size(), CV_8UC3, Scalar(128,128,128));

See the documentation for more ways to declare Mat types. 有关声明Mat类型的更多方法,请参见文档

Wait... you want to simply fill/replace all values with 128? 等待...您只想用128填充/替换所有值? Why use the original image at all? 为什么要完全使用原始图像? If I am understanding you correctly, then simply do this: 如果我正确理解了您,则只需执行以下操作:

1.) Create an empty Mat image with same size as the image. 1.)创建一个与图像相同大小的空Mat图像。

      (Mat Mat_object = Mat(image.rows,image.cols,IMAGE_TYPE) )

2.) Initialize all values of the Mat object to 128. (Google is your friend) 2.)将Mat对象的所有值初始化为128。(Google是您的朋友)

3.) Create two more copies of that image (if you want multiple layers of such a structure) 3.)再创建该图像的两个副本(如果您需要这种结构的多层)

               (  Mat_object.clone() helps here  )

4.) Create a: 4.)创建一个:

         vector<Mat> object, then use object.push_back(image/copy1/copy2)

5.) Use merge(object,final_image) 5.)使用merge(object,final_image)

6.) Enjoy your blank grey image 6.)欣赏空白的灰色图像

7.) Print your blank grey image 7.)打印空白的灰色图像

8.) Frame your blank grey image 8.)框住空白的灰色图像

9.) It is a blank grey image 9.)它是空白的灰色图像

wow

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

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