请帮助我,我不能 [英] help me please I can't

查看:69
本文介绍了请帮助我,我不能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

openMP如何通过位图C ++将图像RGB转换为灰度???

How openMP to convert image RGB to grayscale by bitmap C++ ??

#include "mpi.h"
#include <iostream>
#include <math.h>
#include <stdlib.h>

#pragma once

#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace std;

void saveImage(int* RedImg,int* GreenImg,int *BlueImg,int Length,int Width,int ID);


int main(int argc,char ** argv)
{

    int localWidth;
    int localHeight;
    int coords[2];
    int stride=0;
    int PaddedSize, PaddedXSize,PaddedYSize;


    int OriginalImageWidth, OriginalImageHeight;

    //*********************************************************Read Image and save it to local arrayss*************************
        //Read Image and save it to local arrayss
        System::Drawing::Bitmap BM("C:\\text.jpg");

        OriginalImageWidth = BM.Width;
        OriginalImageHeight = BM.Height;

        int *Red =   new int  [BM.Height * BM.Width];
        int *Green = new int  [BM.Height * BM.Width];
        int *Blue =  new int  [BM.Height * BM.Width];

        for(int i=0;i<BM.Height;i++)
        {
            for(int j=0;j<BM.Width;j++)
            {
                System::Drawing::Color c= BM.GetPixel(j,i);

                Red[i * BM.Width + j] = c.R;
                Blue[i * BM.Width + j] = c.B;
                Green[i * BM.Width + j] = c.G;

            }

        }

        BM.Save("E:\\OrignalImage.jpg");
        cout<<"Original Image Saved  to be sure it read correctlly "<<endl;
    //*********************************************************Some manipulation *************************

        int *ResultRed =   new int  [BM.Height * BM.Width];
        int *ResultGreen = new int  [BM.Height * BM.Width];
        int *ResultBlue =  new int  [BM.Height * BM.Width];
        bool temp = false;
        for(int i=0;i<OriginalImageHeight;i++)
        {

            for(int j=0;j<OriginalImageWidth;j++)
            {
                if ( temp == false )
                {
                    ResultRed   [i * OriginalImageWidth + j]=0;
                    ResultGreen[i * OriginalImageWidth + j]=0;
                    ResultBlue [i * OriginalImageWidth + j]=0;

                    temp =true;
                }
                else
                {
                    ResultRed   [i * OriginalImageWidth + j]=255;
                    ResultGreen[i * OriginalImageWidth + j]=255;
                    ResultBlue [i * OriginalImageWidth + j]=255;
                    temp = false;
                }

            }
        }
        //***************************************************OPenMP********************************

    //*********************************************************Save  Image *************************


        System::Drawing::Bitmap MyNewImage(BM.Width,BM.Height);

        int R,G,B;

        for(int i=0;i<MyNewImage.Height;i++)
        {
            for(int j=0;j<MyNewImage.Width;j++)
            {

                R=ResultRed  [i * OriginalImageWidth + j];
                G=ResultGreen[i * OriginalImageWidth + j];
                B=ResultBlue [i * OriginalImageWidth + j];
                if(R<0 || R>255)
                {
                    cout<<R<<" Red"<<endl;
                    R=0;

                }
                if(G<0 || G>255)
                {
                    cout<<G<<" Green"<<endl;
                    G=0;

                }
                if(B<0 || B>255)
                {
                    cout<<B<<" Blue"<<endl;
                    B=0;

                }

                System::Drawing::Color c=System::Drawing::Color::FromArgb(R,G,B);

                MyNewImage.SetPixel(j,i,System::Drawing::Color::FromArgb(R,G,B));

            }

        }
        MyNewImage.Save("E:\\newImage.jpg");
        cout<<"result Image Saved"<<endl;


    return 0;

}

推荐答案

首先尝试想象将图片缩小到灰度的含义。为此你需要一些关于颜色的知识.... google eg for cielab



目前,颜色将以3维颜色空间表示。其中一个是RGB(更好地定义为CIELAB *)... RGB并不是很好定义,除了你引用RGB(稍微定义一点,但仍然不完全,因为不幸的是,有多个定义用于原色... ms,hp ,adobe喜欢在那里建立品牌......)。其他人是CIELAB *和追随者(但不幸的是,追随者并未真正在行业中使用,例如DIN99)。不要太在意这件事;)



回到你的问题:

而不是你想减少的三维空间一维空间的颜色(灰度)。有几种可能性来做到这一点。但是,让我们先看看最简单的一个:



减少例如三维信息到一个维度,只需将三维空间值的绝对值转换为一个数字......要理解它你需要一些矢量几何知识......但是如果没有这方面的知识,你可以做到:



对于图像的每个像素,执行以下操作

Gray = sqrt(R [x,y] ^ 2 + G [x,y] ^ 2 + B [x,y] ^ 2);

Pic [x,y] .R =灰色;

Pic [x,y] .G =灰色;

Pic [x,y] .B =灰色;





问候
First of all try to imagine what it means to reduce a picture to gray scale. For this you need a little knowledge about "colors"…. google e.g for cielab

At present colors will be represented in 3 dimensional color spaces. One of these is RGB (better defined is CIELAB*)…RGB is not really nice defined except you refer RGBs (little bit more defined, but still not exactly, because there are unfortunately more then one definitions used for the primaries…ms, hp, adobe likes to establish there brand…). Others are CIELAB* and followers (but unfortunately the followers are not really in use in industries, e.g. DIN99). Don’t bother too much about this ;)

Come back to your question:
Instead of a three dimensional space you like to reduce the color to a one dimensional space (gray scale). There are several possibilities to do this. But let us have a look first to the easiest one:

One way of reducing a e.g. three dimensional information to one dimension is, simply to convert the absolute value of your three dimensional space value into one number…to understand it you need some knowledge in vector geometry…but also without this knowledge you can do it:

For each pixel of your image do the following
Gray= sqrt(R[x,y]^2 + G[x,y]^2 + B[x,y]^2);
Pic[x,y].R= Gray;
Pic[x,y].G= Gray;
Pic[x,y].B= Gray;


Regards


这篇关于请帮助我,我不能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆