简体   繁体   English

在 C++ 中将 rgb 更改为灰度

[英]changing rgb to grayscale in c++

i am trying to convert a rgb image to a grayscale one this is the code i am using to generate random pixels for the existing image ... i am using a .h file to generate the output file too ... This is the image.h file : https://www65.zippyshare.com/v/yWLb2IjG/file.html and this is a the used sample : https://www65.zippyshare.com/v/Cx5U4cua/file.html我正在尝试将 rgb 图像转换为灰度图像这是我用来为现有图像生成随机像素的代码……我也在使用 .h 文件来生成输出文件……这是图像.h 文件: https : //www65.zippyshare.com/v/yWLb2IjG/file.html这是一个使用过的示例: https : //www65.zippyshare.com/v/Cx5U4cua/file.html

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<iomanip>
#include"image.h"

using namespace std;

int main(){
    Image im=readPPM("./xmas.ppm");

    int largeur=im.w, hauteur=im.h;
    cout<<"Ouverture d'une image de largeur "<<largeur<<" et de hauteur "<<hauteur<<endl;
    srand(time(NULL));

    for(int j=0;j<largeur;j++){
        for (int i=0;i<hauteur;i++){
            im(j,i).r +=(rand()%100)/300.0; 
            im(j,i).g +=(rand()%100)/300.0; 
            im(j,i).b +=(rand()%100)/300.0; 
        }
    }

    savePPM(im,"./out.ppm");
    return 0;
}

for the grayscale i have tried to add : im(j,i) +=0.2126*im(j,i).r+0.7152*im(j,i).g+0.0722*im(j,i).b;对于我尝试添加的灰度: im(j,i) +=0.2126*im(j,i).r+0.7152*im(j,i).g+0.0722*im(j,i).b;

but it does not work i tried it also with 33% for each color and 0.3 for red 0.57 for green and 0.11 for blue and same result .但它不起作用我也尝试过每种颜色为 33%,红色为 0.3,绿色为 0.57,蓝色为 0.11,结果相同。 it does not work这是行不通的

There is a very simple fix, but I'm going to explain what's going on first.有一个非常简单的修复方法,但我将首先解释发生了什么。

for the grayscale i have tried to add : im(j,i) +=0.2126*im(j,i).r+0.7152*im(j,i).g+0.0722*im(j,i).b;对于我尝试添加的灰度: im(j,i) +=0.2126*im(j,i).r+0.7152*im(j,i).g+0.0722*im(j,i).b;

Let's look at why this "doesn't work"...让我们看看为什么这“不起作用”......

So, Image::operator(int,int) returns a Rgb& .因此, Image::operator(int,int)返回一个Rgb& At least we know we're dealing with a reference so we should be able to modify the value.至少我们知道我们正在处理一个引用,所以我们应该能够修改该值。

Now, you are invoking Rgb::operator+=(float) which doesn't exist.现在,您正在调用不存在的Rgb::operator+=(float) But this won't be a compiler error because of Rgb& operator += (const Rgb &rgb) , and there is an implicit constructor Rgb(float) .但是,这将不会是因为编译错误Rgb& operator += (const Rgb &rgb)并且一个隐式构造Rgb(float) So what is that doing?那这是在做什么? Let's break it down:让我们分解一下:

  1. You calculate a grayscale value based on luminance: 0.2126*im(j,i).r+0.7152*im(j,i).g+0.0722*im(j,i).b您根据亮度计算灰度值: 0.2126*im(j,i).r+0.7152*im(j,i).g+0.0722*im(j,i).b
  2. The += operator implicitly constructs a grayscale Rgb value from that, and adds it to the existing image value. +=运算符从中隐式构造一个灰度 Rgb 值,并将添加到现有图像值中。

The above description looks like this in code:上面的描述在代码中看起来像这样:

float lum = 0.2126f * im(j,i).r + 0.7152f * im(j,i).g + 0.0722f * im(j,i).b;
img(j,i) += Rgb(lum);

By adding it, you are taking the existing color value and offsetting each channel by the same amount.通过添加它,您将采用现有的颜色值并将每个通道偏移相同的量。 This results in a new color that is much brighter.这会产生一种更亮的新颜色。

Instead, you want to change the value to be the new one you just calculated:相反,您希望将该值更改为您刚刚计算的新值:

img(j,i) = Rgb(lum);

I hope you can see the difference.我希望你能看出区别。

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

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