2014-10-02 94 views
1

我学习下面的代码与3个通道,但是当我尝试将其转换为4个通道拿给我衬上的图像,下面的代码工作的3通道图像如何使用图像4个通道

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); 
} 

但我尝试了4通道拿给我衬,4通道代码如下

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); 
} 
+0

你能显示输入和输出图像吗? – AldurDisciple 2014-10-05 10:28:01

回答

2

这个工作对我来说:

#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

+0

为什么使用'RGB2RGBA',是不是opencv使用'BGR'? – AHF 2014-10-05 18:45:48

+0

频道的顺序并不重要,但它必须是双方相同的。你可以使用BGR,但是其他的必须是BGRA。 – 2014-10-06 10:58:58

相关问题