AHF
AHF

Reputation: 1072

How to use image with 4 channels

I learn the following code with 3 channels but when I try to convert it for 4 channels it show me lining on image , below code work for 3 channel image

void Vignette(Mat&img, Mat &out) {
    Mat a, b, c, d, f;
    double sigma = 280; // vignette 'aperture', the param to play with
    a = getGaussianKernel(img.cols, sigma, CV_32F);
    b = getGaussianKernel(img.rows, sigma, CV_32F);
    c = b * a.t();
    double minVal;
    double maxVal;
    cv::minMaxLoc(c, &minVal, &maxVal);
    d = c / maxVal;
    d.convertTo(d, CV_8U, 255);
    cvtColor(d, d, COLOR_GRAY2RGB);
    d.convertTo(d, CV_32F, 1.0 / 255);
    multiply(img, d, out, 1, CV_8U);
}

but I tried it for 4 channel it show me lining , 4 channel code is below

void Vignette(Mat&img, Mat &out) {
    Mat a, b, c, d, f;
    double sigma = 280; // vignette 'aperture', the param to play with
    a = getGaussianKernel(img.cols, sigma, CV_32F);
    b = getGaussianKernel(img.rows, sigma, CV_32F);
    c = b * a.t();
    double minVal;
    double maxVal;
    cv::minMaxLoc(c, &minVal, &maxVal);
    d = c / maxVal;
    d.convertTo(d, CV_8UC4, 255);
    cvtColor(d, d, COLOR_GRAY2RGBA);
    d.convertTo(d, CV_32F, 1.0 / 255);
    multiply(img, d, out, 1, CV_8UC4);
}

Upvotes: 1

Views: 1468

Answers (1)

Andrey  Smorodov
Andrey Smorodov

Reputation: 10852

this works for me:

#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
//-----------------------------------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------------------------------
void Vignette(Mat&img, Mat &out) 
{
    Mat a, b, c, d, f;
    double sigma = 280; // vignette 'aperture', the param to play with
    a = getGaussianKernel(img.cols, sigma, CV_32F);
    b = getGaussianKernel(img.rows, sigma, CV_32F);
    c = b * a.t();
    cv::normalize(c,d,0,1,cv::NORM_MINMAX);
    cvtColor(d, d, COLOR_GRAY2RGBA);
    multiply(img, d, out, 1);
}
//-----------------------------------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------------------------------
int main ( int argc, char** argv )
{
    Mat frame;
    frame=imread("D:/ImagesForTest/lena.jpg",1);
    Mat frame_f;
    cvtColor(frame, frame_f, COLOR_RGB2RGBA);
    frame_f.convertTo(frame_f,CV_32FC4,1.0/255.0);

    Vignette(frame_f,frame);
    imshow("frame",frame);
    imshow("frames",frame_f);
    cv::waitKey(0); 
}

enter image description here

Upvotes: 2

Related Questions