图像处理:亮度加权2

时间:2016-02-10 01:12:11

标签: c++ opencv

我想在新图像上权衡亮度值。 我有一个5像素的图像(5px.jpg),这些亮度:50,100,150,200,250。

我有一个系数向量。

我创建了一个新的Mat Z,它结合了5px.jpg的亮度和系数。

所以,我的第一个亮度值是50(lum [0] = 50),我希望它应用于矩阵的5.1(coef [0] = 5.1)第一个像素。为此,我需要使用第一个和第二个亮度值来加权第6个像素。在我的情况下,我的第6个像素的亮度将是95,因为(0.1 * 50)+(0.9 * 100)= 95

等等......

但我不知道为什么我的代码不起作用。

我已经为一个向量here提出了类似的问题,现在,我试图适应一个图像。

输入中的照片:

enter image description here

我的输出:

enter image description here

#define MPI 3.14159265358979323846264338327950288419716939937510
#define RAD2DEG (180./MPI)
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cmath>
#include <math.h>
#include <string.h>
using namespace cv;
using namespace std;

int main()
{

    Mat image = imread("5px.jpg", 1);
    if (image.empty())
    {
        cout << "Couldn't load " << image << endl;

    }
    else
    {
        cout << "Image upload, go" << endl;
    }


    namedWindow("ImageIn", CV_WINDOW_AUTOSIZE);
    imshow("ImageIn", image);
    Mat imgGrayScale;

    cvtColor(image, imgGrayScale, CV_BGR2GRAY);


    float *deltaP = new float[imgGrayScale.cols];
    float *angle = new float[imgGrayScale.cols];
    float *coeff = new float[imgGrayScale.cols];
    int col;


    for (col = 0; col < imgGrayScale.cols; ++col)
    {
        //cout << "position x = " << col << endl;
        deltaP[col] = imgGrayScale.at<uchar>(0, col);
        //cout << "luminance = " << deltaP[col] << endl;

        angle[col] = acos(deltaP[col] / 255);
        //cout << "angle =" << angle[col] << endl;

        coeff[col] = (1 / cos(angle[col]));
        cout << "coeff = " << coeff[col] << endl;
    }

    int width = imgGrayScale.size().width;
    int height = imgGrayScale.size().height;

    int width2 = width * 5;

    int idx_coef = 0;
    Mat Z = Mat::zeros(height, width2, CV_8UC1);


    //for (int r = 0; r < imgGrayScale.rows; r++)
    //{
    //cout << "Saut de ligne "  << endl << endl << endl;
    for (int t = 0; t < imgGrayScale.cols; t++)
    {
        //cout << "Saut de colonne "  << endl;
        // Attribue le coeff à une variable
        int c = int(coeff[idx_coef]);
        //cout << "x" << t << endl;

        for (int i = 0; i < c; ++i)
        {
            Z.at<uchar>(0, c) = imgGrayScale.at<uchar>(0, t);
        }


        float alpha = fmod(coeff[idx_coef], 1.f);
        float beta = 1.f - alpha;

        Z.at<uchar>(0, c + 1) = (alpha * imgGrayScale.at<uchar>(0, t) + beta *  imgGrayScale.at<uchar>(0, t + 1));

        idx_coef++;
        coeff[idx_coef] = coeff[idx_coef] - beta;



        if (idx_coef >= width - 1)
        {

            int cc = int(coeff[idx_coef]);
            for (int i = 0; i < cc; ++i)
            {
                Z.at<uchar>(0, c) = imgGrayScale.at<uchar>(0, t);
            }
            idx_coef = 0;
            break;

        }

    }
    //}

    namedWindow("m", CV_WINDOW_AUTOSIZE);
    imshow("m", Z);
    imwrite("lumianacetest.jpg", Z);
    int t = waitKey();
    if ((char)t == 27)

        return 0;
}

1 个答案:

答案 0 :(得分:1)

  • 在访问矩阵Z时,你搞砸了索引。您无法在Z列访问c,但您需要访问当前列(如vector::push_back那样)。因此,您可以将当前索引列保留在变量idx_z中,并在每次访问Z
  • 时将其递增
  • 此处ZCV_8U,因此您的值为float,因此会失去准确性。您可以将Z创建为CV_32F,如果您需要以CV_8U格式存储值以保存图片,则最终可以转换为CV_8U
  • Z的最后一列不会设置为任何值(因此我使用值0初始化它们)。如果您需要它们具有imgGrayScale中的最后一个值,只需取消注释代码的相关部分。

这里是代码:

#define MPI 3.14159265358979323846264338327950288419716939937510
#define RAD2DEG (180./MPI)

#include <opencv2\opencv.hpp>
#include <vector>
using namespace cv;
using namespace std;

int main()
{
    Mat1b imgGrayScale = (Mat1b(2, 5) <<    50, 100, 150, 200, 250,
                                            50, 100, 150, 200, 250);

    vector<float> deltaP(imgGrayScale.cols);
    vector<float> angle(imgGrayScale.cols);
    vector<float> coeff(imgGrayScale.cols);
    int col;


    for (col = 0; col < imgGrayScale.cols; ++col)
    {
        //cout << "position x = " << col << endl;
        deltaP[col] = imgGrayScale.at<uchar>(0, col);
        //cout << "luminance = " << deltaP[col] << endl;

        angle[col] = acos(deltaP[col] / 255);
        //cout << "angle =" << angle[col] << endl;

        coeff[col] = (1 / cos(angle[col]));
        cout << "coeff = " << coeff[col] << endl;
    }

    int width = imgGrayScale.size().width;
    int height = imgGrayScale.size().height;

    int width2 = width * 5;


    Mat1f Z(height, width2, 0.f);


    for (int r = 0; r < imgGrayScale.rows; r++)
    {
        int idx_lum = 0;
        int idx_coef = 0;
        int idx_z = 0;

        vector<float> coef = coeff;

        // Set all values in Z to the last value in imgGrayScale
        Z.row(r) = imgGrayScale(r, imgGrayScale.cols-1);

        while (true)
        {
            int c = int(coef[idx_coef]);

            for (int i = 0; i < c; ++i)
            {
                Z(r, idx_z++) = imgGrayScale(r, idx_lum);
            }


            float alpha = fmod(coef[idx_coef], 1.f);
            float beta = 1.f - alpha;

            Z(r, idx_z++) = (alpha * imgGrayScale(r, idx_lum) + beta *  imgGrayScale(r, idx_lum + 1));

            idx_coef++;
            idx_lum++;
            coef[idx_coef] = coef[idx_coef] - beta;

            if (idx_lum >= imgGrayScale.cols - 1 || idx_coef >= coef.size() - 1)
            {

                int cc = int(coef[idx_coef]);
                for (int i = 0; i < cc; ++i)
                {
                    Z(r, idx_z++) = imgGrayScale(r, idx_lum);
                }
                idx_coef = 0;
                break;

            }

        }
    }

    Mat1b ZZ;
    Z.convertTo(ZZ, CV_8U);

    cout << "Float values:" << endl;
    cout << Z << endl << endl;

    cout << "Uchar values:" << endl;
    cout << ZZ << endl << endl;

    namedWindow("m", CV_WINDOW_AUTOSIZE);
    imshow("m", Z);
    imwrite("lumianacetest.png", ZZ);
    waitKey();

    return 0;
}